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