ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/BlockingDeque.java
Revision: 1.37
Committed: Mon Oct 1 00:10:53 2018 UTC (5 years, 8 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.36: +9 -9 lines
Log Message:
update to using jdk11 by default, except link to jdk10 javadocs;
sync @docRoot references in javadoc with upstream

File Contents

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