-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractPriorityQueue.java
More file actions
53 lines (49 loc) · 1.2 KB
/
Copy pathAbstractPriorityQueue.java
File metadata and controls
53 lines (49 loc) · 1.2 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package APQImplementation;
import java.util.Comparator;
public abstract class AbstractPriorityQueue<K,V> implements PriorityQueue<K,V>{
// nested PQ class
static class PQ<K,V> implements Entry<K,V>{
private K key;
private V value;
public PQ(K key, V value) {
this.key=key;
this.value=value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
void setKey(K key) {
this.key=key;
}
void setValue(V value) {
this.value = value;
}
} // end of nested
protected Comparator<K> comparator1;
protected AbstractPriorityQueue(Comparator<K> comparator2) {
comparator1=comparator2;
}
protected AbstractPriorityQueue() {
this(new DefaultComparator<K>());
}
protected int compare(Entry<K,V> obj1, Entry<K,V> obj2) {
return comparator1.compare(obj1.getKey(), obj2.getKey());
}
protected boolean checkKey(K key) throws IllegalArgumentException {
try {
return (comparator1.compare(key,key)==0);
} catch (Exception e){
throw new IllegalArgumentException("Error: The key is not compatible.");
}
}
public boolean isEmpty() {
if (size()==0) {
return true;
} else {
return false;
}
}
}