1 |
|
/* |
2 |
|
* Written by Doug Lea and Josh Bloch with assistance from members of |
3 |
|
* JCP JSR-166 Expert Group and released to the public domain, as explained |
4 |
< |
* at http://creativecommons.org/licenses/publicdomain |
4 |
> |
* at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
package java.util; |
9 |
|
/** |
10 |
|
* A linear collection that supports element insertion and removal at |
11 |
|
* both ends. The name <i>deque</i> is short for "double ended queue" |
12 |
< |
* and is usually pronounced "deck". Most <tt>Deque</tt> |
12 |
> |
* and is usually pronounced "deck". Most {@code Deque} |
13 |
|
* implementations place no fixed limits on the number of elements |
14 |
|
* they may contain, but this interface supports capacity-restricted |
15 |
|
* deques as well as those with no fixed size limit. |
18 |
|
* ends of the deque. Methods are provided to insert, remove, and |
19 |
|
* examine the element. Each of these methods exists in two forms: |
20 |
|
* one throws an exception if the operation fails, the other returns a |
21 |
< |
* special value (either <tt>null</tt> or <tt>false</tt>, depending on |
21 |
> |
* special value (either {@code null} or {@code false}, depending on |
22 |
|
* the operation). The latter form of the insert operation is |
23 |
|
* designed specifically for use with capacity-restricted |
24 |
< |
* <tt>Deque</tt> implementations; in most implementations, insert |
24 |
> |
* {@code Deque} implementations; in most implementations, insert |
25 |
|
* operations cannot fail. |
26 |
|
* |
27 |
|
* <p>The twelve methods described above are summarized in the |
28 |
|
* following table: |
29 |
|
* |
30 |
< |
* <p><table BORDER CELLPADDING=3 CELLSPACING=1> |
30 |
> |
* <p> |
31 |
> |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
32 |
|
* <tr> |
33 |
|
* <td></td> |
34 |
|
* <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td> |
37 |
|
* <tr> |
38 |
|
* <td></td> |
39 |
|
* <td ALIGN=CENTER><em>Throws exception</em></td> |
40 |
< |
* <td ALIGN=CENTER><em>Returns special value</em></td> |
40 |
> |
* <td ALIGN=CENTER><em>Special value</em></td> |
41 |
|
* <td ALIGN=CENTER><em>Throws exception</em></td> |
42 |
< |
* <td ALIGN=CENTER><em>Returns special value</em></td> |
42 |
> |
* <td ALIGN=CENTER><em>Special value</em></td> |
43 |
|
* </tr> |
44 |
|
* <tr> |
45 |
|
* <td><b>Insert</b></td> |
67 |
|
* <p>This interface extends the {@link Queue} interface. When a deque is |
68 |
|
* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are |
69 |
|
* added at the end of the deque and removed from the beginning. The methods |
70 |
< |
* inherited from the <tt>Queue</tt> interface are precisely equivalent to |
71 |
< |
* <tt>Deque</tt> methods as indicated in the following table: |
70 |
> |
* inherited from the {@code Queue} interface are precisely equivalent to |
71 |
> |
* {@code Deque} methods as indicated in the following table: |
72 |
|
* |
73 |
< |
* <p><table BORDER CELLPADDING=3 CELLSPACING=1> |
73 |
> |
* <p> |
74 |
> |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
75 |
|
* <tr> |
76 |
< |
* <td ALIGN=CENTER> <b><tt>Queue</tt> Method</b></td> |
77 |
< |
* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td> |
76 |
< |
* </tr> |
77 |
< |
* <tr> |
78 |
< |
* <td>{@link java.util.Queue#offer offer(e)}</td> |
79 |
< |
* <td>{@link #offerLast offerLast(e)}</td> |
76 |
> |
* <td ALIGN=CENTER> <b>{@code Queue} Method</b></td> |
77 |
> |
* <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td> |
78 |
|
* </tr> |
79 |
|
* <tr> |
80 |
|
* <td>{@link java.util.Queue#add add(e)}</td> |
81 |
|
* <td>{@link #addLast addLast(e)}</td> |
82 |
|
* </tr> |
83 |
|
* <tr> |
84 |
< |
* <td>{@link java.util.Queue#poll poll()}</td> |
85 |
< |
* <td>{@link #pollFirst pollFirst()}</td> |
84 |
> |
* <td>{@link java.util.Queue#offer offer(e)}</td> |
85 |
> |
* <td>{@link #offerLast offerLast(e)}</td> |
86 |
|
* </tr> |
87 |
|
* <tr> |
88 |
|
* <td>{@link java.util.Queue#remove remove()}</td> |
89 |
|
* <td>{@link #removeFirst removeFirst()}</td> |
90 |
|
* </tr> |
91 |
|
* <tr> |
92 |
< |
* <td>{@link java.util.Queue#peek peek()}</td> |
93 |
< |
* <td>{@link #peek peekFirst()}</td> |
92 |
> |
* <td>{@link java.util.Queue#poll poll()}</td> |
93 |
> |
* <td>{@link #pollFirst pollFirst()}</td> |
94 |
|
* </tr> |
95 |
|
* <tr> |
96 |
|
* <td>{@link java.util.Queue#element element()}</td> |
97 |
|
* <td>{@link #getFirst getFirst()}</td> |
98 |
|
* </tr> |
99 |
+ |
* <tr> |
100 |
+ |
* <td>{@link java.util.Queue#peek peek()}</td> |
101 |
+ |
* <td>{@link #peek peekFirst()}</td> |
102 |
+ |
* </tr> |
103 |
|
* </table> |
104 |
|
* |
105 |
|
* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This |
106 |
|
* interface should be used in preference to the legacy {@link Stack} class. |
107 |
|
* When a deque is used as a stack, elements are pushed and popped from the |
108 |
|
* beginning of the deque. Stack methods are precisely equivalent to |
109 |
< |
* <tt>Deque</tt> methods as indicated in the table below: |
109 |
> |
* {@code Deque} methods as indicated in the table below: |
110 |
|
* |
111 |
< |
* <p><table BORDER CELLPADDING=3 CELLSPACING=1> |
111 |
> |
* <p> |
112 |
> |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
113 |
|
* <tr> |
114 |
|
* <td ALIGN=CENTER> <b>Stack Method</b></td> |
115 |
< |
* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td> |
115 |
> |
* <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td> |
116 |
|
* </tr> |
117 |
|
* <tr> |
118 |
|
* <td>{@link #push push(e)}</td> |
134 |
|
* |
135 |
|
* <p>This interface provides two methods to remove interior |
136 |
|
* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and |
137 |
< |
* {@link #removeLastOccurrence removeLastOccurrence}. Unlike the |
138 |
< |
* {@link List} interface, this interface does not provide support for |
139 |
< |
* indexed access to elements. |
137 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}. |
138 |
> |
* |
139 |
> |
* <p>Unlike the {@link List} interface, this interface does not |
140 |
> |
* provide support for indexed access to elements. |
141 |
|
* |
142 |
< |
* <p>While <tt>Deque</tt> implementations are not strictly required |
142 |
> |
* <p>While {@code Deque} implementations are not strictly required |
143 |
|
* to prohibit the insertion of null elements, they are strongly |
144 |
< |
* encouraged to do so. Users of any <tt>Deque</tt> implementations |
144 |
> |
* encouraged to do so. Users of any {@code Deque} implementations |
145 |
|
* that do allow null elements are strongly encouraged <i>not</i> to |
146 |
|
* take advantage of the ability to insert nulls. This is so because |
147 |
< |
* <tt>null</tt> is used as a special return value by various methods |
147 |
> |
* {@code null} is used as a special return value by various methods |
148 |
|
* to indicated that the deque is empty. |
149 |
|
* |
150 |
< |
* <p><tt>Deque</tt> implementations generally do not define |
151 |
< |
* element-based versions of the <tt>equals</tt> and <tt>hashCode</tt> |
150 |
> |
* <p>{@code Deque} implementations generally do not define |
151 |
> |
* element-based versions of the {@code equals} and {@code hashCode} |
152 |
|
* methods, but instead inherit the identity-based versions from class |
153 |
< |
* <tt>Object</tt>. |
153 |
> |
* {@code Object}. |
154 |
|
* |
155 |
|
* <p>This interface is a member of the <a |
156 |
< |
* href="{@docRoot}/../guide/collections/index.html"> Java Collections |
156 |
> |
* href="{@docRoot}/../technotes/guides/collections/index.html"> Java Collections |
157 |
|
* Framework</a>. |
158 |
|
* |
159 |
|
* @author Doug Lea |
161 |
|
* @since 1.6 |
162 |
|
* @param <E> the type of elements held in this collection |
163 |
|
*/ |
160 |
– |
|
164 |
|
public interface Deque<E> extends Queue<E> { |
165 |
|
/** |
166 |
+ |
* Inserts the specified element at the front of this deque if it is |
167 |
+ |
* possible to do so immediately without violating capacity restrictions. |
168 |
+ |
* When using a capacity-restricted deque, it is generally preferable to |
169 |
+ |
* use method {@link #offerFirst}. |
170 |
+ |
* |
171 |
+ |
* @param e the element to add |
172 |
+ |
* @throws IllegalStateException if the element cannot be added at this |
173 |
+ |
* time due to capacity restrictions |
174 |
+ |
* @throws ClassCastException if the class of the specified element |
175 |
+ |
* prevents it from being added to this deque |
176 |
+ |
* @throws NullPointerException if the specified element is null and this |
177 |
+ |
* deque does not permit null elements |
178 |
+ |
* @throws IllegalArgumentException if some property of the specified |
179 |
+ |
* element prevents it from being added to this deque |
180 |
+ |
*/ |
181 |
+ |
void addFirst(E e); |
182 |
+ |
|
183 |
+ |
/** |
184 |
+ |
* Inserts the specified element at the end of this deque if it is |
185 |
+ |
* possible to do so immediately without violating capacity restrictions. |
186 |
+ |
* When using a capacity-restricted deque, it is generally preferable to |
187 |
+ |
* use method {@link #offerLast}. |
188 |
+ |
* |
189 |
+ |
* <p>This method is equivalent to {@link #add}. |
190 |
+ |
* |
191 |
+ |
* @param e the element to add |
192 |
+ |
* @throws IllegalStateException if the element cannot be added at this |
193 |
+ |
* time due to capacity restrictions |
194 |
+ |
* @throws ClassCastException if the class of the specified element |
195 |
+ |
* prevents it from being added to this deque |
196 |
+ |
* @throws NullPointerException if the specified element is null and this |
197 |
+ |
* deque does not permit null elements |
198 |
+ |
* @throws IllegalArgumentException if some property of the specified |
199 |
+ |
* element prevents it from being added to this deque |
200 |
+ |
*/ |
201 |
+ |
void addLast(E e); |
202 |
+ |
|
203 |
+ |
/** |
204 |
|
* Inserts the specified element at the front of this deque unless it would |
205 |
|
* violate capacity restrictions. When using a capacity-restricted deque, |
206 |
< |
* this method is generally preferable to method <tt>addFirst</tt>, which |
207 |
< |
* can fail to insert an element only by throwing an exception. |
206 |
> |
* this method is generally preferable to the {@link #addFirst} method, |
207 |
> |
* which can fail to insert an element only by throwing an exception. |
208 |
|
* |
209 |
< |
* @param e the element to insert |
210 |
< |
* @return <tt>true</tt> if it was possible to insert the element, |
211 |
< |
* else <tt>false</tt> |
209 |
> |
* @param e the element to add |
210 |
> |
* @return {@code true} if the element was added to this deque, else |
211 |
> |
* {@code false} |
212 |
> |
* @throws ClassCastException if the class of the specified element |
213 |
> |
* prevents it from being added to this deque |
214 |
|
* @throws NullPointerException if the specified element is null and this |
215 |
< |
* deque does not permit null elements |
215 |
> |
* deque does not permit null elements |
216 |
> |
* @throws IllegalArgumentException if some property of the specified |
217 |
> |
* element prevents it from being added to this deque |
218 |
|
*/ |
219 |
|
boolean offerFirst(E e); |
220 |
|
|
221 |
|
/** |
222 |
|
* Inserts the specified element at the end of this deque unless it would |
223 |
|
* violate capacity restrictions. When using a capacity-restricted deque, |
224 |
< |
* this method is generally preferable to method <tt>addLast</tt> which |
225 |
< |
* can fail to insert an element only by throwing an exception. |
224 |
> |
* this method is generally preferable to the {@link #addLast} method, |
225 |
> |
* which can fail to insert an element only by throwing an exception. |
226 |
|
* |
227 |
< |
* @param e the element to insert |
228 |
< |
* @return <tt>true</tt> if it was possible to insert the element, |
229 |
< |
* else <tt>false</tt> |
227 |
> |
* @param e the element to add |
228 |
> |
* @return {@code true} if the element was added to this deque, else |
229 |
> |
* {@code false} |
230 |
> |
* @throws ClassCastException if the class of the specified element |
231 |
> |
* prevents it from being added to this deque |
232 |
|
* @throws NullPointerException if the specified element is null and this |
233 |
< |
* deque does not permit null elements |
233 |
> |
* deque does not permit null elements |
234 |
> |
* @throws IllegalArgumentException if some property of the specified |
235 |
> |
* element prevents it from being added to this deque |
236 |
|
*/ |
237 |
|
boolean offerLast(E e); |
238 |
|
|
239 |
|
/** |
240 |
< |
* Inserts the specified element at the front of this deque unless it |
241 |
< |
* would violate capacity restrictions. |
240 |
> |
* Retrieves and removes the first element of this deque. This method |
241 |
> |
* differs from {@link #pollFirst pollFirst} only in that it throws an |
242 |
> |
* exception if this deque is empty. |
243 |
|
* |
244 |
< |
* @param e the element to insert |
245 |
< |
* @throws IllegalStateException if it was not possible to insert |
196 |
< |
* the element due to capacity restrictions |
197 |
< |
* @throws NullPointerException if the specified element is null and this |
198 |
< |
* deque does not permit null elements |
244 |
> |
* @return the head of this deque |
245 |
> |
* @throws NoSuchElementException if this deque is empty |
246 |
|
*/ |
247 |
< |
void addFirst(E e); |
247 |
> |
E removeFirst(); |
248 |
|
|
249 |
|
/** |
250 |
< |
* Inserts the specified element at the end of this deque unless it would |
251 |
< |
* violate capacity restrictions. |
250 |
> |
* Retrieves and removes the last element of this deque. This method |
251 |
> |
* differs from {@link #pollLast pollLast} only in that it throws an |
252 |
> |
* exception if this deque is empty. |
253 |
|
* |
254 |
< |
* @param e the element to insert |
255 |
< |
* @throws IllegalStateException if it was not possible to insert |
208 |
< |
* the element due to capacity restrictions |
209 |
< |
* @throws NullPointerException if the specified element is null and this |
210 |
< |
* deque does not permit null elements |
254 |
> |
* @return the tail of this deque |
255 |
> |
* @throws NoSuchElementException if this deque is empty |
256 |
|
*/ |
257 |
< |
void addLast(E e); |
257 |
> |
E removeLast(); |
258 |
|
|
259 |
|
/** |
260 |
< |
* Retrieves and removes the first element of this deque, or |
261 |
< |
* <tt>null</tt> if this deque is empty. |
260 |
> |
* Retrieves and removes the first element of this deque, |
261 |
> |
* or returns {@code null} if this deque is empty. |
262 |
|
* |
263 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
219 |
< |
* this deque is empty |
263 |
> |
* @return the head of this deque, or {@code null} if this deque is empty |
264 |
|
*/ |
265 |
|
E pollFirst(); |
266 |
|
|
267 |
|
/** |
268 |
< |
* Retrieves and removes the last element of this deque, or |
269 |
< |
* <tt>null</tt> if this deque is empty. |
268 |
> |
* Retrieves and removes the last element of this deque, |
269 |
> |
* or returns {@code null} if this deque is empty. |
270 |
|
* |
271 |
< |
* @return the last element of this deque, or <tt>null</tt> if |
228 |
< |
* this deque is empty |
271 |
> |
* @return the tail of this deque, or {@code null} if this deque is empty |
272 |
|
*/ |
273 |
|
E pollLast(); |
274 |
|
|
275 |
|
/** |
276 |
< |
* Retrieves and removes the first element of this deque. This method |
277 |
< |
* differs from the <tt>pollFirst</tt> method only in that it throws an |
278 |
< |
* exception if this deque is empty. |
276 |
> |
* Retrieves, but does not remove, the first element of this deque. |
277 |
> |
* |
278 |
> |
* This method differs from {@link #peekFirst peekFirst} only in that it |
279 |
> |
* throws an exception if this deque is empty. |
280 |
|
* |
281 |
< |
* @return the first element of this deque |
281 |
> |
* @return the head of this deque |
282 |
|
* @throws NoSuchElementException if this deque is empty |
283 |
|
*/ |
284 |
< |
E removeFirst(); |
284 |
> |
E getFirst(); |
285 |
|
|
286 |
|
/** |
287 |
< |
* Retrieves and removes the last element of this deque. This method |
288 |
< |
* differs from the <tt>pollLast</tt> method only in that it throws an |
289 |
< |
* exception if this deque is empty. |
287 |
> |
* Retrieves, but does not remove, the last element of this deque. |
288 |
> |
* This method differs from {@link #peekLast peekLast} only in that it |
289 |
> |
* throws an exception if this deque is empty. |
290 |
|
* |
291 |
< |
* @return the last element of this deque |
291 |
> |
* @return the tail of this deque |
292 |
|
* @throws NoSuchElementException if this deque is empty |
293 |
|
*/ |
294 |
< |
E removeLast(); |
294 |
> |
E getLast(); |
295 |
|
|
296 |
|
/** |
297 |
|
* Retrieves, but does not remove, the first element of this deque, |
298 |
< |
* returning <tt>null</tt> if this deque is empty. |
298 |
> |
* or returns {@code null} if this deque is empty. |
299 |
|
* |
300 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
257 |
< |
* this deque is empty |
300 |
> |
* @return the head of this deque, or {@code null} if this deque is empty |
301 |
|
*/ |
302 |
|
E peekFirst(); |
303 |
|
|
304 |
|
/** |
305 |
|
* Retrieves, but does not remove, the last element of this deque, |
306 |
< |
* returning <tt>null</tt> if this deque is empty. |
306 |
> |
* or returns {@code null} if this deque is empty. |
307 |
|
* |
308 |
< |
* @return the last element of this deque, or <tt>null</tt> if this deque |
266 |
< |
* is empty |
308 |
> |
* @return the tail of this deque, or {@code null} if this deque is empty |
309 |
|
*/ |
310 |
|
E peekLast(); |
311 |
|
|
312 |
|
/** |
313 |
< |
* Retrieves, but does not remove, the first element of this |
314 |
< |
* deque. This method differs from the <tt>peekFirst</tt> method only |
315 |
< |
* in that it throws an exception if this deque is empty. |
316 |
< |
* |
317 |
< |
* @return the first element of this deque |
318 |
< |
* @throws NoSuchElementException if this deque is empty |
319 |
< |
*/ |
278 |
< |
E getFirst(); |
279 |
< |
|
280 |
< |
/** |
281 |
< |
* Retrieves, but does not remove, the last element of this |
282 |
< |
* deque. This method differs from the <tt>peekLast</tt> method only |
283 |
< |
* in that it throws an exception if this deque is empty. |
284 |
< |
* |
285 |
< |
* @return the last element of this deque |
286 |
< |
* @throws NoSuchElementException if this deque is empty |
287 |
< |
*/ |
288 |
< |
E getLast(); |
289 |
< |
|
290 |
< |
/** |
291 |
< |
* Removes the first occurrence of the specified element in this |
292 |
< |
* deque. If the deque does not contain the element, it is |
293 |
< |
* unchanged. More formally, removes the first element <tt>e</tt> |
294 |
< |
* such that <tt>(o==null ? e==null : o.equals(e))</tt> (if |
295 |
< |
* such an element exists). |
313 |
> |
* Removes the first occurrence of the specified element from this deque. |
314 |
> |
* If the deque does not contain the element, it is unchanged. |
315 |
> |
* More formally, removes the first element {@code e} such that |
316 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt> |
317 |
> |
* (if such an element exists). |
318 |
> |
* Returns {@code true} if this deque contained the specified element |
319 |
> |
* (or equivalently, if this deque changed as a result of the call). |
320 |
|
* |
321 |
|
* @param o element to be removed from this deque, if present |
322 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
322 |
> |
* @return {@code true} if an element was removed as a result of this call |
323 |
> |
* @throws ClassCastException if the class of the specified element |
324 |
> |
* is incompatible with this deque (optional) |
325 |
|
* @throws NullPointerException if the specified element is null and this |
326 |
< |
* deque does not permit null elements |
326 |
> |
* deque does not permit null elements (optional) |
327 |
|
*/ |
328 |
|
boolean removeFirstOccurrence(Object o); |
329 |
|
|
330 |
|
/** |
331 |
< |
* Removes the last occurrence of the specified element in this |
332 |
< |
* deque. If the deque does not contain the element, it is |
333 |
< |
* unchanged. More formally, removes the last element <tt>e</tt> |
334 |
< |
* such that <tt>(o==null ? e==null : o.equals(e))</tt> (if |
335 |
< |
* such an element exists). |
331 |
> |
* Removes the last occurrence of the specified element from this deque. |
332 |
> |
* If the deque does not contain the element, it is unchanged. |
333 |
> |
* More formally, removes the last element {@code e} such that |
334 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt> |
335 |
> |
* (if such an element exists). |
336 |
> |
* Returns {@code true} if this deque contained the specified element |
337 |
> |
* (or equivalently, if this deque changed as a result of the call). |
338 |
|
* |
339 |
|
* @param o element to be removed from this deque, if present |
340 |
< |
* @return <tt>true</tt> if the deque contained the specified element |
340 |
> |
* @return {@code true} if an element was removed as a result of this call |
341 |
> |
* @throws ClassCastException if the class of the specified element |
342 |
> |
* is incompatible with this deque (optional) |
343 |
|
* @throws NullPointerException if the specified element is null and this |
344 |
< |
* deque does not permit null elements |
344 |
> |
* deque does not permit null elements (optional) |
345 |
|
*/ |
346 |
|
boolean removeLastOccurrence(Object o); |
347 |
|
|
318 |
– |
|
348 |
|
// *** Queue methods *** |
349 |
|
|
350 |
|
/** |
351 |
|
* Inserts the specified element into the queue represented by this deque |
352 |
< |
* unless it would violate capacity restrictions. In other words, inserts |
353 |
< |
* the specified element at the end of this deque. When using a |
354 |
< |
* capacity-restricted deque, this method is generally preferable to the |
355 |
< |
* {@link #add} method, which can fail to insert an element only by |
356 |
< |
* throwing an exception. |
357 |
< |
* |
329 |
< |
* <p>This method is equivalent to {@link #offerLast}. |
330 |
< |
* |
331 |
< |
* @param e the element to insert |
332 |
< |
* @return <tt>true</tt> if it was possible to insert the element, |
333 |
< |
* else <tt>false</tt> |
334 |
< |
* @throws NullPointerException if the specified element is null and this |
335 |
< |
* deque does not permit null elements |
336 |
< |
*/ |
337 |
< |
boolean offer(E e); |
338 |
< |
|
339 |
< |
/** |
340 |
< |
* Inserts the specified element into the queue represented by this |
341 |
< |
* deque unless it would violate capacity restrictions. In other words, |
342 |
< |
* inserts the specified element as the last element of this deque. |
352 |
> |
* (in other words, at the tail of this deque) if it is possible to do so |
353 |
> |
* immediately without violating capacity restrictions, returning |
354 |
> |
* {@code true} upon success and throwing an |
355 |
> |
* {@code IllegalStateException} if no space is currently available. |
356 |
> |
* When using a capacity-restricted deque, it is generally preferable to |
357 |
> |
* use {@link #offer(Object) offer}. |
358 |
|
* |
359 |
|
* <p>This method is equivalent to {@link #addLast}. |
360 |
|
* |
361 |
< |
* @param e the element to insert |
362 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
363 |
< |
* @throws IllegalStateException if it was not possible to insert |
364 |
< |
* the element due to capacity restrictions |
361 |
> |
* @param e the element to add |
362 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
363 |
> |
* @throws IllegalStateException if the element cannot be added at this |
364 |
> |
* time due to capacity restrictions |
365 |
> |
* @throws ClassCastException if the class of the specified element |
366 |
> |
* prevents it from being added to this deque |
367 |
|
* @throws NullPointerException if the specified element is null and this |
368 |
< |
* deque does not permit null elements |
368 |
> |
* deque does not permit null elements |
369 |
> |
* @throws IllegalArgumentException if some property of the specified |
370 |
> |
* element prevents it from being added to this deque |
371 |
|
*/ |
372 |
|
boolean add(E e); |
373 |
|
|
374 |
|
/** |
375 |
< |
* Retrieves and removes the head of the queue represented by |
376 |
< |
* this deque, or <tt>null</tt> if this deque is empty. In other words, |
377 |
< |
* retrieves and removes the first element of this deque, or <tt>null</tt> |
378 |
< |
* if this deque is empty. |
375 |
> |
* Inserts the specified element into the queue represented by this deque |
376 |
> |
* (in other words, at the tail of this deque) if it is possible to do so |
377 |
> |
* immediately without violating capacity restrictions, returning |
378 |
> |
* {@code true} upon success and {@code false} if no space is currently |
379 |
> |
* available. When using a capacity-restricted deque, this method is |
380 |
> |
* generally preferable to the {@link #add} method, which can fail to |
381 |
> |
* insert an element only by throwing an exception. |
382 |
|
* |
383 |
< |
* <p>This method is equivalent to {@link #pollFirst()}. |
383 |
> |
* <p>This method is equivalent to {@link #offerLast}. |
384 |
|
* |
385 |
< |
* @return the first element of this deque, or <tt>null</tt> if |
386 |
< |
* this deque is empty |
385 |
> |
* @param e the element to add |
386 |
> |
* @return {@code true} if the element was added to this deque, else |
387 |
> |
* {@code false} |
388 |
> |
* @throws ClassCastException if the class of the specified element |
389 |
> |
* prevents it from being added to this deque |
390 |
> |
* @throws NullPointerException if the specified element is null and this |
391 |
> |
* deque does not permit null elements |
392 |
> |
* @throws IllegalArgumentException if some property of the specified |
393 |
> |
* element prevents it from being added to this deque |
394 |
|
*/ |
395 |
< |
E poll(); |
395 |
> |
boolean offer(E e); |
396 |
|
|
397 |
|
/** |
398 |
< |
* Retrieves and removes the head of the queue represented by this deque. |
399 |
< |
* This method differs from the <tt>poll</tt> method only in that it |
400 |
< |
* throws an exception if this deque is empty. |
398 |
> |
* Retrieves and removes the head of the queue represented by this deque |
399 |
> |
* (in other words, the first element of this deque). |
400 |
> |
* This method differs from {@link #poll poll} only in that it throws an |
401 |
> |
* exception if this deque is empty. |
402 |
|
* |
403 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
404 |
|
* |
408 |
|
E remove(); |
409 |
|
|
410 |
|
/** |
411 |
< |
* Retrieves, but does not remove, the head of the queue represented by |
412 |
< |
* this deque, returning <tt>null</tt> if this deque is empty. |
411 |
> |
* Retrieves and removes the head of the queue represented by this deque |
412 |
> |
* (in other words, the first element of this deque), or returns |
413 |
> |
* {@code null} if this deque is empty. |
414 |
|
* |
415 |
< |
* <p>This method is equivalent to {@link #peekFirst()}. |
415 |
> |
* <p>This method is equivalent to {@link #pollFirst()}. |
416 |
|
* |
417 |
< |
* @return the head of the queue represented by this deque, or |
418 |
< |
* <tt>null</tt> if this deque is empty |
417 |
> |
* @return the first element of this deque, or {@code null} if |
418 |
> |
* this deque is empty |
419 |
|
*/ |
420 |
< |
E peek(); |
420 |
> |
E poll(); |
421 |
|
|
422 |
|
/** |
423 |
|
* Retrieves, but does not remove, the head of the queue represented by |
424 |
< |
* this deque. This method differs from the <tt>peek</tt> method only in |
425 |
< |
* that it throws an exception if this deque is empty. |
424 |
> |
* this deque (in other words, the first element of this deque). |
425 |
> |
* This method differs from {@link #peek peek} only in that it throws an |
426 |
> |
* exception if this deque is empty. |
427 |
|
* |
428 |
|
* <p>This method is equivalent to {@link #getFirst()}. |
429 |
|
* |
432 |
|
*/ |
433 |
|
E element(); |
434 |
|
|
435 |
+ |
/** |
436 |
+ |
* Retrieves, but does not remove, the head of the queue represented by |
437 |
+ |
* this deque (in other words, the first element of this deque), or |
438 |
+ |
* returns {@code null} if this deque is empty. |
439 |
+ |
* |
440 |
+ |
* <p>This method is equivalent to {@link #peekFirst()}. |
441 |
+ |
* |
442 |
+ |
* @return the head of the queue represented by this deque, or |
443 |
+ |
* {@code null} if this deque is empty |
444 |
+ |
*/ |
445 |
+ |
E peek(); |
446 |
+ |
|
447 |
|
|
448 |
|
// *** Stack methods *** |
449 |
|
|
450 |
|
/** |
451 |
< |
* Pushes an element onto the stack represented by this deque. In other |
452 |
< |
* words, inserts the element at the front of this deque unless it would |
453 |
< |
* violate capacity restrictions. |
451 |
> |
* Pushes an element onto the stack represented by this deque (in other |
452 |
> |
* words, at the head of this deque) if it is possible to do so |
453 |
> |
* immediately without violating capacity restrictions, returning |
454 |
> |
* {@code true} upon success and throwing an |
455 |
> |
* {@code IllegalStateException} if no space is currently available. |
456 |
|
* |
457 |
|
* <p>This method is equivalent to {@link #addFirst}. |
458 |
|
* |
459 |
|
* @param e the element to push |
460 |
< |
* @throws IllegalStateException if it was not possible to insert |
461 |
< |
* the element due to capacity restrictions |
460 |
> |
* @throws IllegalStateException if the element cannot be added at this |
461 |
> |
* time due to capacity restrictions |
462 |
> |
* @throws ClassCastException if the class of the specified element |
463 |
> |
* prevents it from being added to this deque |
464 |
|
* @throws NullPointerException if the specified element is null and this |
465 |
< |
* deque does not permit null elements |
465 |
> |
* deque does not permit null elements |
466 |
> |
* @throws IllegalArgumentException if some property of the specified |
467 |
> |
* element prevents it from being added to this deque |
468 |
|
*/ |
469 |
|
void push(E e); |
470 |
|
|
475 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
476 |
|
* |
477 |
|
* @return the element at the front of this deque (which is the top |
478 |
< |
* of the stack represented by this deque) |
478 |
> |
* of the stack represented by this deque) |
479 |
|
* @throws NoSuchElementException if this deque is empty |
480 |
|
*/ |
481 |
|
E pop(); |
482 |
|
|
483 |
|
|
484 |
< |
// *** Collection Method *** |
484 |
> |
// *** Collection methods *** |
485 |
|
|
486 |
|
/** |
487 |
< |
* Returns an iterator over the elements in this deque. The elements |
488 |
< |
* will be ordered from first (head) to last (tail). |
487 |
> |
* Removes the first occurrence of the specified element from this deque. |
488 |
> |
* If the deque does not contain the element, it is unchanged. |
489 |
> |
* More formally, removes the first element {@code e} such that |
490 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt> |
491 |
> |
* (if such an element exists). |
492 |
> |
* Returns {@code true} if this deque contained the specified element |
493 |
> |
* (or equivalently, if this deque changed as a result of the call). |
494 |
|
* |
495 |
< |
* @return an <tt>Iterator</tt> over the elements in this deque |
495 |
> |
* <p>This method is equivalent to {@link #removeFirstOccurrence}. |
496 |
> |
* |
497 |
> |
* @param o element to be removed from this deque, if present |
498 |
> |
* @return {@code true} if an element was removed as a result of this call |
499 |
> |
* @throws ClassCastException if the class of the specified element |
500 |
> |
* is incompatible with this deque (optional) |
501 |
> |
* @throws NullPointerException if the specified element is null and this |
502 |
> |
* deque does not permit null elements (optional) |
503 |
> |
*/ |
504 |
> |
boolean remove(Object o); |
505 |
> |
|
506 |
> |
/** |
507 |
> |
* Returns {@code true} if this deque contains the specified element. |
508 |
> |
* More formally, returns {@code true} if and only if this deque contains |
509 |
> |
* at least one element {@code e} such that |
510 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
511 |
> |
* |
512 |
> |
* @param o element whose presence in this deque is to be tested |
513 |
> |
* @return {@code true} if this deque contains the specified element |
514 |
> |
* @throws ClassCastException if the type of the specified element |
515 |
> |
* is incompatible with this deque (optional) |
516 |
> |
* @throws NullPointerException if the specified element is null and this |
517 |
> |
* deque does not permit null elements (optional) |
518 |
> |
*/ |
519 |
> |
boolean contains(Object o); |
520 |
> |
|
521 |
> |
/** |
522 |
> |
* Returns the number of elements in this deque. |
523 |
> |
* |
524 |
> |
* @return the number of elements in this deque |
525 |
> |
*/ |
526 |
> |
public int size(); |
527 |
> |
|
528 |
> |
/** |
529 |
> |
* Returns an iterator over the elements in this deque in proper sequence. |
530 |
> |
* The elements will be returned in order from first (head) to last (tail). |
531 |
> |
* |
532 |
> |
* @return an iterator over the elements in this deque in proper sequence |
533 |
|
*/ |
534 |
|
Iterator<E> iterator(); |
535 |
+ |
|
536 |
+ |
/** |
537 |
+ |
* Returns an iterator over the elements in this deque in reverse |
538 |
+ |
* sequential order. The elements will be returned in order from |
539 |
+ |
* last (tail) to first (head). |
540 |
+ |
* |
541 |
+ |
* @return an iterator over the elements in this deque in reverse |
542 |
+ |
* sequence |
543 |
+ |
*/ |
544 |
+ |
Iterator<E> descendingIterator(); |
545 |
+ |
|
546 |
|
} |