Deque.java revision be0e537d8953365ae5a008350461ccf777a719fb
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
35package 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 * <p>
63 * <table BORDER CELLPADDING=3 CELLSPACING=1>
64 * <caption>Summary of Deque methods</caption>
65 *  <tr>
66 *    <td></td>
67 *    <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>
68 *    <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>
69 *  </tr>
70 *  <tr>
71 *    <td></td>
72 *    <td ALIGN=CENTER><em>Throws exception</em></td>
73 *    <td ALIGN=CENTER><em>Special value</em></td>
74 *    <td ALIGN=CENTER><em>Throws exception</em></td>
75 *    <td ALIGN=CENTER><em>Special value</em></td>
76 *  </tr>
77 *  <tr>
78 *    <td><b>Insert</b></td>
79 *    <td>{@link Deque#addFirst addFirst(e)}</td>
80 *    <td>{@link Deque#offerFirst offerFirst(e)}</td>
81 *    <td>{@link Deque#addLast addLast(e)}</td>
82 *    <td>{@link Deque#offerLast offerLast(e)}</td>
83 *  </tr>
84 *  <tr>
85 *    <td><b>Remove</b></td>
86 *    <td>{@link Deque#removeFirst removeFirst()}</td>
87 *    <td>{@link Deque#pollFirst pollFirst()}</td>
88 *    <td>{@link Deque#removeLast removeLast()}</td>
89 *    <td>{@link Deque#pollLast pollLast()}</td>
90 *  </tr>
91 *  <tr>
92 *    <td><b>Examine</b></td>
93 *    <td>{@link Deque#getFirst getFirst()}</td>
94 *    <td>{@link Deque#peekFirst peekFirst()}</td>
95 *    <td>{@link Deque#getLast getLast()}</td>
96 *    <td>{@link Deque#peekLast peekLast()}</td>
97 *  </tr>
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 * <p>
107 * <table BORDER CELLPADDING=3 CELLSPACING=1>
108 * <caption>Comparison of Queue and Deque methods</caption>
109 *  <tr>
110 *    <td ALIGN=CENTER> <b>{@code Queue} Method</b></td>
111 *    <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>
112 *  </tr>
113 *  <tr>
114 *    <td>{@link java.util.Queue#add add(e)}</td>
115 *    <td>{@link #addLast addLast(e)}</td>
116 *  </tr>
117 *  <tr>
118 *    <td>{@link java.util.Queue#offer offer(e)}</td>
119 *    <td>{@link #offerLast offerLast(e)}</td>
120 *  </tr>
121 *  <tr>
122 *    <td>{@link java.util.Queue#remove remove()}</td>
123 *    <td>{@link #removeFirst removeFirst()}</td>
124 *  </tr>
125 *  <tr>
126 *    <td>{@link java.util.Queue#poll poll()}</td>
127 *    <td>{@link #pollFirst pollFirst()}</td>
128 *  </tr>
129 *  <tr>
130 *    <td>{@link java.util.Queue#element element()}</td>
131 *    <td>{@link #getFirst getFirst()}</td>
132 *  </tr>
133 *  <tr>
134 *    <td>{@link java.util.Queue#peek peek()}</td>
135 *    <td>{@link #peek peekFirst()}</td>
136 *  </tr>
137 * </table>
138 *
139 * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks.  This
140 * interface should be used in preference to the legacy {@link Stack} class.
141 * When a deque is used as a stack, elements are pushed and popped from the
142 * beginning of the deque.  Stack methods are precisely equivalent to
143 * {@code Deque} methods as indicated in the table below:
144 *
145 * <p>
146 * <table BORDER CELLPADDING=3 CELLSPACING=1>
147 * <caption>Comparison of Stack and Deque methods</caption>
148 *  <tr>
149 *    <td ALIGN=CENTER> <b>Stack Method</b></td>
150 *    <td ALIGN=CENTER> <b>Equivalent {@code Deque} Method</b></td>
151 *  </tr>
152 *  <tr>
153 *    <td>{@link #push push(e)}</td>
154 *    <td>{@link #addFirst addFirst(e)}</td>
155 *  </tr>
156 *  <tr>
157 *    <td>{@link #pop pop()}</td>
158 *    <td>{@link #removeFirst removeFirst()}</td>
159 *  </tr>
160 *  <tr>
161 *    <td>{@link #peek peek()}</td>
162 *    <td>{@link #peekFirst peekFirst()}</td>
163 *  </tr>
164 * </table>
165 *
166 * <p>Note that the {@link #peek peek} method works equally well when
167 * a deque is used as a queue or a stack; in either case, elements are
168 * drawn from the beginning of the deque.
169 *
170 * <p>This interface provides two methods to remove interior
171 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
172 * {@link #removeLastOccurrence removeLastOccurrence}.
173 *
174 * <p>Unlike the {@link List} interface, this interface does not
175 * provide support for indexed access to elements.
176 *
177 * <p>While {@code Deque} implementations are not strictly required
178 * to prohibit the insertion of null elements, they are strongly
179 * encouraged to do so.  Users of any {@code Deque} implementations
180 * that do allow null elements are strongly encouraged <i>not</i> to
181 * take advantage of the ability to insert nulls.  This is so because
182 * {@code null} is used as a special return value by various methods
183 * to indicated that the deque is empty.
184 *
185 * <p>{@code Deque} implementations generally do not define
186 * element-based versions of the {@code equals} and {@code hashCode}
187 * methods, but instead inherit the identity-based versions from class
188 * {@code Object}.
189 *
190 * @author Doug Lea
191 * @author Josh Bloch
192 * @since  1.6
193 * @param <E> the type of elements held in this collection
194 */
195public interface Deque<E> extends Queue<E> {
196    /**
197     * Inserts the specified element at the front of this deque if it is
198     * possible to do so immediately without violating capacity restrictions,
199     * throwing an {@code IllegalStateException} if no space is currently
200     * available.  When using a capacity-restricted deque, it is generally
201     * preferable to use method {@link #offerFirst}.
202     *
203     * @param e the element to add
204     * @throws IllegalStateException if the element cannot be added at this
205     *         time due to capacity restrictions
206     * @throws ClassCastException if the class of the specified element
207     *         prevents it from being added to this deque
208     * @throws NullPointerException if the specified element is null and this
209     *         deque does not permit null elements
210     * @throws IllegalArgumentException if some property of the specified
211     *         element prevents it from being added to this deque
212     */
213    void addFirst(E e);
214
215    /**
216     * Inserts the specified element at the end of this deque if it is
217     * possible to do so immediately without violating capacity restrictions,
218     * throwing an {@code IllegalStateException} if no space is currently
219     * available.  When using a capacity-restricted deque, it is generally
220     * preferable to use method {@link #offerLast}.
221     *
222     * <p>This method is equivalent to {@link #add}.
223     *
224     * @param e the element to add
225     * @throws IllegalStateException if the element cannot be added at this
226     *         time due to capacity restrictions
227     * @throws ClassCastException if the class of the specified element
228     *         prevents it from being added to this deque
229     * @throws NullPointerException if the specified element is null and this
230     *         deque does not permit null elements
231     * @throws IllegalArgumentException if some property of the specified
232     *         element prevents it from being added to this deque
233     */
234    void addLast(E e);
235
236    /**
237     * Inserts the specified element at the front of this deque unless it would
238     * violate capacity restrictions.  When using a capacity-restricted deque,
239     * this method is generally preferable to the {@link #addFirst} method,
240     * which can fail to insert an element only by throwing an exception.
241     *
242     * @param e the element to add
243     * @return {@code true} if the element was added to this deque, else
244     *         {@code false}
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 and this
248     *         deque does not permit null elements
249     * @throws IllegalArgumentException if some property of the specified
250     *         element prevents it from being added to this deque
251     */
252    boolean offerFirst(E e);
253
254    /**
255     * Inserts the specified element at the end of this deque unless it would
256     * violate capacity restrictions.  When using a capacity-restricted deque,
257     * this method is generally preferable to the {@link #addLast} method,
258     * which can fail to insert an element only by throwing an exception.
259     *
260     * @param e the element to add
261     * @return {@code true} if the element was added to this deque, else
262     *         {@code false}
263     * @throws ClassCastException if the class of the specified element
264     *         prevents it from being added to this deque
265     * @throws NullPointerException if the specified element is null and this
266     *         deque does not permit null elements
267     * @throws IllegalArgumentException if some property of the specified
268     *         element prevents it from being added to this deque
269     */
270    boolean offerLast(E e);
271
272    /**
273     * Retrieves and removes the first element of this deque.  This method
274     * differs from {@link #pollFirst pollFirst} only in that it throws an
275     * exception if this deque is empty.
276     *
277     * @return the head of this deque
278     * @throws NoSuchElementException if this deque is empty
279     */
280    E removeFirst();
281
282    /**
283     * Retrieves and removes the last element of this deque.  This method
284     * differs from {@link #pollLast pollLast} only in that it throws an
285     * exception if this deque is empty.
286     *
287     * @return the tail of this deque
288     * @throws NoSuchElementException if this deque is empty
289     */
290    E removeLast();
291
292    /**
293     * Retrieves and removes the first element of this deque,
294     * or returns {@code null} if this deque is empty.
295     *
296     * @return the head of this deque, or {@code null} if this deque is empty
297     */
298    E pollFirst();
299
300    /**
301     * Retrieves and removes the last element of this deque,
302     * or returns {@code null} if this deque is empty.
303     *
304     * @return the tail of this deque, or {@code null} if this deque is empty
305     */
306    E pollLast();
307
308    /**
309     * Retrieves, but does not remove, the first element of this deque.
310     *
311     * This method differs from {@link #peekFirst peekFirst} only in that it
312     * throws an exception if this deque is empty.
313     *
314     * @return the head of this deque
315     * @throws NoSuchElementException if this deque is empty
316     */
317    E getFirst();
318
319    /**
320     * Retrieves, but does not remove, the last element of this deque.
321     * This method differs from {@link #peekLast peekLast} only in that it
322     * throws an exception if this deque is empty.
323     *
324     * @return the tail of this deque
325     * @throws NoSuchElementException if this deque is empty
326     */
327    E getLast();
328
329    /**
330     * Retrieves, but does not remove, the first element of this deque,
331     * or returns {@code null} if this deque is empty.
332     *
333     * @return the head of this deque, or {@code null} if this deque is empty
334     */
335    E peekFirst();
336
337    /**
338     * Retrieves, but does not remove, the last element of this deque,
339     * or returns {@code null} if this deque is empty.
340     *
341     * @return the tail of this deque, or {@code null} if this deque is empty
342     */
343    E peekLast();
344
345    /**
346     * Removes the first occurrence of the specified element from this deque.
347     * If the deque does not contain the element, it is unchanged.
348     * More formally, removes the first element {@code e} such that
349     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
350     * (if such an element exists).
351     * Returns {@code true} if this deque contained the specified element
352     * (or equivalently, if this deque changed as a result of the call).
353     *
354     * @param o element to be removed from this deque, if present
355     * @return {@code true} if an element was removed as a result of this call
356     * @throws ClassCastException if the class of the specified element
357     *         is incompatible with this deque (optional)
358     * @throws NullPointerException if the specified element is null and this
359     *         deque does not permit null elements (optional)
360     */
361    boolean removeFirstOccurrence(Object o);
362
363    /**
364     * Removes the last occurrence of the specified element from this deque.
365     * If the deque does not contain the element, it is unchanged.
366     * More formally, removes the last element {@code e} such that
367     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
368     * (if such an element exists).
369     * Returns {@code true} if this deque contained the specified element
370     * (or equivalently, if this deque changed as a result of the call).
371     *
372     * @param o element to be removed from this deque, if present
373     * @return {@code true} if an element was removed as a result of this call
374     * @throws ClassCastException if the class of the specified element
375     *         is incompatible with this deque (optional)
376     * @throws NullPointerException if the specified element is null and this
377     *         deque does not permit null elements (optional)
378     */
379    boolean removeLastOccurrence(Object o);
380
381    // *** Queue methods ***
382
383    /**
384     * Inserts the specified element into the queue represented by this deque
385     * (in other words, at the tail of this deque) if it is possible to do so
386     * immediately without violating capacity restrictions, returning
387     * {@code true} upon success and throwing an
388     * {@code IllegalStateException} if no space is currently available.
389     * When using a capacity-restricted deque, it is generally preferable to
390     * use {@link #offer(Object) offer}.
391     *
392     * <p>This method is equivalent to {@link #addLast}.
393     *
394     * @param e the element to add
395     * @return {@code true} (as specified by {@link Collection#add})
396     * @throws IllegalStateException if the element cannot be added at this
397     *         time due to capacity restrictions
398     * @throws ClassCastException if the class of the specified element
399     *         prevents it from being added to this deque
400     * @throws NullPointerException if the specified element is null and this
401     *         deque does not permit null elements
402     * @throws IllegalArgumentException if some property of the specified
403     *         element prevents it from being added to this deque
404     */
405    boolean add(E e);
406
407    /**
408     * Inserts the specified element into the queue represented by this deque
409     * (in other words, at the tail of this deque) if it is possible to do so
410     * immediately without violating capacity restrictions, returning
411     * {@code true} upon success and {@code false} if no space is currently
412     * available.  When using a capacity-restricted deque, this method is
413     * generally preferable to the {@link #add} method, which can fail to
414     * insert an element only by throwing an exception.
415     *
416     * <p>This method is equivalent to {@link #offerLast}.
417     *
418     * @param e the element to add
419     * @return {@code true} if the element was added to this deque, else
420     *         {@code false}
421     * @throws ClassCastException if the class of the specified element
422     *         prevents it from being added to this deque
423     * @throws NullPointerException if the specified element is null and this
424     *         deque does not permit null elements
425     * @throws IllegalArgumentException if some property of the specified
426     *         element prevents it from being added to this deque
427     */
428    boolean offer(E e);
429
430    /**
431     * Retrieves and removes the head of the queue represented by this deque
432     * (in other words, the first element of this deque).
433     * This method differs from {@link #poll poll} only in that it throws an
434     * exception if this deque is empty.
435     *
436     * <p>This method is equivalent to {@link #removeFirst()}.
437     *
438     * @return the head of the queue represented by this deque
439     * @throws NoSuchElementException if this deque is empty
440     */
441    E remove();
442
443    /**
444     * Retrieves and removes the head of the queue represented by this deque
445     * (in other words, the first element of this deque), or returns
446     * {@code null} if this deque is empty.
447     *
448     * <p>This method is equivalent to {@link #pollFirst()}.
449     *
450     * @return the first element of this deque, or {@code null} if
451     *         this deque is empty
452     */
453    E poll();
454
455    /**
456     * Retrieves, but does not remove, the head of the queue represented by
457     * this deque (in other words, the first element of this deque).
458     * This method differs from {@link #peek peek} only in that it throws an
459     * exception if this deque is empty.
460     *
461     * <p>This method is equivalent to {@link #getFirst()}.
462     *
463     * @return the head of the queue represented by this deque
464     * @throws NoSuchElementException if this deque is empty
465     */
466    E element();
467
468    /**
469     * Retrieves, but does not remove, the head of the queue represented by
470     * this deque (in other words, the first element of this deque), or
471     * returns {@code null} if this deque is empty.
472     *
473     * <p>This method is equivalent to {@link #peekFirst()}.
474     *
475     * @return the head of the queue represented by this deque, or
476     *         {@code null} if this deque is empty
477     */
478    E peek();
479
480
481    // *** Stack methods ***
482
483    /**
484     * Pushes an element onto the stack represented by this deque (in other
485     * words, at the head of this deque) if it is possible to do so
486     * immediately without violating capacity restrictions, throwing an
487     * {@code IllegalStateException} if no space is currently available.
488     *
489     * <p>This method is equivalent to {@link #addFirst}.
490     *
491     * @param e the element to push
492     * @throws IllegalStateException if the element cannot be added at this
493     *         time due to capacity restrictions
494     * @throws ClassCastException if the class of the specified element
495     *         prevents it from being added to this deque
496     * @throws NullPointerException if the specified element is null and this
497     *         deque does not permit null elements
498     * @throws IllegalArgumentException if some property of the specified
499     *         element prevents it from being added to this deque
500     */
501    void push(E e);
502
503    /**
504     * Pops an element from the stack represented by this deque.  In other
505     * words, removes and returns the first element of this deque.
506     *
507     * <p>This method is equivalent to {@link #removeFirst()}.
508     *
509     * @return the element at the front of this deque (which is the top
510     *         of the stack represented by this deque)
511     * @throws NoSuchElementException if this deque is empty
512     */
513    E pop();
514
515
516    // *** Collection methods ***
517
518    /**
519     * Removes the first occurrence of the specified element from this deque.
520     * If the deque does not contain the element, it is unchanged.
521     * More formally, removes the first element {@code e} such that
522     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
523     * (if such an element exists).
524     * Returns {@code true} if this deque contained the specified element
525     * (or equivalently, if this deque changed as a result of the call).
526     *
527     * <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
528     *
529     * @param o element to be removed from this deque, if present
530     * @return {@code true} if an element was removed as a result of this call
531     * @throws ClassCastException if the class of the specified element
532     *         is incompatible with this deque (optional)
533     * @throws NullPointerException if the specified element is null and this
534     *         deque does not permit null elements (optional)
535     */
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
542     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
543     *
544     * @param o element whose presence in this deque is to be tested
545     * @return {@code true} if this deque contains the specified element
546     * @throws ClassCastException if the type of the specified element
547     *         is incompatible with this deque (optional)
548     * @throws NullPointerException if the specified element is null and this
549     *         deque does not permit null elements (optional)
550     */
551    boolean contains(Object o);
552
553    /**
554     * Returns the number of elements in this deque.
555     *
556     * @return the number of elements in this deque
557     */
558    public int size();
559
560    /**
561     * Returns an iterator over the elements in this deque in proper sequence.
562     * The elements will be returned in order from first (head) to last (tail).
563     *
564     * @return an iterator over the elements in this deque in proper sequence
565     */
566    Iterator<E> iterator();
567
568    /**
569     * Returns an iterator over the elements in this deque in reverse
570     * sequential order.  The elements will be returned in order from
571     * last (tail) to first (head).
572     *
573     * @return an iterator over the elements in this deque in reverse
574     * sequence
575     */
576    Iterator<E> descendingIterator();
577
578}
579