1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain. Use, modify, and |
4 |
< |
* redistribute this code in any way without acknowledgement. |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package java.util; |
9 |
|
/** |
10 |
|
* This class provides skeletal implementations of some {@link Queue} |
11 |
|
* operations. The implementations in this class are appropriate when |
12 |
< |
* the base implementation does <em>not</em> allow <tt>null</tt> |
12 |
> |
* the base implementation does <em>not</em> allow {@code null} |
13 |
|
* elements. Methods {@link #add add}, {@link #remove remove}, and |
14 |
|
* {@link #element element} are based on {@link #offer offer}, {@link |
15 |
< |
* #poll poll}, and {@link #peek peek}, respectively but throw |
16 |
< |
* exceptions instead of indicating failure via <tt>false</tt> or |
17 |
< |
* <tt>null</tt> returns. |
15 |
> |
* #poll poll}, and {@link #peek peek}, respectively, but throw |
16 |
> |
* exceptions instead of indicating failure via {@code false} or |
17 |
> |
* {@code null} returns. |
18 |
|
* |
19 |
< |
* <p> A <tt>Queue</tt> implementation that extends this class must |
19 |
> |
* <p>A {@code Queue} implementation that extends this class must |
20 |
|
* minimally define a method {@link Queue#offer} which does not permit |
21 |
< |
* insertion of <tt>null</tt> elements, along with methods {@link |
22 |
< |
* Queue#peek}, {@link Queue#poll}, {@link Collection#size}, {@link |
23 |
< |
* Collection#remove}, and a {@link Collection#iterator} supporting |
24 |
< |
* {@link Iterator#remove}. If these requirements cannot be met, |
25 |
< |
* consider instead subclassing {@link AbstractCollection}. |
21 |
> |
* insertion of {@code null} elements, along with methods {@link |
22 |
> |
* Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and |
23 |
> |
* {@link Collection#iterator}. Typically, additional methods will be |
24 |
> |
* overridden as well. If these requirements cannot be met, consider |
25 |
> |
* instead subclassing {@link AbstractCollection}. |
26 |
> |
* |
27 |
> |
* <p>This class is a member of the |
28 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
29 |
> |
* Java Collections Framework</a>. |
30 |
|
* |
31 |
|
* @since 1.5 |
32 |
|
* @author Doug Lea |
33 |
+ |
* @param <E> the type of elements held in this collection |
34 |
|
*/ |
35 |
|
public abstract class AbstractQueue<E> |
36 |
|
extends AbstractCollection<E> |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* Inserts the specified element to this queue, if possible. |
47 |
< |
* |
48 |
< |
* @param o the element to add. |
49 |
< |
* @return <tt>true</tt> if it was possible to add the element to |
50 |
< |
* this queue, else <tt>false</tt> |
51 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt> |
52 |
< |
*/ |
53 |
< |
// public boolean offer(E o) { return false; } |
54 |
< |
// FIXME: Replace above no-op with following abstract version |
55 |
< |
// when javac allows it. |
56 |
< |
public abstract boolean offer(E o); |
57 |
< |
|
58 |
< |
/** |
59 |
< |
* Adds the specified element to this queue. This implementation |
60 |
< |
* returns <tt>true</tt> if <tt>offer</tt> succeeds, else |
61 |
< |
* throws an IllegalStateException. |
62 |
< |
* th |
63 |
< |
* @param o the element |
59 |
< |
* @return <tt>true</tt> (as per the general contract of |
60 |
< |
* <tt>Collection.add</tt>). |
61 |
< |
* |
62 |
< |
* @throws NullPointerException if the specified element is <tt>null</tt> |
63 |
< |
* @throws IllegalStateException if element cannot be added |
46 |
> |
* Inserts the specified element into this queue if it is possible to do so |
47 |
> |
* immediately without violating capacity restrictions, returning |
48 |
> |
* {@code true} upon success and throwing an {@code IllegalStateException} |
49 |
> |
* if no space is currently available. |
50 |
> |
* |
51 |
> |
* <p>This implementation returns {@code true} if {@code offer} succeeds, |
52 |
> |
* else throws an {@code IllegalStateException}. |
53 |
> |
* |
54 |
> |
* @param e the element to add |
55 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
56 |
> |
* @throws IllegalStateException if the element cannot be added at this |
57 |
> |
* time due to capacity restrictions |
58 |
> |
* @throws ClassCastException if the class of the specified element |
59 |
> |
* prevents it from being added to this queue |
60 |
> |
* @throws NullPointerException if the specified element is null and |
61 |
> |
* this queue does not permit null elements |
62 |
> |
* @throws IllegalArgumentException if some property of this element |
63 |
> |
* prevents it from being added to this queue |
64 |
|
*/ |
65 |
< |
public boolean add(E o) { |
66 |
< |
if (offer(o)) |
65 |
> |
public boolean add(E e) { |
66 |
> |
if (offer(e)) |
67 |
|
return true; |
68 |
|
else |
69 |
|
throw new IllegalStateException("Queue full"); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* Retrieves and removes the head of this queue. |
74 |
< |
* This implementation returns the result of <tt>poll</tt> |
73 |
> |
* Retrieves and removes the head of this queue. This method differs |
74 |
> |
* from {@link #poll poll} only in that it throws an exception if this |
75 |
> |
* queue is empty. |
76 |
> |
* |
77 |
> |
* <p>This implementation returns the result of {@code poll} |
78 |
|
* unless the queue is empty. |
79 |
|
* |
80 |
< |
* @return the head of this queue. |
81 |
< |
* @throws NoSuchElementException if this queue is empty. |
80 |
> |
* @return the head of this queue |
81 |
> |
* @throws NoSuchElementException if this queue is empty |
82 |
|
*/ |
83 |
|
public E remove() { |
84 |
|
E x = poll(); |
88 |
|
throw new NoSuchElementException(); |
89 |
|
} |
90 |
|
|
88 |
– |
|
91 |
|
/** |
92 |
< |
* Retrieves, but does not remove, the head of this queue. |
93 |
< |
* This implementation returns the result of <tt>peek</tt> |
92 |
> |
* Retrieves, but does not remove, the head of this queue. This method |
93 |
> |
* differs from {@link #peek peek} only in that it throws an exception if |
94 |
> |
* this queue is empty. |
95 |
> |
* |
96 |
> |
* <p>This implementation returns the result of {@code peek} |
97 |
|
* unless the queue is empty. |
98 |
|
* |
99 |
< |
* @return the head of this queue. |
100 |
< |
* @throws NoSuchElementException if this queue is empty. |
99 |
> |
* @return the head of this queue |
100 |
> |
* @throws NoSuchElementException if this queue is empty |
101 |
|
*/ |
102 |
|
public E element() { |
103 |
|
E x = peek(); |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
< |
* Removes all of the elements from this collection. |
112 |
< |
* The collection will be empty after this call returns. |
111 |
> |
* Removes all of the elements from this queue. |
112 |
> |
* The queue will be empty after this call returns. |
113 |
> |
* |
114 |
|
* <p>This implementation repeatedly invokes {@link #poll poll} until it |
115 |
< |
* returns <tt>null</tt>. |
115 |
> |
* returns {@code null}. |
116 |
|
*/ |
117 |
|
public void clear() { |
118 |
|
while (poll() != null) |
119 |
|
; |
120 |
|
} |
121 |
|
|
116 |
– |
// Declarations that mostly just remove optionality clauses. |
117 |
– |
|
118 |
– |
/** |
119 |
– |
* Removes a single instance of the specified element from this |
120 |
– |
* queue, if one is present. More formally, removes an element |
121 |
– |
* <tt>e</tt> such that <tt>(o==null ? e==null : |
122 |
– |
* o.equals(e))</tt>, if the queue contains such an element. |
123 |
– |
* Returns <tt>true</tt> if the queue contained the specified |
124 |
– |
* element (or equivalently, if the queue changed as a result of |
125 |
– |
* the call). |
126 |
– |
* |
127 |
– |
* @param o the element to remove |
128 |
– |
* @return true if the element was removed |
129 |
– |
*/ |
130 |
– |
public abstract boolean remove(Object o); |
131 |
– |
|
132 |
– |
/** |
133 |
– |
* Removes from this queue all of its elements that are contained in |
134 |
– |
* the specified collection. <p> |
135 |
– |
* |
136 |
– |
* This implementation iterates over this queue, checking each |
137 |
– |
* element returned by the iterator in turn to see if it's contained |
138 |
– |
* in the specified collection. If it's so contained, it's removed from |
139 |
– |
* this queue with the iterator's <tt>remove</tt> method.<p> |
140 |
– |
* |
141 |
– |
* @param c elements to be removed from this collection. |
142 |
– |
* @return <tt>true</tt> if this queue changed as a result of the |
143 |
– |
* call. |
144 |
– |
* @throws NullPointerException if the specified collection is null. |
145 |
– |
*/ |
146 |
– |
public boolean removeAll(Collection<?> c) { |
147 |
– |
return super.removeAll(c); |
148 |
– |
} |
149 |
– |
|
150 |
– |
/** |
151 |
– |
* Retains only the elements in this queue that are contained in the |
152 |
– |
* specified collection. In other words, removes |
153 |
– |
* from this queue all of its elements that are not contained in the |
154 |
– |
* specified collection. <p> |
155 |
– |
* |
156 |
– |
* This implementation iterates over this queue, checking each |
157 |
– |
* element returned by the iterator in turn to see if it's contained |
158 |
– |
* in the specified collection. If it's not so contained, it's removed |
159 |
– |
* from this queue with the iterator's <tt>remove</tt> method.<p> |
160 |
– |
* |
161 |
– |
* @param c elements to be retained in this collection. |
162 |
– |
* @return <tt>true</tt> if this queue changed as a result of the |
163 |
– |
* call. |
164 |
– |
* @throws NullPointerException if the specified collection is null. |
165 |
– |
* |
166 |
– |
*/ |
167 |
– |
public boolean retainAll(Collection<?> c) { |
168 |
– |
return super.retainAll(c); |
169 |
– |
} |
170 |
– |
|
122 |
|
/** |
123 |
|
* Adds all of the elements in the specified collection to this |
124 |
< |
* queue. The behavior of this operation is undefined if the |
125 |
< |
* specified collection is modified while the operation is in |
126 |
< |
* progress. (This implies that the behavior of this call is |
127 |
< |
* undefined if the specified collection is this queue, and this |
128 |
< |
* queue is nonempty.) |
129 |
< |
* |
130 |
< |
* <p> This implementation iterates over the specified collection, |
131 |
< |
* and uses the <tt>add</tt> method to insert each object returned by |
132 |
< |
* the iterator to this queue, in turn. |
133 |
< |
* |
134 |
< |
* @param c collection whose elements are to be added to this queue |
135 |
< |
* @return <tt>true</tt> if this queue changed as a result of the |
136 |
< |
* call. |
137 |
< |
* @throws NullPointerException if <tt>c</tt> or any element in <tt>c</tt> |
138 |
< |
* is <tt>null</tt> |
139 |
< |
* @throws IllegalStateException if any element cannot be added. |
140 |
< |
* |
124 |
> |
* queue. Attempts to addAll of a queue to itself result in |
125 |
> |
* {@code IllegalArgumentException}. Further, the behavior of |
126 |
> |
* this operation is undefined if the specified collection is |
127 |
> |
* modified while the operation is in progress. |
128 |
> |
* |
129 |
> |
* <p>This implementation iterates over the specified collection, |
130 |
> |
* and adds each element returned by the iterator to this |
131 |
> |
* queue, in turn. A runtime exception encountered while |
132 |
> |
* trying to add an element (including, in particular, a |
133 |
> |
* {@code null} element) may result in only some of the elements |
134 |
> |
* having been successfully added when the associated exception is |
135 |
> |
* thrown. |
136 |
> |
* |
137 |
> |
* @param c collection containing elements to be added to this queue |
138 |
> |
* @return {@code true} if this queue changed as a result of the call |
139 |
> |
* @throws ClassCastException if the class of an element of the specified |
140 |
> |
* collection prevents it from being added to this queue |
141 |
> |
* @throws NullPointerException if the specified collection contains a |
142 |
> |
* null element and this queue does not permit null elements, |
143 |
> |
* or if the specified collection is null |
144 |
> |
* @throws IllegalArgumentException if some property of an element of the |
145 |
> |
* specified collection prevents it from being added to this |
146 |
> |
* queue, or if the specified collection is this queue |
147 |
> |
* @throws IllegalStateException if not all the elements can be added at |
148 |
> |
* this time due to insertion restrictions |
149 |
> |
* @see #add(Object) |
150 |
|
*/ |
151 |
|
public boolean addAll(Collection<? extends E> c) { |
152 |
< |
return super.addAll(c); |
152 |
> |
if (c == null) |
153 |
> |
throw new NullPointerException(); |
154 |
> |
if (c == this) |
155 |
> |
throw new IllegalArgumentException(); |
156 |
> |
boolean modified = false; |
157 |
> |
for (E e : c) |
158 |
> |
if (add(e)) |
159 |
> |
modified = true; |
160 |
> |
return modified; |
161 |
|
} |
162 |
|
|
163 |
|
} |