1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
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.concurrent; |
8 |
|
9 |
import java.util.Deque; |
10 |
import java.util.Iterator; |
11 |
import java.util.NoSuchElementException; |
12 |
|
13 |
/** |
14 |
* A {@link Deque} that additionally supports blocking operations that wait |
15 |
* for the deque to become non-empty when retrieving an element, and wait for |
16 |
* space to become available in the deque when storing an element. |
17 |
* |
18 |
* <p>{@code BlockingDeque} methods come in four forms, with different ways |
19 |
* of handling operations that cannot be satisfied immediately, but may be |
20 |
* satisfied at some point in the future: |
21 |
* one throws an exception, the second returns a special value (either |
22 |
* {@code null} or {@code false}, depending on the operation), the third |
23 |
* blocks the current thread indefinitely until the operation can succeed, |
24 |
* and the fourth blocks for only a given maximum time limit before giving |
25 |
* up. These methods are summarized in the following table: |
26 |
* |
27 |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
28 |
* <caption>Summary of BlockingDeque methods</caption> |
29 |
* <tr> |
30 |
* <td ALIGN=CENTER COLSPAN = 5> <b>First Element (Head)</b></td> |
31 |
* </tr> |
32 |
* <tr> |
33 |
* <td></td> |
34 |
* <td ALIGN=CENTER><em>Throws exception</em></td> |
35 |
* <td ALIGN=CENTER><em>Special value</em></td> |
36 |
* <td ALIGN=CENTER><em>Blocks</em></td> |
37 |
* <td ALIGN=CENTER><em>Times out</em></td> |
38 |
* </tr> |
39 |
* <tr> |
40 |
* <td><b>Insert</b></td> |
41 |
* <td>{@link #addFirst addFirst(e)}</td> |
42 |
* <td>{@link #offerFirst(Object) offerFirst(e)}</td> |
43 |
* <td>{@link #putFirst putFirst(e)}</td> |
44 |
* <td>{@link #offerFirst(Object, long, TimeUnit) offerFirst(e, time, unit)}</td> |
45 |
* </tr> |
46 |
* <tr> |
47 |
* <td><b>Remove</b></td> |
48 |
* <td>{@link #removeFirst removeFirst()}</td> |
49 |
* <td>{@link #pollFirst pollFirst()}</td> |
50 |
* <td>{@link #takeFirst takeFirst()}</td> |
51 |
* <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td> |
52 |
* </tr> |
53 |
* <tr> |
54 |
* <td><b>Examine</b></td> |
55 |
* <td>{@link #getFirst getFirst()}</td> |
56 |
* <td>{@link #peekFirst peekFirst()}</td> |
57 |
* <td><em>not applicable</em></td> |
58 |
* <td><em>not applicable</em></td> |
59 |
* </tr> |
60 |
* <tr> |
61 |
* <td ALIGN=CENTER COLSPAN = 5> <b>Last Element (Tail)</b></td> |
62 |
* </tr> |
63 |
* <tr> |
64 |
* <td></td> |
65 |
* <td ALIGN=CENTER><em>Throws exception</em></td> |
66 |
* <td ALIGN=CENTER><em>Special value</em></td> |
67 |
* <td ALIGN=CENTER><em>Blocks</em></td> |
68 |
* <td ALIGN=CENTER><em>Times out</em></td> |
69 |
* </tr> |
70 |
* <tr> |
71 |
* <td><b>Insert</b></td> |
72 |
* <td>{@link #addLast addLast(e)}</td> |
73 |
* <td>{@link #offerLast(Object) offerLast(e)}</td> |
74 |
* <td>{@link #putLast putLast(e)}</td> |
75 |
* <td>{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)}</td> |
76 |
* </tr> |
77 |
* <tr> |
78 |
* <td><b>Remove</b></td> |
79 |
* <td>{@link #removeLast() removeLast()}</td> |
80 |
* <td>{@link #pollLast() pollLast()}</td> |
81 |
* <td>{@link #takeLast takeLast()}</td> |
82 |
* <td>{@link #pollLast(long, TimeUnit) pollLast(time, unit)}</td> |
83 |
* </tr> |
84 |
* <tr> |
85 |
* <td><b>Examine</b></td> |
86 |
* <td>{@link #getLast getLast()}</td> |
87 |
* <td>{@link #peekLast peekLast()}</td> |
88 |
* <td><em>not applicable</em></td> |
89 |
* <td><em>not applicable</em></td> |
90 |
* </tr> |
91 |
* </table> |
92 |
* |
93 |
* <p>Like any {@link BlockingQueue}, a {@code BlockingDeque} is thread safe, |
94 |
* does not permit null elements, and may (or may not) be |
95 |
* capacity-constrained. |
96 |
* |
97 |
* <p>A {@code BlockingDeque} implementation may be used directly as a FIFO |
98 |
* {@code BlockingQueue}. The methods inherited from the |
99 |
* {@code BlockingQueue} interface are precisely equivalent to |
100 |
* {@code BlockingDeque} methods as indicated in the following table: |
101 |
* |
102 |
* <table BORDER CELLPADDING=3 CELLSPACING=1> |
103 |
* <caption>Comparison of BlockingQueue and BlockingDeque methods</caption> |
104 |
* <tr> |
105 |
* <td ALIGN=CENTER> <b>{@code BlockingQueue} Method</b></td> |
106 |
* <td ALIGN=CENTER> <b>Equivalent {@code BlockingDeque} Method</b></td> |
107 |
* </tr> |
108 |
* <tr> |
109 |
* <td ALIGN=CENTER COLSPAN = 2> <b>Insert</b></td> |
110 |
* </tr> |
111 |
* <tr> |
112 |
* <td>{@link #add(Object) add(e)}</td> |
113 |
* <td>{@link #addLast(Object) addLast(e)}</td> |
114 |
* </tr> |
115 |
* <tr> |
116 |
* <td>{@link #offer(Object) offer(e)}</td> |
117 |
* <td>{@link #offerLast(Object) offerLast(e)}</td> |
118 |
* </tr> |
119 |
* <tr> |
120 |
* <td>{@link #put(Object) put(e)}</td> |
121 |
* <td>{@link #putLast(Object) putLast(e)}</td> |
122 |
* </tr> |
123 |
* <tr> |
124 |
* <td>{@link #offer(Object, long, TimeUnit) offer(e, time, unit)}</td> |
125 |
* <td>{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)}</td> |
126 |
* </tr> |
127 |
* <tr> |
128 |
* <td ALIGN=CENTER COLSPAN = 2> <b>Remove</b></td> |
129 |
* </tr> |
130 |
* <tr> |
131 |
* <td>{@link #remove() remove()}</td> |
132 |
* <td>{@link #removeFirst() removeFirst()}</td> |
133 |
* </tr> |
134 |
* <tr> |
135 |
* <td>{@link #poll() poll()}</td> |
136 |
* <td>{@link #pollFirst() pollFirst()}</td> |
137 |
* </tr> |
138 |
* <tr> |
139 |
* <td>{@link #take() take()}</td> |
140 |
* <td>{@link #takeFirst() takeFirst()}</td> |
141 |
* </tr> |
142 |
* <tr> |
143 |
* <td>{@link #poll(long, TimeUnit) poll(time, unit)}</td> |
144 |
* <td>{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)}</td> |
145 |
* </tr> |
146 |
* <tr> |
147 |
* <td ALIGN=CENTER COLSPAN = 2> <b>Examine</b></td> |
148 |
* </tr> |
149 |
* <tr> |
150 |
* <td>{@link #element() element()}</td> |
151 |
* <td>{@link #getFirst() getFirst()}</td> |
152 |
* </tr> |
153 |
* <tr> |
154 |
* <td>{@link #peek() peek()}</td> |
155 |
* <td>{@link #peekFirst() peekFirst()}</td> |
156 |
* </tr> |
157 |
* </table> |
158 |
* |
159 |
* <p>Memory consistency effects: As with other concurrent |
160 |
* collections, actions in a thread prior to placing an object into a |
161 |
* {@code BlockingDeque} |
162 |
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a> |
163 |
* actions subsequent to the access or removal of that element from |
164 |
* the {@code BlockingDeque} in another thread. |
165 |
* |
166 |
* <p>This interface is a member of the |
167 |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
168 |
* Java Collections Framework</a>. |
169 |
* |
170 |
* @since 1.6 |
171 |
* @author Doug Lea |
172 |
* @param <E> the type of elements held in this deque |
173 |
*/ |
174 |
public interface BlockingDeque<E> extends BlockingQueue<E>, Deque<E> { |
175 |
/* |
176 |
* We have "diamond" multiple interface inheritance here, and that |
177 |
* introduces ambiguities. Methods might end up with different |
178 |
* specs depending on the branch chosen by javadoc. Thus a lot of |
179 |
* methods specs here are copied from superinterfaces. |
180 |
*/ |
181 |
|
182 |
/** |
183 |
* Inserts the specified element at the front of this deque if it is |
184 |
* possible to do so immediately without violating capacity restrictions, |
185 |
* throwing an {@code IllegalStateException} if no space is currently |
186 |
* available. When using a capacity-restricted deque, it is generally |
187 |
* preferable to use {@link #offerFirst(Object) offerFirst}. |
188 |
* |
189 |
* @param e the element to add |
190 |
* @throws IllegalStateException {@inheritDoc} |
191 |
* @throws ClassCastException {@inheritDoc} |
192 |
* @throws NullPointerException if the specified element is null |
193 |
* @throws IllegalArgumentException {@inheritDoc} |
194 |
*/ |
195 |
void addFirst(E e); |
196 |
|
197 |
/** |
198 |
* Inserts the specified element at the end of this deque if it is |
199 |
* possible to do so immediately without violating capacity restrictions, |
200 |
* throwing an {@code IllegalStateException} if no space is currently |
201 |
* available. When using a capacity-restricted deque, it is generally |
202 |
* preferable to use {@link #offerLast(Object) offerLast}. |
203 |
* |
204 |
* @param e the element to add |
205 |
* @throws IllegalStateException {@inheritDoc} |
206 |
* @throws ClassCastException {@inheritDoc} |
207 |
* @throws NullPointerException if the specified element is null |
208 |
* @throws IllegalArgumentException {@inheritDoc} |
209 |
*/ |
210 |
void addLast(E e); |
211 |
|
212 |
/** |
213 |
* Inserts the specified element at the front of this deque if it is |
214 |
* possible to do so immediately without violating capacity restrictions, |
215 |
* returning {@code true} upon success and {@code false} if no space is |
216 |
* currently available. |
217 |
* When using a capacity-restricted deque, this method is generally |
218 |
* preferable to the {@link #addFirst(Object) addFirst} method, which can |
219 |
* fail to insert an element only by throwing an exception. |
220 |
* |
221 |
* @param e the element to add |
222 |
* @throws ClassCastException {@inheritDoc} |
223 |
* @throws NullPointerException if the specified element is null |
224 |
* @throws IllegalArgumentException {@inheritDoc} |
225 |
*/ |
226 |
boolean offerFirst(E e); |
227 |
|
228 |
/** |
229 |
* Inserts the specified element at the end of this deque if it is |
230 |
* possible to do so immediately without violating capacity restrictions, |
231 |
* returning {@code true} upon success and {@code false} if no space is |
232 |
* currently available. |
233 |
* When using a capacity-restricted deque, this method is generally |
234 |
* preferable to the {@link #addLast(Object) addLast} method, which can |
235 |
* fail to insert an element only by throwing an exception. |
236 |
* |
237 |
* @param e the element to add |
238 |
* @throws ClassCastException {@inheritDoc} |
239 |
* @throws NullPointerException if the specified element is null |
240 |
* @throws IllegalArgumentException {@inheritDoc} |
241 |
*/ |
242 |
boolean offerLast(E e); |
243 |
|
244 |
/** |
245 |
* Inserts the specified element at the front of this deque, |
246 |
* waiting if necessary for space to become available. |
247 |
* |
248 |
* @param e the element to add |
249 |
* @throws InterruptedException if interrupted while waiting |
250 |
* @throws ClassCastException if the class of the specified element |
251 |
* prevents it from being added to this deque |
252 |
* @throws NullPointerException if the specified element is null |
253 |
* @throws IllegalArgumentException if some property of the specified |
254 |
* element prevents it from being added to this deque |
255 |
*/ |
256 |
void putFirst(E e) throws InterruptedException; |
257 |
|
258 |
/** |
259 |
* Inserts the specified element at the end of this deque, |
260 |
* waiting if necessary for space to become available. |
261 |
* |
262 |
* @param e the element to add |
263 |
* @throws InterruptedException if interrupted while waiting |
264 |
* @throws ClassCastException if the class of the specified element |
265 |
* prevents it from being added to this deque |
266 |
* @throws NullPointerException if the specified element is null |
267 |
* @throws IllegalArgumentException if some property of the specified |
268 |
* element prevents it from being added to this deque |
269 |
*/ |
270 |
void putLast(E e) throws InterruptedException; |
271 |
|
272 |
/** |
273 |
* Inserts the specified element at the front of this deque, |
274 |
* waiting up to the specified wait time if necessary for space to |
275 |
* become available. |
276 |
* |
277 |
* @param e the element to add |
278 |
* @param timeout how long to wait before giving up, in units of |
279 |
* {@code unit} |
280 |
* @param unit a {@code TimeUnit} determining how to interpret the |
281 |
* {@code timeout} parameter |
282 |
* @return {@code true} if successful, or {@code false} if |
283 |
* the specified waiting time elapses before space is available |
284 |
* @throws InterruptedException if interrupted while waiting |
285 |
* @throws ClassCastException if the class of the specified element |
286 |
* prevents it from being added to this deque |
287 |
* @throws NullPointerException if the specified element is null |
288 |
* @throws IllegalArgumentException if some property of the specified |
289 |
* element prevents it from being added to this deque |
290 |
*/ |
291 |
boolean offerFirst(E e, long timeout, TimeUnit unit) |
292 |
throws InterruptedException; |
293 |
|
294 |
/** |
295 |
* Inserts the specified element at the end of this deque, |
296 |
* waiting up to the specified wait time if necessary for space to |
297 |
* become available. |
298 |
* |
299 |
* @param e the element to add |
300 |
* @param timeout how long to wait before giving up, in units of |
301 |
* {@code unit} |
302 |
* @param unit a {@code TimeUnit} determining how to interpret the |
303 |
* {@code timeout} parameter |
304 |
* @return {@code true} if successful, or {@code false} if |
305 |
* the specified waiting time elapses before space is available |
306 |
* @throws InterruptedException if interrupted while waiting |
307 |
* @throws ClassCastException if the class of the specified element |
308 |
* prevents it from being added to this deque |
309 |
* @throws NullPointerException if the specified element is null |
310 |
* @throws IllegalArgumentException if some property of the specified |
311 |
* element prevents it from being added to this deque |
312 |
*/ |
313 |
boolean offerLast(E e, long timeout, TimeUnit unit) |
314 |
throws InterruptedException; |
315 |
|
316 |
/** |
317 |
* Retrieves and removes the first element of this deque, waiting |
318 |
* if necessary until an element becomes available. |
319 |
* |
320 |
* @return the head of this deque |
321 |
* @throws InterruptedException if interrupted while waiting |
322 |
*/ |
323 |
E takeFirst() throws InterruptedException; |
324 |
|
325 |
/** |
326 |
* Retrieves and removes the last element of this deque, waiting |
327 |
* if necessary until an element becomes available. |
328 |
* |
329 |
* @return the tail of this deque |
330 |
* @throws InterruptedException if interrupted while waiting |
331 |
*/ |
332 |
E takeLast() throws InterruptedException; |
333 |
|
334 |
/** |
335 |
* Retrieves and removes the first element of this deque, waiting |
336 |
* up to the specified wait time if necessary for an element to |
337 |
* become available. |
338 |
* |
339 |
* @param timeout how long to wait before giving up, in units of |
340 |
* {@code unit} |
341 |
* @param unit a {@code TimeUnit} determining how to interpret the |
342 |
* {@code timeout} parameter |
343 |
* @return the head of this deque, or {@code null} if the specified |
344 |
* waiting time elapses before an element is available |
345 |
* @throws InterruptedException if interrupted while waiting |
346 |
*/ |
347 |
E pollFirst(long timeout, TimeUnit unit) |
348 |
throws InterruptedException; |
349 |
|
350 |
/** |
351 |
* Retrieves and removes the last element of this deque, waiting |
352 |
* up to the specified wait time if necessary for an element to |
353 |
* become available. |
354 |
* |
355 |
* @param timeout how long to wait before giving up, in units of |
356 |
* {@code unit} |
357 |
* @param unit a {@code TimeUnit} determining how to interpret the |
358 |
* {@code timeout} parameter |
359 |
* @return the tail of this deque, or {@code null} if the specified |
360 |
* waiting time elapses before an element is available |
361 |
* @throws InterruptedException if interrupted while waiting |
362 |
*/ |
363 |
E pollLast(long timeout, TimeUnit unit) |
364 |
throws InterruptedException; |
365 |
|
366 |
/** |
367 |
* Removes the first occurrence of the specified element from this deque. |
368 |
* If the deque does not contain the element, it is unchanged. |
369 |
* More formally, removes the first element {@code e} such that |
370 |
* {@code o.equals(e)} (if such an element exists). |
371 |
* Returns {@code true} if this deque contained the specified element |
372 |
* (or equivalently, if this deque changed as a result of the call). |
373 |
* |
374 |
* @param o element to be removed from this deque, if present |
375 |
* @return {@code true} if an element was removed as a result of this call |
376 |
* @throws ClassCastException if the class of the specified element |
377 |
* is incompatible with this deque |
378 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
379 |
* @throws NullPointerException if the specified element is null |
380 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
381 |
*/ |
382 |
boolean removeFirstOccurrence(Object o); |
383 |
|
384 |
/** |
385 |
* Removes the last occurrence of the specified element from this deque. |
386 |
* If the deque does not contain the element, it is unchanged. |
387 |
* More formally, removes the last element {@code e} such that |
388 |
* {@code o.equals(e)} (if such an element exists). |
389 |
* Returns {@code true} if this deque contained the specified element |
390 |
* (or equivalently, if this deque changed as a result of the call). |
391 |
* |
392 |
* @param o element to be removed from this deque, if present |
393 |
* @return {@code true} if an element was removed as a result of this call |
394 |
* @throws ClassCastException if the class of the specified element |
395 |
* is incompatible with this deque |
396 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
397 |
* @throws NullPointerException if the specified element is null |
398 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
399 |
*/ |
400 |
boolean removeLastOccurrence(Object o); |
401 |
|
402 |
// *** BlockingQueue methods *** |
403 |
|
404 |
/** |
405 |
* Inserts the specified element into the queue represented by this deque |
406 |
* (in other words, at the tail of this deque) if it is possible to do so |
407 |
* immediately without violating capacity restrictions, returning |
408 |
* {@code true} upon success and throwing an |
409 |
* {@code IllegalStateException} if no space is currently available. |
410 |
* When using a capacity-restricted deque, it is generally preferable to |
411 |
* use {@link #offer(Object) offer}. |
412 |
* |
413 |
* <p>This method is equivalent to {@link #addLast(Object) addLast}. |
414 |
* |
415 |
* @param e the element to add |
416 |
* @throws IllegalStateException {@inheritDoc} |
417 |
* @throws ClassCastException if the class of the specified element |
418 |
* prevents it from being added to this deque |
419 |
* @throws NullPointerException if the specified element is null |
420 |
* @throws IllegalArgumentException if some property of the specified |
421 |
* element prevents it from being added to this deque |
422 |
*/ |
423 |
boolean add(E e); |
424 |
|
425 |
/** |
426 |
* Inserts the specified element into the queue represented by this deque |
427 |
* (in other words, at the tail of this deque) if it is possible to do so |
428 |
* immediately without violating capacity restrictions, returning |
429 |
* {@code true} upon success and {@code false} if no space is currently |
430 |
* available. When using a capacity-restricted deque, this method is |
431 |
* generally preferable to the {@link #add} method, which can fail to |
432 |
* insert an element only by throwing an exception. |
433 |
* |
434 |
* <p>This method is equivalent to {@link #offerLast(Object) offerLast}. |
435 |
* |
436 |
* @param e the element to add |
437 |
* @throws ClassCastException if the class of the specified element |
438 |
* prevents it from being added to this deque |
439 |
* @throws NullPointerException if the specified element is null |
440 |
* @throws IllegalArgumentException if some property of the specified |
441 |
* element prevents it from being added to this deque |
442 |
*/ |
443 |
boolean offer(E e); |
444 |
|
445 |
/** |
446 |
* Inserts the specified element into the queue represented by this deque |
447 |
* (in other words, at the tail of this deque), waiting if necessary for |
448 |
* space to become available. |
449 |
* |
450 |
* <p>This method is equivalent to {@link #putLast(Object) putLast}. |
451 |
* |
452 |
* @param e the element to add |
453 |
* @throws InterruptedException {@inheritDoc} |
454 |
* @throws ClassCastException if the class of the specified element |
455 |
* prevents it from being added to this deque |
456 |
* @throws NullPointerException if the specified element is null |
457 |
* @throws IllegalArgumentException if some property of the specified |
458 |
* element prevents it from being added to this deque |
459 |
*/ |
460 |
void put(E e) throws InterruptedException; |
461 |
|
462 |
/** |
463 |
* Inserts the specified element into the queue represented by this deque |
464 |
* (in other words, at the tail of this deque), waiting up to the |
465 |
* specified wait time if necessary for space to become available. |
466 |
* |
467 |
* <p>This method is equivalent to |
468 |
* {@link #offerLast(Object,long,TimeUnit) offerLast}. |
469 |
* |
470 |
* @param e the element to add |
471 |
* @return {@code true} if the element was added to this deque, else |
472 |
* {@code false} |
473 |
* @throws InterruptedException {@inheritDoc} |
474 |
* @throws ClassCastException if the class of the specified element |
475 |
* prevents it from being added to this deque |
476 |
* @throws NullPointerException if the specified element is null |
477 |
* @throws IllegalArgumentException if some property of the specified |
478 |
* element prevents it from being added to this deque |
479 |
*/ |
480 |
boolean offer(E e, long timeout, TimeUnit unit) |
481 |
throws InterruptedException; |
482 |
|
483 |
/** |
484 |
* Retrieves and removes the head of the queue represented by this deque |
485 |
* (in other words, the first element of this deque). |
486 |
* This method differs from {@link #poll poll} only in that it |
487 |
* throws an exception if this deque is empty. |
488 |
* |
489 |
* <p>This method is equivalent to {@link #removeFirst() removeFirst}. |
490 |
* |
491 |
* @return the head of the queue represented by this deque |
492 |
* @throws NoSuchElementException if this deque is empty |
493 |
*/ |
494 |
E remove(); |
495 |
|
496 |
/** |
497 |
* Retrieves and removes the head of the queue represented by this deque |
498 |
* (in other words, the first element of this deque), or returns |
499 |
* {@code null} if this deque is empty. |
500 |
* |
501 |
* <p>This method is equivalent to {@link #pollFirst()}. |
502 |
* |
503 |
* @return the head of this deque, or {@code null} if this deque is empty |
504 |
*/ |
505 |
E poll(); |
506 |
|
507 |
/** |
508 |
* Retrieves and removes the head of the queue represented by this deque |
509 |
* (in other words, the first element of this deque), waiting if |
510 |
* necessary until an element becomes available. |
511 |
* |
512 |
* <p>This method is equivalent to {@link #takeFirst() takeFirst}. |
513 |
* |
514 |
* @return the head of this deque |
515 |
* @throws InterruptedException if interrupted while waiting |
516 |
*/ |
517 |
E take() throws InterruptedException; |
518 |
|
519 |
/** |
520 |
* Retrieves and removes the head of the queue represented by this deque |
521 |
* (in other words, the first element of this deque), waiting up to the |
522 |
* specified wait time if necessary for an element to become available. |
523 |
* |
524 |
* <p>This method is equivalent to |
525 |
* {@link #pollFirst(long,TimeUnit) pollFirst}. |
526 |
* |
527 |
* @return the head of this deque, or {@code null} if the |
528 |
* specified waiting time elapses before an element is available |
529 |
* @throws InterruptedException if interrupted while waiting |
530 |
*/ |
531 |
E poll(long timeout, TimeUnit unit) |
532 |
throws InterruptedException; |
533 |
|
534 |
/** |
535 |
* Retrieves, but does not remove, the head of the queue represented by |
536 |
* this deque (in other words, the first element of this deque). |
537 |
* This method differs from {@link #peek peek} only in that it throws an |
538 |
* exception if this deque is empty. |
539 |
* |
540 |
* <p>This method is equivalent to {@link #getFirst() getFirst}. |
541 |
* |
542 |
* @return the head of this deque |
543 |
* @throws NoSuchElementException if this deque is empty |
544 |
*/ |
545 |
E element(); |
546 |
|
547 |
/** |
548 |
* Retrieves, but does not remove, the head of the queue represented by |
549 |
* this deque (in other words, the first element of this deque), or |
550 |
* returns {@code null} if this deque is empty. |
551 |
* |
552 |
* <p>This method is equivalent to {@link #peekFirst() peekFirst}. |
553 |
* |
554 |
* @return the head of this deque, or {@code null} if this deque is empty |
555 |
*/ |
556 |
E peek(); |
557 |
|
558 |
/** |
559 |
* Removes the first occurrence of the specified element from this deque. |
560 |
* If the deque does not contain the element, it is unchanged. |
561 |
* More formally, removes the first element {@code e} such that |
562 |
* {@code o.equals(e)} (if such an element exists). |
563 |
* Returns {@code true} if this deque contained the specified element |
564 |
* (or equivalently, if this deque changed as a result of the call). |
565 |
* |
566 |
* <p>This method is equivalent to |
567 |
* {@link #removeFirstOccurrence(Object) removeFirstOccurrence}. |
568 |
* |
569 |
* @param o element to be removed from this deque, if present |
570 |
* @return {@code true} if this deque changed as a result of the call |
571 |
* @throws ClassCastException if the class of the specified element |
572 |
* is incompatible with this deque |
573 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
574 |
* @throws NullPointerException if the specified element is null |
575 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
576 |
*/ |
577 |
boolean remove(Object o); |
578 |
|
579 |
/** |
580 |
* Returns {@code true} if this deque contains the specified element. |
581 |
* More formally, returns {@code true} if and only if this deque contains |
582 |
* at least one element {@code e} such that {@code o.equals(e)}. |
583 |
* |
584 |
* @param o object to be checked for containment in this deque |
585 |
* @return {@code true} if this deque contains the specified element |
586 |
* @throws ClassCastException if the class of the specified element |
587 |
* is incompatible with this deque |
588 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
589 |
* @throws NullPointerException if the specified element is null |
590 |
* (<a href="{@docRoot}/../api/java/util/Collection.html#optional-restrictions">optional</a>) |
591 |
*/ |
592 |
boolean contains(Object o); |
593 |
|
594 |
/** |
595 |
* Returns the number of elements in this deque. |
596 |
* |
597 |
* @return the number of elements in this deque |
598 |
*/ |
599 |
int size(); |
600 |
|
601 |
/** |
602 |
* Returns an iterator over the elements in this deque in proper sequence. |
603 |
* The elements will be returned in order from first (head) to last (tail). |
604 |
* |
605 |
* @return an iterator over the elements in this deque in proper sequence |
606 |
*/ |
607 |
Iterator<E> iterator(); |
608 |
|
609 |
// *** Stack methods *** |
610 |
|
611 |
/** |
612 |
* Pushes an element onto the stack represented by this deque (in other |
613 |
* words, at the head of this deque) if it is possible to do so |
614 |
* immediately without violating capacity restrictions, throwing an |
615 |
* {@code IllegalStateException} if no space is currently available. |
616 |
* |
617 |
* <p>This method is equivalent to {@link #addFirst(Object) addFirst}. |
618 |
* |
619 |
* @throws IllegalStateException {@inheritDoc} |
620 |
* @throws ClassCastException {@inheritDoc} |
621 |
* @throws NullPointerException if the specified element is null |
622 |
* @throws IllegalArgumentException {@inheritDoc} |
623 |
*/ |
624 |
void push(E e); |
625 |
} |