15 |
|
* obeying its ordering rules and implementation characteristics. |
16 |
|
* @since 1.5 |
17 |
|
* @author Doug Lea |
18 |
< |
**/ |
18 |
> |
*/ |
19 |
|
public class PriorityBlockingQueue<E> extends AbstractQueue<E> |
20 |
< |
implements BlockingQueue<E>, java.io.Serializable { |
20 |
> |
implements Sorted, BlockingQueue<E>, java.io.Serializable { |
21 |
|
|
22 |
|
private final PriorityQueue<E> q; |
23 |
|
private final ReentrantLock lock = new ReentrantLock(true); |
24 |
|
private final Condition notEmpty = lock.newCondition(); |
25 |
|
|
26 |
|
/** |
27 |
< |
* Create a <tt>PriorityBlockingQueue</tt> with the default initial capacity |
27 |
> |
* Create a <tt>PriorityBlockingQueue</tt> with the default initial |
28 |
> |
* capacity |
29 |
|
* (11) that orders its elements according to their natural |
30 |
|
* ordering (using <tt>Comparable</tt>.) |
31 |
|
*/ |
56 |
|
* ordering. |
57 |
|
*/ |
58 |
|
public PriorityBlockingQueue(int initialCapacity, |
59 |
< |
Comparator<E> comparator) { |
59 |
> |
Comparator<? super E> comparator) { |
60 |
|
q = new PriorityQueue<E>(initialCapacity, comparator); |
61 |
|
} |
62 |
|
|
72 |
|
* <tt>Sorted</tt>, the priority queue is ordered according to |
73 |
|
* its elements' natural order. |
74 |
|
* |
75 |
< |
* @param initialElements the collection whose elements are to be placed |
75 |
> |
* @param c the collection whose elements are to be placed |
76 |
|
* into this priority queue. |
77 |
|
* @throws ClassCastException if elements of the specified collection |
78 |
|
* cannot be compared to one another according to the priority |
79 |
|
* queue's ordering. |
80 |
< |
* @throws NullPointerException if the specified collection or an |
81 |
< |
* element of the specified collection is <tt>null</tt>. |
80 |
> |
* @throws NullPointerException if <tt>c</tt> or any element within it |
81 |
> |
* is <tt>null</tt> |
82 |
|
*/ |
83 |
< |
public PriorityBlockingQueue(Collection<E> initialElements) { |
84 |
< |
q = new PriorityQueue<E>(initialElements); |
83 |
> |
public PriorityBlockingQueue(Collection<? extends E> c) { |
84 |
> |
q = new PriorityQueue<E>(c); |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
// these first two override just to get the throws docs |
89 |
|
|
90 |
|
/** |
91 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt>. |
91 |
> |
* @throws NullPointerException {@inheritDoc} |
92 |
|
*/ |
93 |
|
public boolean add(E element) { |
94 |
|
return super.add(element); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* @throws NullPointerException if any element is <tt>null</tt>. |
98 |
> |
* @throws NullPointerException {@inheritDoc} |
99 |
|
*/ |
100 |
|
public boolean addAll(Collection<? extends E> c) { |
101 |
|
return super.addAll(c); |
105 |
|
return q.comparator(); |
106 |
|
} |
107 |
|
|
108 |
< |
/** @throws NullPointerException if <tt>x</tt> is <tt>null</tt> */ |
109 |
< |
public boolean offer(E x) { |
110 |
< |
if (x == null) throw new NullPointerException(); |
108 |
> |
/** |
109 |
> |
* @throws NullPointerException if the specified element is <tt>null</tt> |
110 |
> |
**/ |
111 |
> |
public boolean offer(E o) { |
112 |
> |
if (o == null) throw new NullPointerException(); |
113 |
|
lock.lock(); |
114 |
|
try { |
115 |
< |
boolean ok = q.offer(x); |
115 |
> |
boolean ok = q.offer(o); |
116 |
|
assert ok; |
117 |
|
notEmpty.signal(); |
118 |
|
return true; |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
< |
public void put(E x) throws InterruptedException { |
126 |
< |
offer(x); // never need to block |
125 |
> |
public void put(E o) throws InterruptedException { |
126 |
> |
offer(o); // never need to block |
127 |
|
} |
128 |
|
|
129 |
< |
public boolean offer(E x, long timeout, TimeUnit unit) |
129 |
> |
public boolean offer(E o, long timeout, TimeUnit unit) |
130 |
|
throws InterruptedException { |
131 |
< |
return offer(x); // never need to block |
131 |
> |
return offer(o); // never need to block |
132 |
|
} |
133 |
|
|
134 |
|
public E take() throws InterruptedException { |
215 |
|
return Integer.MAX_VALUE; |
216 |
|
} |
217 |
|
|
218 |
< |
public boolean remove(Object x) { |
218 |
> |
public boolean remove(Object o) { |
219 |
|
lock.lock(); |
220 |
|
try { |
221 |
< |
return q.remove(x); |
221 |
> |
return q.remove(o); |
222 |
|
} |
223 |
|
finally { |
224 |
|
lock.unlock(); |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
< |
public boolean contains(Object x) { |
228 |
> |
public boolean contains(Object o) { |
229 |
|
lock.lock(); |
230 |
|
try { |
231 |
< |
return q.contains(x); |
231 |
> |
return q.contains(o); |
232 |
|
} |
233 |
|
finally { |
234 |
|
lock.unlock(); |