ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Deque.java
Revision: 1.42
Committed: Mon Oct 1 00:10:53 2018 UTC (5 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.41: +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 and Josh Bloch with assistance from members of
3     * JCP JSR-166 Expert Group and released to the public domain, as explained
4 jsr166 1.20 * at http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     package java.util;
8    
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 jsr166 1.22 * and is usually pronounced "deck". Most {@code Deque}
13 dl 1.1 * 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.
16     *
17     * <p>This interface defines methods to access the elements at both
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 jsr166 1.22 * special value (either {@code null} or {@code false}, depending on
22 dl 1.1 * the operation). The latter form of the insert operation is
23     * designed specifically for use with capacity-restricted
24 jsr166 1.22 * {@code Deque} implementations; in most implementations, insert
25 dl 1.1 * operations cannot fail.
26     *
27 dl 1.3 * <p>The twelve methods described above are summarized in the
28 jsr166 1.5 * following table:
29 dl 1.3 *
30 jsr166 1.40 * <table class="striped">
31 jsr166 1.24 * <caption>Summary of Deque methods</caption>
32 jsr166 1.40 * <thead>
33 dl 1.1 * <tr>
34 jsr166 1.40 * <td rowspan="2"></td>
35     * <th scope="col" colspan="2"> First Element (Head)</th>
36     * <th scope="col" colspan="2"> Last Element (Tail)</th>
37 dl 1.1 * </tr>
38     * <tr>
39 jsr166 1.40 * <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th>
40     * <th scope="col" style="font-weight:normal; font-style:italic">Special value</th>
41     * <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th>
42     * <th scope="col" style="font-weight:normal; font-style:italic">Special value</th>
43 dl 1.1 * </tr>
44 jsr166 1.40 * </thead>
45     * <tbody>
46 dl 1.1 * <tr>
47 jsr166 1.40 * <th scope="row">Insert</th>
48 jsr166 1.35 * <td>{@link #addFirst(Object) addFirst(e)}</td>
49     * <td>{@link #offerFirst(Object) offerFirst(e)}</td>
50     * <td>{@link #addLast(Object) addLast(e)}</td>
51     * <td>{@link #offerLast(Object) offerLast(e)}</td>
52 dl 1.1 * </tr>
53     * <tr>
54 jsr166 1.40 * <th scope="row">Remove</th>
55 jsr166 1.35 * <td>{@link #removeFirst() removeFirst()}</td>
56     * <td>{@link #pollFirst() pollFirst()}</td>
57     * <td>{@link #removeLast() removeLast()}</td>
58     * <td>{@link #pollLast() pollLast()}</td>
59 dl 1.1 * </tr>
60     * <tr>
61 jsr166 1.40 * <th scope="row">Examine</th>
62 jsr166 1.35 * <td>{@link #getFirst() getFirst()}</td>
63     * <td>{@link #peekFirst() peekFirst()}</td>
64     * <td>{@link #getLast() getLast()}</td>
65     * <td>{@link #peekLast() peekLast()}</td>
66 dl 1.1 * </tr>
67 jsr166 1.40 * </tbody>
68 dl 1.1 * </table>
69     *
70     * <p>This interface extends the {@link Queue} interface. When a deque is
71     * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
72 dl 1.4 * added at the end of the deque and removed from the beginning. The methods
73 jsr166 1.22 * inherited from the {@code Queue} interface are precisely equivalent to
74     * {@code Deque} methods as indicated in the following table:
75 dl 1.1 *
76 jsr166 1.40 * <table class="striped">
77 jsr166 1.24 * <caption>Comparison of Queue and Deque methods</caption>
78 jsr166 1.40 * <thead>
79 dl 1.1 * <tr>
80 jsr166 1.40 * <th scope="col"> {@code Queue} Method</th>
81     * <th scope="col"> Equivalent {@code Deque} Method</th>
82 dl 1.1 * </tr>
83 jsr166 1.40 * </thead>
84     * <tbody>
85 dl 1.1 * <tr>
86 jsr166 1.40 * <th scope="row">{@link #add(Object) add(e)}</th>
87 jsr166 1.35 * <td>{@link #addLast(Object) addLast(e)}</td>
88 jsr166 1.8 * </tr>
89     * <tr>
90 jsr166 1.40 * <th scope="row">{@link #offer(Object) offer(e)}</th>
91 jsr166 1.35 * <td>{@link #offerLast(Object) offerLast(e)}</td>
92 jsr166 1.5 * </tr>
93     * <tr>
94 jsr166 1.40 * <th scope="row">{@link #remove() remove()}</th>
95 jsr166 1.35 * <td>{@link #removeFirst() removeFirst()}</td>
96 jsr166 1.5 * </tr>
97     * <tr>
98 jsr166 1.40 * <th scope="row">{@link #poll() poll()}</th>
99 jsr166 1.35 * <td>{@link #pollFirst() pollFirst()}</td>
100 jsr166 1.5 * </tr>
101     * <tr>
102 jsr166 1.40 * <th scope="row">{@link #element() element()}</th>
103 jsr166 1.35 * <td>{@link #getFirst() getFirst()}</td>
104 jsr166 1.5 * </tr>
105     * <tr>
106 jsr166 1.40 * <th scope="row">{@link #peek() peek()}</th>
107 jsr166 1.35 * <td>{@link #peekFirst() peekFirst()}</td>
108 jsr166 1.5 * </tr>
109 jsr166 1.40 * </tbody>
110 dl 1.1 * </table>
111     *
112     * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This
113     * interface should be used in preference to the legacy {@link Stack} class.
114 dl 1.3 * When a deque is used as a stack, elements are pushed and popped from the
115 jsr166 1.41 * beginning of the deque. Stack methods are equivalent to {@code Deque}
116     * methods as indicated in the table below:
117 dl 1.1 *
118 jsr166 1.40 * <table class="striped">
119 jsr166 1.24 * <caption>Comparison of Stack and Deque methods</caption>
120 jsr166 1.40 * <thead>
121 dl 1.1 * <tr>
122 jsr166 1.40 * <th scope="col"> Stack Method</th>
123     * <th scope="col"> Equivalent {@code Deque} Method</th>
124 dl 1.1 * </tr>
125 jsr166 1.40 * </thead>
126     * <tbody>
127 dl 1.1 * <tr>
128 jsr166 1.40 * <th scope="row">{@link #push(Object) push(e)}</th>
129 jsr166 1.35 * <td>{@link #addFirst(Object) addFirst(e)}</td>
130 jsr166 1.5 * </tr>
131     * <tr>
132 jsr166 1.40 * <th scope="row">{@link #pop() pop()}</th>
133 jsr166 1.35 * <td>{@link #removeFirst() removeFirst()}</td>
134 jsr166 1.5 * </tr>
135     * <tr>
136 jsr166 1.40 * <th scope="row">{@link #peek() peek()}</th>
137 jsr166 1.41 * <td>{@link #getFirst() getFirst()}</td>
138 jsr166 1.5 * </tr>
139 jsr166 1.40 * </tbody>
140 dl 1.1 * </table>
141     *
142     * <p>Note that the {@link #peek peek} method works equally well when
143     * a deque is used as a queue or a stack; in either case, elements are
144     * drawn from the beginning of the deque.
145     *
146 dl 1.2 * <p>This interface provides two methods to remove interior
147 dl 1.1 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
148 jsr166 1.8 * {@link #removeLastOccurrence removeLastOccurrence}.
149     *
150     * <p>Unlike the {@link List} interface, this interface does not
151     * provide support for indexed access to elements.
152 dl 1.1 *
153 jsr166 1.22 * <p>While {@code Deque} implementations are not strictly required
154 dl 1.1 * to prohibit the insertion of null elements, they are strongly
155 jsr166 1.22 * encouraged to do so. Users of any {@code Deque} implementations
156 dl 1.1 * that do allow null elements are strongly encouraged <i>not</i> to
157     * take advantage of the ability to insert nulls. This is so because
158 jsr166 1.22 * {@code null} is used as a special return value by various methods
159 jsr166 1.34 * to indicate that the deque is empty.
160 dl 1.3 *
161 jsr166 1.22 * <p>{@code Deque} implementations generally do not define
162     * element-based versions of the {@code equals} and {@code hashCode}
163 dl 1.1 * methods, but instead inherit the identity-based versions from class
164 jsr166 1.22 * {@code Object}.
165 dl 1.1 *
166 jsr166 1.36 * <p>This interface is a member of the
167 jsr166 1.42 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
168 jsr166 1.36 * Java Collections Framework</a>.
169 dl 1.1 *
170     * @author Doug Lea
171     * @author Josh Bloch
172     * @since 1.6
173 jsr166 1.30 * @param <E> the type of elements held in this deque
174 dl 1.1 */
175     public interface Deque<E> extends Queue<E> {
176     /**
177 jsr166 1.8 * Inserts the specified element at the front of this deque if it is
178 jsr166 1.25 * possible to do so immediately without violating capacity restrictions,
179     * throwing an {@code IllegalStateException} if no space is currently
180     * available. When using a capacity-restricted deque, it is generally
181     * preferable to use method {@link #offerFirst}.
182 jsr166 1.8 *
183     * @param e the element to add
184     * @throws IllegalStateException if the element cannot be added at this
185     * time due to capacity restrictions
186 jsr166 1.9 * @throws ClassCastException if the class of the specified element
187     * prevents it from being added to this deque
188 jsr166 1.8 * @throws NullPointerException if the specified element is null and this
189     * deque does not permit null elements
190     * @throws IllegalArgumentException if some property of the specified
191     * element prevents it from being added to this deque
192     */
193     void addFirst(E e);
194    
195     /**
196 jsr166 1.13 * Inserts the specified element at the end of this deque if it is
197 jsr166 1.25 * possible to do so immediately without violating capacity restrictions,
198     * throwing an {@code IllegalStateException} if no space is currently
199     * available. When using a capacity-restricted deque, it is generally
200     * preferable to use method {@link #offerLast}.
201 jsr166 1.8 *
202 jsr166 1.14 * <p>This method is equivalent to {@link #add}.
203     *
204 jsr166 1.8 * @param e the element to add
205     * @throws IllegalStateException if the element cannot be added at this
206     * time due to capacity restrictions
207 jsr166 1.9 * @throws ClassCastException if the class of the specified element
208     * prevents it from being added to this deque
209 jsr166 1.8 * @throws NullPointerException if the specified element is null and this
210     * deque does not permit null elements
211     * @throws IllegalArgumentException if some property of the specified
212     * element prevents it from being added to this deque
213     */
214     void addLast(E e);
215    
216     /**
217 dl 1.3 * Inserts the specified element at the front of this deque unless it would
218 dl 1.1 * violate capacity restrictions. When using a capacity-restricted deque,
219 jsr166 1.8 * this method is generally preferable to the {@link #addFirst} method,
220     * which can fail to insert an element only by throwing an exception.
221 dl 1.1 *
222 jsr166 1.8 * @param e the element to add
223 jsr166 1.22 * @return {@code true} if the element was added to this deque, else
224     * {@code false}
225 jsr166 1.9 * @throws ClassCastException if the class of the specified element
226     * prevents it from being added to this deque
227 dl 1.3 * @throws NullPointerException if the specified element is null and this
228 jsr166 1.8 * deque does not permit null elements
229     * @throws IllegalArgumentException if some property of the specified
230     * element prevents it from being added to this deque
231 dl 1.1 */
232     boolean offerFirst(E e);
233    
234     /**
235 dl 1.4 * Inserts the specified element at the end of this deque unless it would
236 dl 1.1 * violate capacity restrictions. When using a capacity-restricted deque,
237 jsr166 1.8 * this method is generally preferable to the {@link #addLast} method,
238     * which can fail to insert an element only by throwing an exception.
239 dl 1.1 *
240 jsr166 1.8 * @param e the element to add
241 jsr166 1.22 * @return {@code true} if the element was added to this deque, else
242     * {@code false}
243 jsr166 1.9 * @throws ClassCastException if the class of the specified element
244     * prevents it from being added to this deque
245 dl 1.3 * @throws NullPointerException if the specified element is null and this
246 jsr166 1.8 * deque does not permit null elements
247     * @throws IllegalArgumentException if some property of the specified
248     * element prevents it from being added to this deque
249 dl 1.1 */
250     boolean offerLast(E e);
251    
252     /**
253 jsr166 1.8 * Retrieves and removes the first element of this deque. This method
254 jsr166 1.15 * differs from {@link #pollFirst pollFirst} only in that it throws an
255     * exception if this deque is empty.
256 dl 1.1 *
257 jsr166 1.8 * @return the head of this deque
258     * @throws NoSuchElementException if this deque is empty
259 dl 1.1 */
260 jsr166 1.8 E removeFirst();
261 dl 1.1
262     /**
263 jsr166 1.8 * Retrieves and removes the last element of this deque. This method
264 jsr166 1.15 * differs from {@link #pollLast pollLast} only in that it throws an
265     * exception if this deque is empty.
266 dl 1.1 *
267 jsr166 1.8 * @return the tail of this deque
268     * @throws NoSuchElementException if this deque is empty
269 dl 1.1 */
270 jsr166 1.8 E removeLast();
271 dl 1.1
272     /**
273 jsr166 1.8 * Retrieves and removes the first element of this deque,
274 jsr166 1.22 * or returns {@code null} if this deque is empty.
275 dl 1.1 *
276 jsr166 1.22 * @return the head of this deque, or {@code null} if this deque is empty
277 dl 1.1 */
278     E pollFirst();
279    
280     /**
281 jsr166 1.8 * Retrieves and removes the last element of this deque,
282 jsr166 1.22 * or returns {@code null} if this deque is empty.
283 dl 1.1 *
284 jsr166 1.22 * @return the tail of this deque, or {@code null} if this deque is empty
285 dl 1.1 */
286     E pollLast();
287    
288     /**
289 jsr166 1.8 * Retrieves, but does not remove, the first element of this deque.
290 jsr166 1.16 *
291 jsr166 1.15 * This method differs from {@link #peekFirst peekFirst} only in that it
292     * throws an exception if this deque is empty.
293 dl 1.1 *
294 jsr166 1.8 * @return the head of this deque
295 dl 1.1 * @throws NoSuchElementException if this deque is empty
296     */
297 jsr166 1.8 E getFirst();
298 dl 1.1
299     /**
300 jsr166 1.8 * Retrieves, but does not remove, the last element of this deque.
301 jsr166 1.15 * This method differs from {@link #peekLast peekLast} only in that it
302     * throws an exception if this deque is empty.
303 dl 1.1 *
304 jsr166 1.8 * @return the tail of this deque
305 dl 1.1 * @throws NoSuchElementException if this deque is empty
306     */
307 jsr166 1.8 E getLast();
308 dl 1.1
309     /**
310     * Retrieves, but does not remove, the first element of this deque,
311 jsr166 1.22 * or returns {@code null} if this deque is empty.
312 dl 1.1 *
313 jsr166 1.22 * @return the head of this deque, or {@code null} if this deque is empty
314 dl 1.1 */
315     E peekFirst();
316    
317     /**
318     * Retrieves, but does not remove, the last element of this deque,
319 jsr166 1.22 * or returns {@code null} if this deque is empty.
320 dl 1.1 *
321 jsr166 1.22 * @return the tail of this deque, or {@code null} if this deque is empty
322 dl 1.1 */
323     E peekLast();
324    
325     /**
326 jsr166 1.8 * Removes the first occurrence of the specified element from this deque.
327     * If the deque does not contain the element, it is unchanged.
328 jsr166 1.22 * More formally, removes the first element {@code e} such that
329 jsr166 1.29 * {@code Objects.equals(o, e)} (if such an element exists).
330 jsr166 1.22 * Returns {@code true} if this deque contained the specified element
331 jsr166 1.12 * (or equivalently, if this deque changed as a result of the call).
332 dl 1.1 *
333 dl 1.3 * @param o element to be removed from this deque, if present
334 jsr166 1.22 * @return {@code true} if an element was removed as a result of this call
335 jsr166 1.9 * @throws ClassCastException if the class of the specified element
336 jsr166 1.27 * is incompatible with this deque
337 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
338 jsr166 1.32 * @throws NullPointerException if the specified element is null and this
339     * deque does not permit null elements
340 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
341 dl 1.1 */
342 dl 1.3 boolean removeFirstOccurrence(Object o);
343 dl 1.1
344     /**
345 jsr166 1.8 * Removes the last occurrence of the specified element from this deque.
346     * If the deque does not contain the element, it is unchanged.
347 jsr166 1.22 * More formally, removes the last element {@code e} such that
348 jsr166 1.29 * {@code Objects.equals(o, e)} (if such an element exists).
349 jsr166 1.22 * Returns {@code true} if this deque contained the specified element
350 jsr166 1.12 * (or equivalently, if this deque changed as a result of the call).
351 dl 1.1 *
352 dl 1.3 * @param o element to be removed from this deque, if present
353 jsr166 1.22 * @return {@code true} if an element was removed as a result of this call
354 jsr166 1.9 * @throws ClassCastException if the class of the specified element
355 jsr166 1.27 * is incompatible with this deque
356 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
357 jsr166 1.32 * @throws NullPointerException if the specified element is null and this
358     * deque does not permit null elements
359 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
360 dl 1.1 */
361 dl 1.3 boolean removeLastOccurrence(Object o);
362 dl 1.1
363 jsr166 1.8 // *** Queue methods ***
364 dl 1.1
365 jsr166 1.8 /**
366     * Inserts the specified element into the queue represented by this deque
367     * (in other words, at the tail of this deque) if it is possible to do so
368     * immediately without violating capacity restrictions, returning
369 jsr166 1.22 * {@code true} upon success and throwing an
370     * {@code IllegalStateException} if no space is currently available.
371 jsr166 1.8 * When using a capacity-restricted deque, it is generally preferable to
372     * use {@link #offer(Object) offer}.
373     *
374 jsr166 1.14 * <p>This method is equivalent to {@link #addLast}.
375 jsr166 1.8 *
376     * @param e the element to add
377 jsr166 1.22 * @return {@code true} (as specified by {@link Collection#add})
378 jsr166 1.8 * @throws IllegalStateException if the element cannot be added at this
379     * time due to capacity restrictions
380 jsr166 1.9 * @throws ClassCastException if the class of the specified element
381     * prevents it from being added to this deque
382 jsr166 1.8 * @throws NullPointerException if the specified element is null and this
383     * deque does not permit null elements
384     * @throws IllegalArgumentException if some property of the specified
385     * element prevents it from being added to this deque
386     */
387     boolean add(E e);
388 dl 1.1
389     /**
390     * Inserts the specified element into the queue represented by this deque
391 jsr166 1.8 * (in other words, at the tail of this deque) if it is possible to do so
392     * immediately without violating capacity restrictions, returning
393 jsr166 1.22 * {@code true} upon success and {@code false} if no space is currently
394 jsr166 1.8 * available. When using a capacity-restricted deque, this method is
395     * generally preferable to the {@link #add} method, which can fail to
396     * insert an element only by throwing an exception.
397 dl 1.1 *
398     * <p>This method is equivalent to {@link #offerLast}.
399     *
400 jsr166 1.8 * @param e the element to add
401 jsr166 1.22 * @return {@code true} if the element was added to this deque, else
402     * {@code false}
403 jsr166 1.9 * @throws ClassCastException if the class of the specified element
404     * prevents it from being added to this deque
405 dl 1.3 * @throws NullPointerException if the specified element is null and this
406 jsr166 1.8 * deque does not permit null elements
407     * @throws IllegalArgumentException if some property of the specified
408     * element prevents it from being added to this deque
409 dl 1.1 */
410     boolean offer(E e);
411    
412     /**
413 jsr166 1.8 * Retrieves and removes the head of the queue represented by this deque
414     * (in other words, the first element of this deque).
415 jsr166 1.35 * This method differs from {@link #poll() poll()} only in that it
416     * throws an exception if this deque is empty.
417 dl 1.1 *
418 jsr166 1.8 * <p>This method is equivalent to {@link #removeFirst()}.
419 dl 1.1 *
420 jsr166 1.8 * @return the head of the queue represented by this deque
421     * @throws NoSuchElementException if this deque is empty
422 dl 1.1 */
423 jsr166 1.8 E remove();
424 dl 1.1
425     /**
426 jsr166 1.8 * Retrieves and removes the head of the queue represented by this deque
427     * (in other words, the first element of this deque), or returns
428 jsr166 1.22 * {@code null} if this deque is empty.
429 dl 1.1 *
430     * <p>This method is equivalent to {@link #pollFirst()}.
431     *
432 jsr166 1.22 * @return the first element of this deque, or {@code null} if
433 jsr166 1.8 * this deque is empty
434 dl 1.1 */
435     E poll();
436    
437     /**
438 jsr166 1.8 * Retrieves, but does not remove, the head of the queue represented by
439     * this deque (in other words, the first element of this deque).
440 jsr166 1.15 * This method differs from {@link #peek peek} only in that it throws an
441 jsr166 1.8 * exception if this deque is empty.
442 dl 1.1 *
443 jsr166 1.8 * <p>This method is equivalent to {@link #getFirst()}.
444 dl 1.1 *
445     * @return the head of the queue represented by this deque
446     * @throws NoSuchElementException if this deque is empty
447     */
448 jsr166 1.8 E element();
449 dl 1.1
450     /**
451     * Retrieves, but does not remove, the head of the queue represented by
452 jsr166 1.8 * this deque (in other words, the first element of this deque), or
453 jsr166 1.22 * returns {@code null} if this deque is empty.
454 dl 1.1 *
455 dl 1.3 * <p>This method is equivalent to {@link #peekFirst()}.
456 dl 1.1 *
457     * @return the head of the queue represented by this deque, or
458 jsr166 1.22 * {@code null} if this deque is empty
459 dl 1.1 */
460     E peek();
461    
462 jsr166 1.39 /**
463     * Adds all of the elements in the specified collection at the end
464     * of this deque, as if by calling {@link #addLast} on each one,
465     * in the order that they are returned by the collection's iterator.
466     *
467     * <p>When using a capacity-restricted deque, it is generally preferable
468     * to call {@link #offer(Object) offer} separately on each element.
469     *
470     * <p>An exception encountered while trying to add an element may result
471     * in only some of the elements having been successfully added when
472     * the associated exception is thrown.
473     *
474     * @param c the elements to be inserted into this deque
475     * @return {@code true} if this deque changed as a result of the call
476     * @throws IllegalStateException if not all the elements can be added at
477     * this time due to insertion restrictions
478     * @throws ClassCastException if the class of an element of the specified
479     * collection prevents it from being added to this deque
480     * @throws NullPointerException if the specified collection contains a
481     * null element and this deque does not permit null elements,
482     * or if the specified collection is null
483     * @throws IllegalArgumentException if some property of an element of the
484     * specified collection prevents it from being added to this deque
485     */
486     boolean addAll(Collection<? extends E> c);
487 dl 1.1
488     // *** Stack methods ***
489    
490     /**
491 jsr166 1.8 * Pushes an element onto the stack represented by this deque (in other
492     * words, at the head of this deque) if it is possible to do so
493 jsr166 1.25 * immediately without violating capacity restrictions, throwing an
494 jsr166 1.22 * {@code IllegalStateException} if no space is currently available.
495 dl 1.1 *
496     * <p>This method is equivalent to {@link #addFirst}.
497     *
498 dl 1.3 * @param e the element to push
499 jsr166 1.8 * @throws IllegalStateException if the element cannot be added at this
500     * time due to capacity restrictions
501 jsr166 1.9 * @throws ClassCastException if the class of the specified element
502     * prevents it from being added to this deque
503 dl 1.3 * @throws NullPointerException if the specified element is null and this
504 jsr166 1.8 * deque does not permit null elements
505     * @throws IllegalArgumentException if some property of the specified
506     * element prevents it from being added to this deque
507 dl 1.1 */
508     void push(E e);
509    
510     /**
511     * Pops an element from the stack represented by this deque. In other
512 dl 1.2 * words, removes and returns the first element of this deque.
513 dl 1.1 *
514     * <p>This method is equivalent to {@link #removeFirst()}.
515     *
516     * @return the element at the front of this deque (which is the top
517 jsr166 1.8 * of the stack represented by this deque)
518 dl 1.1 * @throws NoSuchElementException if this deque is empty
519     */
520     E pop();
521    
522    
523 jsr166 1.8 // *** Collection methods ***
524    
525     /**
526     * Removes the first occurrence of the specified element from this deque.
527     * If the deque does not contain the element, it is unchanged.
528 jsr166 1.22 * More formally, removes the first element {@code e} such that
529 jsr166 1.29 * {@code Objects.equals(o, e)} (if such an element exists).
530 jsr166 1.22 * Returns {@code true} if this deque contained the specified element
531 jsr166 1.12 * (or equivalently, if this deque changed as a result of the call).
532 jsr166 1.8 *
533 jsr166 1.23 * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
534 jsr166 1.8 *
535     * @param o element to be removed from this deque, if present
536 jsr166 1.22 * @return {@code true} if an element was removed as a result of this call
537 jsr166 1.9 * @throws ClassCastException if the class of the specified element
538 jsr166 1.27 * is incompatible with this deque
539 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
540 jsr166 1.32 * @throws NullPointerException if the specified element is null and this
541     * deque does not permit null elements
542 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
543 jsr166 1.8 */
544     boolean remove(Object o);
545    
546     /**
547 jsr166 1.22 * Returns {@code true} if this deque contains the specified element.
548     * More formally, returns {@code true} if and only if this deque contains
549 jsr166 1.29 * at least one element {@code e} such that {@code Objects.equals(o, e)}.
550 jsr166 1.8 *
551     * @param o element whose presence in this deque is to be tested
552 jsr166 1.22 * @return {@code true} if this deque contains the specified element
553 jsr166 1.27 * @throws ClassCastException if the class of the specified element
554     * is incompatible with this deque
555 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
556 jsr166 1.32 * @throws NullPointerException if the specified element is null and this
557     * deque does not permit null elements
558 jsr166 1.42 * (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
559 jsr166 1.8 */
560     boolean contains(Object o);
561    
562     /**
563     * Returns the number of elements in this deque.
564     *
565     * @return the number of elements in this deque
566     */
567 jsr166 1.31 int size();
568 dl 1.1
569     /**
570 jsr166 1.10 * Returns an iterator over the elements in this deque in proper sequence.
571     * The elements will be returned in order from first (head) to last (tail).
572 dl 1.3 *
573 jsr166 1.10 * @return an iterator over the elements in this deque in proper sequence
574 dl 1.1 */
575     Iterator<E> iterator();
576 dl 1.17
577     /**
578     * Returns an iterator over the elements in this deque in reverse
579     * sequential order. The elements will be returned in order from
580     * last (tail) to first (head).
581     *
582     * @return an iterator over the elements in this deque in reverse
583     * sequence
584     */
585     Iterator<E> descendingIterator();
586    
587 dl 1.1 }