7 |
|
package java.util; |
8 |
|
|
9 |
|
/** |
10 |
< |
* AbstractQueue provides default implementations of add, remove, and |
11 |
< |
* element based on offer, poll, and peek, respectively but that throw |
12 |
< |
* exceptions instead of indicating failure via false or null returns. |
13 |
< |
* The provided implementations all assume that the base implementation |
10 |
> |
* <tt>AbstractQueue</tt> provides default implementations of |
11 |
> |
* {@link #add add}, {@link #remove remove}, and {@link #element element} |
12 |
> |
* based on |
13 |
> |
* {@link #offer offer}, {@link #poll poll}, and {@link #peek peek}, |
14 |
> |
* respectively but that |
15 |
> |
* throw exceptions instead of indicating failure via <tt>false</tt> or |
16 |
> |
* <tt>null</tt> returns. |
17 |
> |
* <p>The provided implementations all assume that the base implementation |
18 |
|
* does <em>not</em> allow <tt>null</tt> elements. |
19 |
|
* @since 1.5 |
20 |
|
* @author Doug Lea |
21 |
|
*/ |
22 |
< |
|
23 |
< |
public abstract class AbstractQueue<E> extends AbstractCollection<E> implements Queue<E> { |
24 |
< |
|
25 |
< |
public boolean add(E x) { |
26 |
< |
if (offer(x)) |
22 |
> |
public abstract class AbstractQueue<E> |
23 |
> |
extends AbstractCollection<E> |
24 |
> |
implements Queue<E> { |
25 |
> |
|
26 |
> |
// note that optional methods are not optional here or in our subclasses, |
27 |
> |
// so we redefine each optional method to document that it is not optional |
28 |
> |
// We also inherit, or define, all necessary @throws comments |
29 |
> |
|
30 |
> |
/** |
31 |
> |
* Adds the specified element to this queue. |
32 |
> |
* @return <tt>true</tt> (as per the general contract of |
33 |
> |
* <tt>Collection.add</tt>). |
34 |
> |
* |
35 |
> |
* @throws NullPointerException if the element is null |
36 |
> |
*/ |
37 |
> |
public boolean add(E o) { |
38 |
> |
if (offer(o)) |
39 |
|
return true; |
40 |
|
else |
41 |
|
throw new IllegalStateException("Queue full"); |
42 |
|
} |
43 |
|
|
44 |
+ |
/** |
45 |
+ |
* Adds all of the elements in the specified collection to this queue. |
46 |
+ |
* The behavior of this operation is undefined if |
47 |
+ |
* the specified collection is modified while the operation is in |
48 |
+ |
* progress. (This implies that the behavior of this call is undefined if |
49 |
+ |
* the specified collection is this queue, and this queue is nonempty.) |
50 |
+ |
* <p> |
51 |
+ |
* This implementation iterates over the specified collection, and adds |
52 |
+ |
* each object returned by the iterator to this collection, in turn. |
53 |
+ |
* |
54 |
+ |
* @param c collection whose elements are to be added to this queue |
55 |
+ |
* @return <tt>true</tt> if this collection changed as a result of the |
56 |
+ |
* call. |
57 |
+ |
* @throws NullPointerException if <tt>c</tt> is <tt>null</tt> |
58 |
+ |
* |
59 |
+ |
*/ |
60 |
+ |
boolean addAll(Collection<? extends E> c) { |
61 |
+ |
return super.addAll(c); |
62 |
+ |
} |
63 |
+ |
|
64 |
+ |
/** @throws NoSuchElementException {@inheritDoc} */ |
65 |
|
public E remove() { |
66 |
|
E x = poll(); |
67 |
|
if (x != null) |
70 |
|
throw new NoSuchElementException(); |
71 |
|
} |
72 |
|
|
73 |
+ |
/** @throws NoSuchElementException {@inheritDoc} */ |
74 |
|
public E element() { |
75 |
|
E x = peek(); |
76 |
|
if (x != null) |
79 |
|
throw new NoSuchElementException(); |
80 |
|
} |
81 |
|
|
82 |
+ |
/** |
83 |
+ |
* Removes all of the elements from this collection. |
84 |
+ |
* The collection will be empty after this call returns. |
85 |
+ |
* <p>This implementation repeatedly invokes {@link #poll poll} until it |
86 |
+ |
* returns <tt>null</tt>. |
87 |
+ |
*/ |
88 |
|
public void clear() { |
89 |
|
while (poll() != null) |
90 |
|
; |