-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractSortedMap.java
34 lines (29 loc) · 1016 Bytes
/
AbstractSortedMap.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Comparator;
public abstract class AbstractSortedMap<K, V> extends AbstractMap<K, V> implements SortedMap<K, V>{
private Comparator<K> comp;
protected AbstractSortedMap(Comparator<K> c){
comp = c;
}
protected AbstractSortedMap(){
this(new DefaultComparator<K>());
}
protected int compare(Entry<K, V> a, Entry<K, V> b){
return comp.compare(a.getKey(), b.getKey());
}
protected int compare(K a, Entry<K, V> b){
return comp.compare(a, b.getKey());
}
protected int compare(Entry<K, V> a, K b){
return comp.compare(a.getKey(), b);
}
protected int compare(K a, K b){
return comp.compare(a, b);
}
protected boolean checkKey(K key) throws IllegalArgumentException{
try{
return (comp.compare(key, key) == 0);
}catch(ClassCastException e){
throw new IllegalArgumentException("Incompatible key");
}
}
}