• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * This file is available under and governed by the GNU General Public
27  * License version 2 only, as published by the Free Software Foundation.
28  * However, the following notice accompanied the original version of this
29  * file:
30  * Written by Doug Lea and Josh Bloch with assistance from members of
31  * JCP JSR-166 Expert Group and released to the public domain, as explained
32  * at http://creativecommons.org/publicdomain/zero/1.0/
33  */
34 
35 package java.util;
36 
37 // BEGIN android-note
38 // removed link to collections framework docs
39 // END android-note
40 
41 /**
42  * A linear collection that supports element insertion and removal at
43  * both ends.  The name <i>deque</i> is short for "double ended queue"
44  * and is usually pronounced "deck".  Most {@code Deque}
45  * implementations place no fixed limits on the number of elements
46  * they may contain, but this interface supports capacity-restricted
47  * deques as well as those with no fixed size limit.
48  *
49  * <p>This interface defines methods to access the elements at both
50  * ends of the deque.  Methods are provided to insert, remove, and
51  * examine the element.  Each of these methods exists in two forms:
52  * one throws an exception if the operation fails, the other returns a
53  * special value (either {@code null} or {@code false}, depending on
54  * the operation).  The latter form of the insert operation is
55  * designed specifically for use with capacity-restricted
56  * {@code Deque} implementations; in most implementations, insert
57  * operations cannot fail.
58  *
59  * <p>The twelve methods described above are summarized in the
60  * following table:
61  *
62  * <table BORDER CELLPADDING=3 CELLSPACING=1>
63  * <caption>Summary of Deque methods</caption>
64  *  <tr>
65  *    <td></td>
66  *    <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>
67  *    <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>
68  *  </tr>
69  *  <tr>
70  *    <td></td>
71  *    <td ALIGN=CENTER><em>Throws exception</em></td>
72  *    <td ALIGN=CENTER><em>Special value</em></td>
73  *    <td ALIGN=CENTER><em>Throws exception</em></td>
74  *    <td ALIGN=CENTER><em>Special value</em></td>
75  *  </tr>
76  *  <tr>
77  *    <td><b>Insert</b></td>
78  *    <td>{@link Deque#addFirst addFirst(e)}</td>
79  *    <td>{@link Deque#offerFirst offerFirst(e)}</td>
80  *    <td>{@link Deque#addLast addLast(e)}</td>
81  *    <td>{@link Deque#offerLast offerLast(e)}</td>
82  *  </tr>
83  *  <tr>
84  *    <td><b>Remove</b></td>
85  *    <td>{@link Deque#removeFirst removeFirst()}</td>
86  *    <td>{@link Deque#pollFirst pollFirst()}</td>
87  *    <td>{@link Deque#removeLast removeLast()}</td>
88  *    <td>{@link Deque#pollLast pollLast()}</td>
89  *  </tr>
90  *  <tr>
91  *    <td><b>Examine</b></td>
92  *    <td>{@link Deque#getFirst getFirst()}</td>
93  *    <td>{@link Deque#peekFirst peekFirst()}</td>
94  *    <td>{@link Deque#getLast getLast()}</td>
95  *    <td>{@link Deque#peekLast peekLast()}</td>
96  *  </tr>
97  * </table>
98  *
99  * <p>This interface extends the {@link Queue} interface.  When a deque is
100  * used as a queue, FIFO (First-In-First-Out) behavior results.  Elements are
101  * added at the end of the deque and removed from the beginning.  The methods
102  * inherited from the {@code Queue} interface are precisely equivalent to
103  * {@code Deque} methods as indicated in the following table:
104  *
105  * <table BORDER CELLPADDING=3 CELLSPACING=1>
106  * <caption>Comparison of Queue and Deque methods</caption>
107  *  <tr>
108  *    <td ALIGN=CENTER> <b>{@code Queue} Method</b></td>
109  *    <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>
110  *  </tr>
111  *  <tr>
112  *    <td>{@link java.util.Queue#add add(e)}</td>
113  *    <td>{@link #addLast addLast(e)}</td>
114  *  </tr>
115  *  <tr>
116  *    <td>{@link java.util.Queue#offer offer(e)}</td>
117  *    <td>{@link #offerLast offerLast(e)}</td>
118  *  </tr>
119  *  <tr>
120  *    <td>{@link java.util.Queue#remove remove()}</td>
121  *    <td>{@link #removeFirst removeFirst()}</td>
122  *  </tr>
123  *  <tr>
124  *    <td>{@link java.util.Queue#poll poll()}</td>
125  *    <td>{@link #pollFirst pollFirst()}</td>
126  *  </tr>
127  *  <tr>
128  *    <td>{@link java.util.Queue#element element()}</td>
129  *    <td>{@link #getFirst getFirst()}</td>
130  *  </tr>
131  *  <tr>
132  *    <td>{@link java.util.Queue#peek peek()}</td>
133  *    <td>{@link #peek peekFirst()}</td>
134  *  </tr>
135  * </table>
136  *
137  * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks.  This
138  * interface should be used in preference to the legacy {@link Stack} class.
139  * When a deque is used as a stack, elements are pushed and popped from the
140  * beginning of the deque.  Stack methods are precisely equivalent to
141  * {@code Deque} methods as indicated in the table below:
142  *
143  * <table BORDER CELLPADDING=3 CELLSPACING=1>
144  * <caption>Comparison of Stack and Deque methods</caption>
145  *  <tr>
146  *    <td ALIGN=CENTER> <b>Stack Method</b></td>
147  *    <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>
148  *  </tr>
149  *  <tr>
150  *    <td>{@link #push push(e)}</td>
151  *    <td>{@link #addFirst addFirst(e)}</td>
152  *  </tr>
153  *  <tr>
154  *    <td>{@link #pop pop()}</td>
155  *    <td>{@link #removeFirst removeFirst()}</td>
156  *  </tr>
157  *  <tr>
158  *    <td>{@link #peek peek()}</td>
159  *    <td>{@link #peekFirst peekFirst()}</td>
160  *  </tr>
161  * </table>
162  *
163  * <p>Note that the {@link #peek peek} method works equally well when
164  * a deque is used as a queue or a stack; in either case, elements are
165  * drawn from the beginning of the deque.
166  *
167  * <p>This interface provides two methods to remove interior
168  * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
169  * {@link #removeLastOccurrence removeLastOccurrence}.
170  *
171  * <p>Unlike the {@link List} interface, this interface does not
172  * provide support for indexed access to elements.
173  *
174  * <p>While {@code Deque} implementations are not strictly required
175  * to prohibit the insertion of null elements, they are strongly
176  * encouraged to do so.  Users of any {@code Deque} implementations
177  * that do allow null elements are strongly encouraged <i>not</i> to
178  * take advantage of the ability to insert nulls.  This is so because
179  * {@code null} is used as a special return value by various methods
180  * to indicated that the deque is empty.
181  *
182  * <p>{@code Deque} implementations generally do not define
183  * element-based versions of the {@code equals} and {@code hashCode}
184  * methods, but instead inherit the identity-based versions from class
185  * {@code Object}.
186  *
187  * @author Doug Lea
188  * @author Josh Bloch
189  * @since  1.6
190  * @param <E> the type of elements held in this deque
191  */
192 public interface Deque<E> extends Queue<E> {
193     /**
194      * Inserts the specified element at the front of this deque if it is
195      * possible to do so immediately without violating capacity restrictions,
196      * throwing an {@code IllegalStateException} if no space is currently
197      * available.  When using a capacity-restricted deque, it is generally
198      * preferable to use method {@link #offerFirst}.
199      *
200      * @param e the element to add
201      * @throws IllegalStateException if the element cannot be added at this
202      *         time due to capacity restrictions
203      * @throws ClassCastException if the class of the specified element
204      *         prevents it from being added to this deque
205      * @throws NullPointerException if the specified element is null and this
206      *         deque does not permit null elements
207      * @throws IllegalArgumentException if some property of the specified
208      *         element prevents it from being added to this deque
209      */
addFirst(E e)210     void addFirst(E e);
211 
212     /**
213      * Inserts the specified element at the end of this deque if it is
214      * possible to do so immediately without violating capacity restrictions,
215      * throwing an {@code IllegalStateException} if no space is currently
216      * available.  When using a capacity-restricted deque, it is generally
217      * preferable to use method {@link #offerLast}.
218      *
219      * <p>This method is equivalent to {@link #add}.
220      *
221      * @param e the element to add
222      * @throws IllegalStateException if the element cannot be added at this
223      *         time due to capacity restrictions
224      * @throws ClassCastException if the class of the specified element
225      *         prevents it from being added to this deque
226      * @throws NullPointerException if the specified element is null and this
227      *         deque does not permit null elements
228      * @throws IllegalArgumentException if some property of the specified
229      *         element prevents it from being added to this deque
230      */
addLast(E e)231     void addLast(E e);
232 
233     /**
234      * Inserts the specified element at the front of this deque unless it would
235      * violate capacity restrictions.  When using a capacity-restricted deque,
236      * this method is generally preferable to the {@link #addFirst} method,
237      * which can fail to insert an element only by throwing an exception.
238      *
239      * @param e the element to add
240      * @return {@code true} if the element was added to this deque, else
241      *         {@code false}
242      * @throws ClassCastException if the class of the specified element
243      *         prevents it from being added to this deque
244      * @throws NullPointerException if the specified element is null and this
245      *         deque does not permit null elements
246      * @throws IllegalArgumentException if some property of the specified
247      *         element prevents it from being added to this deque
248      */
offerFirst(E e)249     boolean offerFirst(E e);
250 
251     /**
252      * Inserts the specified element at the end of this deque unless it would
253      * violate capacity restrictions.  When using a capacity-restricted deque,
254      * this method is generally preferable to the {@link #addLast} method,
255      * which can fail to insert an element only by throwing an exception.
256      *
257      * @param e the element to add
258      * @return {@code true} if the element was added to this deque, else
259      *         {@code false}
260      * @throws ClassCastException if the class of the specified element
261      *         prevents it from being added to this deque
262      * @throws NullPointerException if the specified element is null and this
263      *         deque does not permit null elements
264      * @throws IllegalArgumentException if some property of the specified
265      *         element prevents it from being added to this deque
266      */
offerLast(E e)267     boolean offerLast(E e);
268 
269     /**
270      * Retrieves and removes the first element of this deque.  This method
271      * differs from {@link #pollFirst pollFirst} only in that it throws an
272      * exception if this deque is empty.
273      *
274      * @return the head of this deque
275      * @throws NoSuchElementException if this deque is empty
276      */
removeFirst()277     E removeFirst();
278 
279     /**
280      * Retrieves and removes the last element of this deque.  This method
281      * differs from {@link #pollLast pollLast} only in that it throws an
282      * exception if this deque is empty.
283      *
284      * @return the tail of this deque
285      * @throws NoSuchElementException if this deque is empty
286      */
removeLast()287     E removeLast();
288 
289     /**
290      * Retrieves and removes the first element of this deque,
291      * or returns {@code null} if this deque is empty.
292      *
293      * @return the head of this deque, or {@code null} if this deque is empty
294      */
pollFirst()295     E pollFirst();
296 
297     /**
298      * Retrieves and removes the last element of this deque,
299      * or returns {@code null} if this deque is empty.
300      *
301      * @return the tail of this deque, or {@code null} if this deque is empty
302      */
pollLast()303     E pollLast();
304 
305     /**
306      * Retrieves, but does not remove, the first element of this deque.
307      *
308      * This method differs from {@link #peekFirst peekFirst} only in that it
309      * throws an exception if this deque is empty.
310      *
311      * @return the head of this deque
312      * @throws NoSuchElementException if this deque is empty
313      */
getFirst()314     E getFirst();
315 
316     /**
317      * Retrieves, but does not remove, the last element of this deque.
318      * This method differs from {@link #peekLast peekLast} only in that it
319      * throws an exception if this deque is empty.
320      *
321      * @return the tail of this deque
322      * @throws NoSuchElementException if this deque is empty
323      */
getLast()324     E getLast();
325 
326     /**
327      * Retrieves, but does not remove, the first element of this deque,
328      * or returns {@code null} if this deque is empty.
329      *
330      * @return the head of this deque, or {@code null} if this deque is empty
331      */
peekFirst()332     E peekFirst();
333 
334     /**
335      * Retrieves, but does not remove, the last element of this deque,
336      * or returns {@code null} if this deque is empty.
337      *
338      * @return the tail of this deque, or {@code null} if this deque is empty
339      */
peekLast()340     E peekLast();
341 
342     /**
343      * Removes the first occurrence of the specified element from this deque.
344      * If the deque does not contain the element, it is unchanged.
345      * More formally, removes the first element {@code e} such that
346      * {@code Objects.equals(o, e)} (if such an element exists).
347      * Returns {@code true} if this deque contained the specified element
348      * (or equivalently, if this deque changed as a result of the call).
349      *
350      * @param o element to be removed from this deque, if present
351      * @return {@code true} if an element was removed as a result of this call
352      * @throws ClassCastException if the class of the specified element
353      *         is incompatible with this deque
354      * (<a href="Collection.html#optional-restrictions">optional</a>)
355      * @throws NullPointerException if the specified element is null and this
356      *         deque does not permit null elements
357      * (<a href="Collection.html#optional-restrictions">optional</a>)
358      */
removeFirstOccurrence(Object o)359     boolean removeFirstOccurrence(Object o);
360 
361     /**
362      * Removes the last occurrence of the specified element from this deque.
363      * If the deque does not contain the element, it is unchanged.
364      * More formally, removes the last element {@code e} such that
365      * {@code Objects.equals(o, e)} (if such an element exists).
366      * Returns {@code true} if this deque contained the specified element
367      * (or equivalently, if this deque changed as a result of the call).
368      *
369      * @param o element to be removed from this deque, if present
370      * @return {@code true} if an element was removed as a result of this call
371      * @throws ClassCastException if the class of the specified element
372      *         is incompatible with this deque
373      * (<a href="Collection.html#optional-restrictions">optional</a>)
374      * @throws NullPointerException if the specified element is null and this
375      *         deque does not permit null elements
376      * (<a href="Collection.html#optional-restrictions">optional</a>)
377      */
removeLastOccurrence(Object o)378     boolean removeLastOccurrence(Object o);
379 
380     // *** Queue methods ***
381 
382     /**
383      * Inserts the specified element into the queue represented by this deque
384      * (in other words, at the tail of this deque) if it is possible to do so
385      * immediately without violating capacity restrictions, returning
386      * {@code true} upon success and throwing an
387      * {@code IllegalStateException} if no space is currently available.
388      * When using a capacity-restricted deque, it is generally preferable to
389      * use {@link #offer(Object) offer}.
390      *
391      * <p>This method is equivalent to {@link #addLast}.
392      *
393      * @param e the element to add
394      * @return {@code true} (as specified by {@link Collection#add})
395      * @throws IllegalStateException if the element cannot be added at this
396      *         time due to capacity restrictions
397      * @throws ClassCastException if the class of the specified element
398      *         prevents it from being added to this deque
399      * @throws NullPointerException if the specified element is null and this
400      *         deque does not permit null elements
401      * @throws IllegalArgumentException if some property of the specified
402      *         element prevents it from being added to this deque
403      */
add(E e)404     boolean add(E e);
405 
406     /**
407      * Inserts the specified element into the queue represented by this deque
408      * (in other words, at the tail of this deque) if it is possible to do so
409      * immediately without violating capacity restrictions, returning
410      * {@code true} upon success and {@code false} if no space is currently
411      * available.  When using a capacity-restricted deque, this method is
412      * generally preferable to the {@link #add} method, which can fail to
413      * insert an element only by throwing an exception.
414      *
415      * <p>This method is equivalent to {@link #offerLast}.
416      *
417      * @param e the element to add
418      * @return {@code true} if the element was added to this deque, else
419      *         {@code false}
420      * @throws ClassCastException if the class of the specified element
421      *         prevents it from being added to this deque
422      * @throws NullPointerException if the specified element is null and this
423      *         deque does not permit null elements
424      * @throws IllegalArgumentException if some property of the specified
425      *         element prevents it from being added to this deque
426      */
offer(E e)427     boolean offer(E e);
428 
429     /**
430      * Retrieves and removes the head of the queue represented by this deque
431      * (in other words, the first element of this deque).
432      * This method differs from {@link #poll poll} only in that it throws an
433      * exception if this deque is empty.
434      *
435      * <p>This method is equivalent to {@link #removeFirst()}.
436      *
437      * @return the head of the queue represented by this deque
438      * @throws NoSuchElementException if this deque is empty
439      */
remove()440     E remove();
441 
442     /**
443      * Retrieves and removes the head of the queue represented by this deque
444      * (in other words, the first element of this deque), or returns
445      * {@code null} if this deque is empty.
446      *
447      * <p>This method is equivalent to {@link #pollFirst()}.
448      *
449      * @return the first element of this deque, or {@code null} if
450      *         this deque is empty
451      */
poll()452     E poll();
453 
454     /**
455      * Retrieves, but does not remove, the head of the queue represented by
456      * this deque (in other words, the first element of this deque).
457      * This method differs from {@link #peek peek} only in that it throws an
458      * exception if this deque is empty.
459      *
460      * <p>This method is equivalent to {@link #getFirst()}.
461      *
462      * @return the head of the queue represented by this deque
463      * @throws NoSuchElementException if this deque is empty
464      */
element()465     E element();
466 
467     /**
468      * Retrieves, but does not remove, the head of the queue represented by
469      * this deque (in other words, the first element of this deque), or
470      * returns {@code null} if this deque is empty.
471      *
472      * <p>This method is equivalent to {@link #peekFirst()}.
473      *
474      * @return the head of the queue represented by this deque, or
475      *         {@code null} if this deque is empty
476      */
peek()477     E peek();
478 
479 
480     // *** Stack methods ***
481 
482     /**
483      * Pushes an element onto the stack represented by this deque (in other
484      * words, at the head of this deque) if it is possible to do so
485      * immediately without violating capacity restrictions, throwing an
486      * {@code IllegalStateException} if no space is currently available.
487      *
488      * <p>This method is equivalent to {@link #addFirst}.
489      *
490      * @param e the element to push
491      * @throws IllegalStateException if the element cannot be added at this
492      *         time due to capacity restrictions
493      * @throws ClassCastException if the class of the specified element
494      *         prevents it from being added to this deque
495      * @throws NullPointerException if the specified element is null and this
496      *         deque does not permit null elements
497      * @throws IllegalArgumentException if some property of the specified
498      *         element prevents it from being added to this deque
499      */
push(E e)500     void push(E e);
501 
502     /**
503      * Pops an element from the stack represented by this deque.  In other
504      * words, removes and returns the first element of this deque.
505      *
506      * <p>This method is equivalent to {@link #removeFirst()}.
507      *
508      * @return the element at the front of this deque (which is the top
509      *         of the stack represented by this deque)
510      * @throws NoSuchElementException if this deque is empty
511      */
pop()512     E pop();
513 
514 
515     // *** Collection methods ***
516 
517     /**
518      * Removes the first occurrence of the specified element from this deque.
519      * If the deque does not contain the element, it is unchanged.
520      * More formally, removes the first element {@code e} such that
521      * {@code Objects.equals(o, e)} (if such an element exists).
522      * Returns {@code true} if this deque contained the specified element
523      * (or equivalently, if this deque changed as a result of the call).
524      *
525      * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
526      *
527      * @param o element to be removed from this deque, if present
528      * @return {@code true} if an element was removed as a result of this call
529      * @throws ClassCastException if the class of the specified element
530      *         is incompatible with this deque
531      * (<a href="Collection.html#optional-restrictions">optional</a>)
532      * @throws NullPointerException if the specified element is null and this
533      *         deque does not permit null elements
534      * (<a href="Collection.html#optional-restrictions">optional</a>)
535      */
remove(Object o)536     boolean remove(Object o);
537 
538     /**
539      * Returns {@code true} if this deque contains the specified element.
540      * More formally, returns {@code true} if and only if this deque contains
541      * at least one element {@code e} such that {@code Objects.equals(o, e)}.
542      *
543      * @param o element whose presence in this deque is to be tested
544      * @return {@code true} if this deque contains the specified element
545      * @throws ClassCastException if the class of the specified element
546      *         is incompatible with this deque
547      * (<a href="Collection.html#optional-restrictions">optional</a>)
548      * @throws NullPointerException if the specified element is null and this
549      *         deque does not permit null elements
550      * (<a href="Collection.html#optional-restrictions">optional</a>)
551      */
contains(Object o)552     boolean contains(Object o);
553 
554     /**
555      * Returns the number of elements in this deque.
556      *
557      * @return the number of elements in this deque
558      */
size()559     int size();
560 
561     /**
562      * Returns an iterator over the elements in this deque in proper sequence.
563      * The elements will be returned in order from first (head) to last (tail).
564      *
565      * @return an iterator over the elements in this deque in proper sequence
566      */
iterator()567     Iterator<E> iterator();
568 
569     /**
570      * Returns an iterator over the elements in this deque in reverse
571      * sequential order.  The elements will be returned in order from
572      * last (tail) to first (head).
573      *
574      * @return an iterator over the elements in this deque in reverse
575      * sequence
576      */
descendingIterator()577     Iterator<E> descendingIterator();
578 
579 }
580