1 |
dl |
1.2 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
dl |
1.24 |
* Expert Group and released to the public domain, as explained at |
4 |
jsr166 |
1.37 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.2 |
*/ |
6 |
|
|
|
7 |
tim |
1.1 |
package java.util; |
8 |
|
|
|
9 |
|
|
/** |
10 |
dl |
1.14 |
* This class provides skeletal implementations of some {@link Queue} |
11 |
|
|
* operations. The implementations in this class are appropriate when |
12 |
jsr166 |
1.38 |
* the base implementation does <em>not</em> allow {@code null} |
13 |
dl |
1.14 |
* elements. Methods {@link #add add}, {@link #remove remove}, and |
14 |
|
|
* {@link #element element} are based on {@link #offer offer}, {@link |
15 |
jsr166 |
1.35 |
* #poll poll}, and {@link #peek peek}, respectively, but throw |
16 |
jsr166 |
1.38 |
* exceptions instead of indicating failure via {@code false} or |
17 |
|
|
* {@code null} returns. |
18 |
dl |
1.14 |
* |
19 |
jsr166 |
1.38 |
* <p>A {@code Queue} implementation that extends this class must |
20 |
dl |
1.14 |
* minimally define a method {@link Queue#offer} which does not permit |
21 |
jsr166 |
1.38 |
* insertion of {@code null} elements, along with methods {@link |
22 |
jsr166 |
1.35 |
* 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 |
dl |
1.14 |
* |
27 |
dl |
1.25 |
* <p>This class is a member of the |
28 |
jsr166 |
1.34 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
29 |
dl |
1.25 |
* Java Collections Framework</a>. |
30 |
jsr166 |
1.26 |
* |
31 |
dl |
1.4 |
* @since 1.5 |
32 |
|
|
* @author Doug Lea |
33 |
dl |
1.21 |
* @param <E> the type of elements held in this collection |
34 |
tim |
1.1 |
*/ |
35 |
tim |
1.10 |
public abstract class AbstractQueue<E> |
36 |
|
|
extends AbstractCollection<E> |
37 |
dholmes |
1.9 |
implements Queue<E> { |
38 |
tim |
1.5 |
|
39 |
dl |
1.14 |
/** |
40 |
|
|
* Constructor for use by subclasses. |
41 |
|
|
*/ |
42 |
|
|
protected AbstractQueue() { |
43 |
|
|
} |
44 |
tim |
1.1 |
|
45 |
dholmes |
1.12 |
/** |
46 |
jsr166 |
1.28 |
* Inserts the specified element into this queue if it is possible to do so |
47 |
|
|
* immediately without violating capacity restrictions, returning |
48 |
jsr166 |
1.38 |
* {@code true} upon success and throwing an {@code IllegalStateException} |
49 |
jsr166 |
1.28 |
* if no space is currently available. |
50 |
|
|
* |
51 |
jsr166 |
1.38 |
* <p>This implementation returns {@code true} if {@code offer} succeeds, |
52 |
|
|
* else throws an {@code IllegalStateException}. |
53 |
jsr166 |
1.28 |
* |
54 |
|
|
* @param e the element to add |
55 |
jsr166 |
1.38 |
* @return {@code true} (as specified by {@link Collection#add}) |
56 |
jsr166 |
1.28 |
* @throws IllegalStateException if the element cannot be added at this |
57 |
|
|
* time due to capacity restrictions |
58 |
jsr166 |
1.29 |
* @throws ClassCastException if the class of the specified element |
59 |
|
|
* prevents it from being added to this queue |
60 |
jsr166 |
1.28 |
* @throws NullPointerException if the specified element is null and |
61 |
jsr166 |
1.33 |
* this queue does not permit null elements |
62 |
jsr166 |
1.28 |
* @throws IllegalArgumentException if some property of this element |
63 |
|
|
* prevents it from being added to this queue |
64 |
dholmes |
1.9 |
*/ |
65 |
jsr166 |
1.27 |
public boolean add(E e) { |
66 |
|
|
if (offer(e)) |
67 |
tim |
1.1 |
return true; |
68 |
|
|
else |
69 |
|
|
throw new IllegalStateException("Queue full"); |
70 |
|
|
} |
71 |
|
|
|
72 |
dholmes |
1.9 |
/** |
73 |
jsr166 |
1.28 |
* Retrieves and removes the head of this queue. This method differs |
74 |
jsr166 |
1.31 |
* from {@link #poll poll} only in that it throws an exception if this |
75 |
|
|
* queue is empty. |
76 |
jsr166 |
1.28 |
* |
77 |
jsr166 |
1.38 |
* <p>This implementation returns the result of {@code poll} |
78 |
dl |
1.14 |
* unless the queue is empty. |
79 |
dholmes |
1.9 |
* |
80 |
jsr166 |
1.28 |
* @return the head of this queue |
81 |
|
|
* @throws NoSuchElementException if this queue is empty |
82 |
dholmes |
1.9 |
*/ |
83 |
tim |
1.8 |
public E remove() { |
84 |
tim |
1.1 |
E x = poll(); |
85 |
|
|
if (x != null) |
86 |
|
|
return x; |
87 |
|
|
else |
88 |
|
|
throw new NoSuchElementException(); |
89 |
|
|
} |
90 |
|
|
|
91 |
dl |
1.14 |
/** |
92 |
jsr166 |
1.28 |
* Retrieves, but does not remove, the head of this queue. This method |
93 |
jsr166 |
1.31 |
* differs from {@link #peek peek} only in that it throws an exception if |
94 |
jsr166 |
1.28 |
* this queue is empty. |
95 |
|
|
* |
96 |
jsr166 |
1.38 |
* <p>This implementation returns the result of {@code peek} |
97 |
dl |
1.14 |
* unless the queue is empty. |
98 |
|
|
* |
99 |
jsr166 |
1.28 |
* @return the head of this queue |
100 |
|
|
* @throws NoSuchElementException if this queue is empty |
101 |
dl |
1.14 |
*/ |
102 |
tim |
1.8 |
public E element() { |
103 |
tim |
1.1 |
E x = peek(); |
104 |
|
|
if (x != null) |
105 |
|
|
return x; |
106 |
|
|
else |
107 |
|
|
throw new NoSuchElementException(); |
108 |
|
|
} |
109 |
|
|
|
110 |
dholmes |
1.9 |
/** |
111 |
jsr166 |
1.26 |
* Removes all of the elements from this queue. |
112 |
|
|
* The queue will be empty after this call returns. |
113 |
jsr166 |
1.28 |
* |
114 |
dholmes |
1.9 |
* <p>This implementation repeatedly invokes {@link #poll poll} until it |
115 |
jsr166 |
1.38 |
* returns {@code null}. |
116 |
dholmes |
1.9 |
*/ |
117 |
tim |
1.1 |
public void clear() { |
118 |
|
|
while (poll() != null) |
119 |
|
|
; |
120 |
|
|
} |
121 |
dl |
1.19 |
|
122 |
|
|
/** |
123 |
|
|
* Adds all of the elements in the specified collection to this |
124 |
|
|
* queue. Attempts to addAll of a queue to itself result in |
125 |
jsr166 |
1.38 |
* {@code IllegalArgumentException}. Further, the behavior of |
126 |
dl |
1.19 |
* 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 |
jsr166 |
1.26 |
* queue, in turn. A runtime exception encountered while |
132 |
dl |
1.19 |
* trying to add an element (including, in particular, a |
133 |
jsr166 |
1.38 |
* {@code null} element) may result in only some of the elements |
134 |
dl |
1.19 |
* having been successfully added when the associated exception is |
135 |
|
|
* thrown. |
136 |
|
|
* |
137 |
jsr166 |
1.30 |
* @param c collection containing elements to be added to this queue |
138 |
jsr166 |
1.38 |
* @return {@code true} if this queue changed as a result of the call |
139 |
jsr166 |
1.29 |
* @throws ClassCastException if the class of an element of the specified |
140 |
|
|
* collection prevents it from being added to this queue |
141 |
jsr166 |
1.28 |
* @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 |
jsr166 |
1.29 |
* specified collection prevents it from being added to this |
146 |
|
|
* queue, or if the specified collection is this queue |
147 |
jsr166 |
1.28 |
* @throws IllegalStateException if not all the elements can be added at |
148 |
|
|
* this time due to insertion restrictions |
149 |
dl |
1.19 |
* @see #add(Object) |
150 |
|
|
*/ |
151 |
|
|
public boolean addAll(Collection<? extends E> c) { |
152 |
|
|
if (c == null) |
153 |
|
|
throw new NullPointerException(); |
154 |
|
|
if (c == this) |
155 |
|
|
throw new IllegalArgumentException(); |
156 |
jozart |
1.23 |
boolean modified = false; |
157 |
jsr166 |
1.36 |
for (E e : c) |
158 |
|
|
if (add(e)) |
159 |
jozart |
1.23 |
modified = true; |
160 |
|
|
return modified; |
161 |
dl |
1.19 |
} |
162 |
|
|
|
163 |
dl |
1.14 |
} |