1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2003 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
14 |
|
* <i>natural order</i> (see {@link Comparable}), or according to a |
15 |
|
* {@link java.util.Comparator}, depending on which constructor is |
16 |
|
* used. A priority queue does not permit <tt>null</tt> elements. |
17 |
+ |
* A priority queue relying on natural ordering also does not |
18 |
+ |
* permit insertion of non-comparable objects (doing so may result |
19 |
+ |
* in <tt>ClassCastException</tt>). |
20 |
|
* |
21 |
|
* <p>The <em>head</em> of this queue is the <em>least</em> element |
22 |
|
* with respect to the specified ordering. If multiple elements are |
23 |
|
* tied for least value, the head is one of those elements -- ties are |
24 |
< |
* broken arbitrarily. The {@link #remove()} and {@link #poll()} |
25 |
< |
* methods remove and return the head of the queue, and the {@link |
26 |
< |
* #element()} and {@link #peek()} methods return, but do not delete, |
24 |
< |
* the head of the queue. |
24 |
> |
* broken arbitrarily. The queue retrieval operations <tt>poll</tt>, |
25 |
> |
* <tt>remove</tt>, <tt>peek</tt>, and <tt>element</tt> access the |
26 |
> |
* element at the head of the queue. |
27 |
|
* |
28 |
|
* <p>A priority queue is unbounded, but has an internal |
29 |
|
* <i>capacity</i> governing the size of an array used to store the |
59 |
|
* @since 1.5 |
60 |
|
* @version %I%, %G% |
61 |
|
* @author Josh Bloch |
62 |
+ |
* @param <E> the type of elements held in this collection |
63 |
|
*/ |
64 |
|
public class PriorityQueue<E> extends AbstractQueue<E> |
65 |
< |
implements Queue<E>, java.io.Serializable { |
65 |
> |
implements java.io.Serializable { |
66 |
|
|
67 |
|
private static final long serialVersionUID = -7720805057305804111L; |
68 |
|
|
199 |
|
public PriorityQueue(Collection<? extends E> c) { |
200 |
|
initializeArray(c); |
201 |
|
if (c instanceof SortedSet) { |
202 |
< |
// @fixme double-cast workaround for compiler |
200 |
< |
SortedSet<? extends E> s = (SortedSet<? extends E>) (SortedSet)c; |
202 |
> |
SortedSet<? extends E> s = (SortedSet<? extends E>)c; |
203 |
|
comparator = (Comparator<? super E>)s.comparator(); |
204 |
|
fillFromSorted(s); |
205 |
|
} else if (c instanceof PriorityQueue) { |
280 |
|
|
281 |
|
|
282 |
|
/** |
283 |
< |
* Inserts the specified element to this priority queue. |
283 |
> |
* Inserts the specified element into this priority queue. |
284 |
|
* |
285 |
|
* @return <tt>true</tt> |
286 |
|
* @throws ClassCastException if the specified element cannot be compared |
325 |
|
return offer(o); |
326 |
|
} |
327 |
|
|
326 |
– |
|
328 |
|
/** |
329 |
< |
* Adds all of the elements in the specified collection to this queue. |
330 |
< |
* The behavior of this operation is undefined if |
330 |
< |
* the specified collection is modified while the operation is in |
331 |
< |
* progress. (This implies that the behavior of this call is undefined if |
332 |
< |
* the specified collection is this queue, and this queue is nonempty.) |
333 |
< |
* <p> |
334 |
< |
* This implementation iterates over the specified collection, and adds |
335 |
< |
* each object returned by the iterator to this collection, in turn. |
336 |
< |
* @param c collection whose elements are to be added to this queue |
337 |
< |
* @return <tt>true</tt> if this queue changed as a result of the |
338 |
< |
* call. |
339 |
< |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
340 |
< |
* is <tt>null</tt> |
341 |
< |
* @throws ClassCastException if any element cannot be compared |
342 |
< |
* with elements currently in the priority queue according |
343 |
< |
* to the priority queue's ordering. |
329 |
> |
* Removes a single instance of the specified element from this |
330 |
> |
* collection, if it is present. |
331 |
|
*/ |
345 |
– |
public boolean addAll(Collection<? extends E> c) { |
346 |
– |
return super.addAll(c); |
347 |
– |
} |
348 |
– |
|
332 |
|
public boolean remove(Object o) { |
333 |
|
if (o == null) |
334 |
|
return false; |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
< |
* Remove all elements from the priority queue. |
463 |
> |
* Removes all elements from the priority queue. |
464 |
> |
* The queue will be empty after this call returns. |
465 |
|
*/ |
466 |
|
public void clear() { |
467 |
|
modCount++; |