10 |
|
import java.util.*; |
11 |
|
|
12 |
|
/** |
13 |
< |
* An unbounded {@link BlockingQueue blocking queue} based on a {@link |
14 |
< |
* PriorityQueue}, obeying its ordering rules and implementation |
15 |
< |
* characteristics. While this queue is logically unbounded, |
13 |
> |
* An unbounded {@linkplain BlockingQueue blocking queue} based on a |
14 |
> |
* {@link PriorityQueue}, |
15 |
> |
* obeying its ordering rules and implementation characteristics. |
16 |
> |
* While this queue is logically unbounded, |
17 |
|
* attempted additions may fail due to resource exhaustion (causing |
18 |
< |
* <tt>OutOfMemoryError</tt>) when <tt>Integer.MAX_VALUE</tt> elements |
18 |
< |
* are held. |
18 |
> |
* <tt>OutOfMemoryError</tt>). |
19 |
|
* @since 1.5 |
20 |
|
* @author Doug Lea |
21 |
|
*/ |
27 |
|
private final Condition notEmpty = lock.newCondition(); |
28 |
|
|
29 |
|
/** |
30 |
< |
* Create a <tt>PriorityBlockingQueue</tt> with the default initial |
30 |
> |
* Creates a <tt>PriorityBlockingQueue</tt> with the default initial |
31 |
|
* capacity |
32 |
|
* (11) that orders its elements according to their natural |
33 |
|
* ordering (using <tt>Comparable</tt>.) |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* Create a <tt>PriorityBlockingQueue</tt> with the specified initial |
40 |
> |
* Creates a <tt>PriorityBlockingQueue</tt> with the specified initial |
41 |
|
* capacity |
42 |
|
* that orders its elements according to their natural ordering |
43 |
|
* (using <tt>Comparable</tt>.) |
44 |
|
* |
45 |
|
* @param initialCapacity the initial capacity for this priority queue. |
46 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
47 |
+ |
* than 1 |
48 |
|
*/ |
49 |
|
public PriorityBlockingQueue(int initialCapacity) { |
50 |
|
q = new PriorityQueue<E>(initialCapacity, null); |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* Create a <tt>PriorityBlockingQueue</tt> with the specified initial |
54 |
> |
* Creates a <tt>PriorityBlockingQueue</tt> with the specified initial |
55 |
|
* capacity |
56 |
|
* that orders its elements according to the specified comparator. |
57 |
|
* |
59 |
|
* @param comparator the comparator used to order this priority queue. |
60 |
|
* If <tt>null</tt> then the order depends on the elements' natural |
61 |
|
* ordering. |
62 |
+ |
* @throws IllegalArgumentException if <tt>initialCapacity</tt> is less |
63 |
+ |
* than 1 |
64 |
|
*/ |
65 |
|
public PriorityBlockingQueue(int initialCapacity, |
66 |
|
Comparator<? super E> comparator) { |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* Create a <tt>PriorityBlockingQueue</tt> containing the elements |
71 |
> |
* Creates a <tt>PriorityBlockingQueue</tt> containing the elements |
72 |
|
* in the specified collection. The priority queue has an initial |
73 |
|
* capacity of 110% of the size of the specified collection. If |
74 |
|
* the specified collection is a {@link SortedSet} or a {@link |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
< |
// these first two override just to get the throws docs |
94 |
> |
// these first few override just to update doc comments |
95 |
|
|
96 |
|
/** |
97 |
+ |
* Adds the specified element to this queue. |
98 |
+ |
* @return <tt>true</tt> (as per the general contract of |
99 |
+ |
* <tt>Collection.add</tt>). |
100 |
+ |
* |
101 |
|
* @throws NullPointerException {@inheritDoc} |
102 |
+ |
* @throws ClassCastException if the specified element cannot be compared |
103 |
+ |
* with elements currently in the priority queue according |
104 |
+ |
* to the priority queue's ordering. |
105 |
|
*/ |
106 |
< |
public boolean add(E element) { |
107 |
< |
return super.add(element); |
106 |
> |
public boolean add(E o) { |
107 |
> |
return super.add(o); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
+ |
* Adds all of the elements in the specified collection to this queue. |
112 |
+ |
* The behavior of this operation is undefined if |
113 |
+ |
* the specified collection is modified while the operation is in |
114 |
+ |
* progress. (This implies that the behavior of this call is undefined if |
115 |
+ |
* the specified collection is this queue, and this queue is nonempty.) |
116 |
+ |
* <p> |
117 |
+ |
* This implementation iterates over the specified collection, and adds |
118 |
+ |
* each object returned by the iterator to this collection, in turn. |
119 |
|
* @throws NullPointerException {@inheritDoc} |
120 |
+ |
* @throws ClassCastException if any element cannot be compared |
121 |
+ |
* with elements currently in the priority queue according |
122 |
+ |
* to the priority queue's ordering. |
123 |
|
*/ |
124 |
|
public boolean addAll(Collection<? extends E> c) { |
125 |
|
return super.addAll(c); |
126 |
|
} |
127 |
|
|
128 |
+ |
/** |
129 |
+ |
* Returns the comparator used to order this collection, or <tt>null</tt> |
130 |
+ |
* if this collection is sorted according to its elements natural ordering |
131 |
+ |
* (using <tt>Comparable</tt>.) |
132 |
+ |
* |
133 |
+ |
* @return the comparator used to order this collection, or <tt>null</tt> |
134 |
+ |
* if this collection is sorted according to its elements natural ordering. |
135 |
+ |
*/ |
136 |
|
public Comparator comparator() { |
137 |
|
return q.comparator(); |
138 |
|
} |
139 |
|
|
140 |
< |
/** |
141 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt> |
142 |
< |
**/ |
140 |
> |
/** |
141 |
> |
* Adds the specified element to this priority queue. |
142 |
> |
* |
143 |
> |
* @return <tt>true</tt> |
144 |
> |
* @throws ClassCastException if the specified element cannot be compared |
145 |
> |
* with elements currently in the priority queue according |
146 |
> |
* to the priority queue's ordering. |
147 |
> |
* @throws NullPointerException {@inheritDoc} |
148 |
> |
*/ |
149 |
|
public boolean offer(E o) { |
150 |
|
if (o == null) throw new NullPointerException(); |
151 |
|
lock.lock(); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
< |
public void put(E o) throws InterruptedException { |
163 |
> |
/** |
164 |
> |
* Adds the specified element to this priority queue. As the queue is |
165 |
> |
* unbounded this method will never block. |
166 |
> |
* @throws ClassCastException if the element cannot be compared |
167 |
> |
* with elements currently in the priority queue according |
168 |
> |
* to the priority queue's ordering. |
169 |
> |
* @throws NullPointerException {@inheritDoc} |
170 |
> |
*/ |
171 |
> |
public void put(E o) { |
172 |
|
offer(o); // never need to block |
173 |
|
} |
174 |
|
|
175 |
< |
public boolean offer(E o, long timeout, TimeUnit unit) |
176 |
< |
throws InterruptedException { |
175 |
> |
/** |
176 |
> |
* Adds the specified element to this priority queue. As the queue is |
177 |
> |
* unbounded this method will never block. |
178 |
> |
* @param o {@inheritDoc} |
179 |
> |
* @param timeout This parameter is ignored as the method never blocks |
180 |
> |
* @param unit This parameter is ignored as the method never blocks |
181 |
> |
* @throws ClassCastException if the element cannot be compared |
182 |
> |
* with elements currently in the priority queue according |
183 |
> |
* to the priority queue's ordering. |
184 |
> |
* @throws NullPointerException {@inheritDoc} |
185 |
> |
* @return <tt>true</tt> |
186 |
> |
*/ |
187 |
> |
public boolean offer(E o, long timeout, TimeUnit unit) { |
188 |
|
return offer(o); // never need to block |
189 |
|
} |
190 |
|
|
265 |
|
|
266 |
|
/** |
267 |
|
* Always returns <tt>Integer.MAX_VALUE</tt> because |
268 |
< |
* PriorityBlockingQueues are not capacity constrained. |
268 |
> |
* a <tt>PriorityBlockingQueue</tt> is not capacity constrained. |
269 |
|
* @return <tt>Integer.MAX_VALUE</tt> |
270 |
|
*/ |
271 |
|
public int remainingCapacity() { |
272 |
|
return Integer.MAX_VALUE; |
273 |
|
} |
274 |
|
|
275 |
+ |
/** |
276 |
+ |
* Removes a single instance of the specified element from this |
277 |
+ |
* queue, if it is present. More formally, |
278 |
+ |
* removes an element <tt>e</tt> such that <tt>(o==null ? e==null : |
279 |
+ |
* o.equals(e))</tt>, if the queue contains one or more such |
280 |
+ |
* elements. Returns <tt>true</tt> if the queue contained the |
281 |
+ |
* specified element (or equivalently, if the queue changed as a |
282 |
+ |
* result of the call). |
283 |
+ |
* |
284 |
+ |
* <p>This implementation iterates over the queue looking for the |
285 |
+ |
* specified element. If it finds the element, it removes the element |
286 |
+ |
* from the queue using the iterator's remove method.<p> |
287 |
+ |
* |
288 |
+ |
*/ |
289 |
|
public boolean remove(Object o) { |
290 |
|
lock.lock(); |
291 |
|
try { |
337 |
|
} |
338 |
|
} |
339 |
|
|
340 |
+ |
/** |
341 |
+ |
* Returns an iterator over the elements in this queue. The iterator |
342 |
+ |
* does not return the elements in any particular order. |
343 |
+ |
* |
344 |
+ |
* @return an iterator over the elements in this queue. |
345 |
+ |
*/ |
346 |
|
public Iterator<E> iterator() { |
347 |
|
lock.lock(); |
348 |
|
try { |