jaroslav@597: /* jaroslav@597: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@597: * jaroslav@597: * This code is free software; you can redistribute it and/or modify it jaroslav@597: * under the terms of the GNU General Public License version 2 only, as jaroslav@597: * published by the Free Software Foundation. Oracle designates this jaroslav@597: * particular file as subject to the "Classpath" exception as provided jaroslav@597: * by Oracle in the LICENSE file that accompanied this code. jaroslav@597: * jaroslav@597: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@597: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@597: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@597: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@597: * accompanied this code). jaroslav@597: * jaroslav@597: * You should have received a copy of the GNU General Public License version jaroslav@597: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@597: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@597: * jaroslav@597: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@597: * or visit www.oracle.com if you need additional information or have any jaroslav@597: * questions. jaroslav@597: */ jaroslav@597: jaroslav@597: /* jaroslav@597: * This file is available under and governed by the GNU General Public jaroslav@597: * License version 2 only, as published by the Free Software Foundation. jaroslav@597: * However, the following notice accompanied the original version of this jaroslav@597: * file: jaroslav@597: * jaroslav@597: * Written by Doug Lea and Josh Bloch with assistance from members of jaroslav@597: * JCP JSR-166 Expert Group and released to the public domain, as explained jaroslav@597: * at http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@597: */ jaroslav@597: jaroslav@597: package java.util; jaroslav@597: jaroslav@597: /** jaroslav@597: * A linear collection that supports element insertion and removal at jaroslav@597: * both ends. The name deque is short for "double ended queue" jaroslav@597: * and is usually pronounced "deck". Most Deque jaroslav@597: * implementations place no fixed limits on the number of elements jaroslav@597: * they may contain, but this interface supports capacity-restricted jaroslav@597: * deques as well as those with no fixed size limit. jaroslav@597: * jaroslav@597: *
This interface defines methods to access the elements at both jaroslav@597: * ends of the deque. Methods are provided to insert, remove, and jaroslav@597: * examine the element. Each of these methods exists in two forms: jaroslav@597: * one throws an exception if the operation fails, the other returns a jaroslav@597: * special value (either null or false, depending on jaroslav@597: * the operation). The latter form of the insert operation is jaroslav@597: * designed specifically for use with capacity-restricted jaroslav@597: * Deque implementations; in most implementations, insert jaroslav@597: * operations cannot fail. jaroslav@597: * jaroslav@597: *
The twelve methods described above are summarized in the jaroslav@597: * following table: jaroslav@597: * jaroslav@597: *
jaroslav@597: *
jaroslav@597: * | First Element (Head) | jaroslav@597: *Last Element (Tail) | jaroslav@597: *||
jaroslav@597: * | Throws exception | jaroslav@597: *Special value | jaroslav@597: *Throws exception | jaroslav@597: *Special value | jaroslav@597: *
Insert | jaroslav@597: *{@link #addFirst addFirst(e)} | jaroslav@597: *{@link #offerFirst offerFirst(e)} | jaroslav@597: *{@link #addLast addLast(e)} | jaroslav@597: *{@link #offerLast offerLast(e)} | jaroslav@597: *
Remove | jaroslav@597: *{@link #removeFirst removeFirst()} | jaroslav@597: *{@link #pollFirst pollFirst()} | jaroslav@597: *{@link #removeLast removeLast()} | jaroslav@597: *{@link #pollLast pollLast()} | jaroslav@597: *
Examine | jaroslav@597: *{@link #getFirst getFirst()} | jaroslav@597: *{@link #peekFirst peekFirst()} | jaroslav@597: *{@link #getLast getLast()} | jaroslav@597: *{@link #peekLast peekLast()} | jaroslav@597: *
This interface extends the {@link Queue} interface. When a deque is jaroslav@597: * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are jaroslav@597: * added at the end of the deque and removed from the beginning. The methods jaroslav@597: * inherited from the Queue interface are precisely equivalent to jaroslav@597: * Deque methods as indicated in the following table: jaroslav@597: * jaroslav@597: *
jaroslav@597: *
Queue Method | jaroslav@597: *Equivalent Deque Method | jaroslav@597: *
{@link java.util.Queue#add add(e)} | jaroslav@597: *{@link #addLast addLast(e)} | jaroslav@597: *
{@link java.util.Queue#offer offer(e)} | jaroslav@597: *{@link #offerLast offerLast(e)} | jaroslav@597: *
{@link java.util.Queue#remove remove()} | jaroslav@597: *{@link #removeFirst removeFirst()} | jaroslav@597: *
{@link java.util.Queue#poll poll()} | jaroslav@597: *{@link #pollFirst pollFirst()} | jaroslav@597: *
{@link java.util.Queue#element element()} | jaroslav@597: *{@link #getFirst getFirst()} | jaroslav@597: *
{@link java.util.Queue#peek peek()} | jaroslav@597: *{@link #peek peekFirst()} | jaroslav@597: *
Deques can also be used as LIFO (Last-In-First-Out) stacks. This jaroslav@597: * interface should be used in preference to the legacy {@link Stack} class. jaroslav@597: * When a deque is used as a stack, elements are pushed and popped from the jaroslav@597: * beginning of the deque. Stack methods are precisely equivalent to jaroslav@597: * Deque methods as indicated in the table below: jaroslav@597: * jaroslav@597: *
jaroslav@597: *
Stack Method | jaroslav@597: *Equivalent Deque Method | jaroslav@597: *
{@link #push push(e)} | jaroslav@597: *{@link #addFirst addFirst(e)} | jaroslav@597: *
{@link #pop pop()} | jaroslav@597: *{@link #removeFirst removeFirst()} | jaroslav@597: *
{@link #peek peek()} | jaroslav@597: *{@link #peekFirst peekFirst()} | jaroslav@597: *
Note that the {@link #peek peek} method works equally well when jaroslav@597: * a deque is used as a queue or a stack; in either case, elements are jaroslav@597: * drawn from the beginning of the deque. jaroslav@597: * jaroslav@597: *
This interface provides two methods to remove interior jaroslav@597: * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and jaroslav@597: * {@link #removeLastOccurrence removeLastOccurrence}. jaroslav@597: * jaroslav@597: *
Unlike the {@link List} interface, this interface does not jaroslav@597: * provide support for indexed access to elements. jaroslav@597: * jaroslav@597: *
While Deque implementations are not strictly required jaroslav@597: * to prohibit the insertion of null elements, they are strongly jaroslav@597: * encouraged to do so. Users of any Deque implementations jaroslav@597: * that do allow null elements are strongly encouraged not to jaroslav@597: * take advantage of the ability to insert nulls. This is so because jaroslav@597: * null is used as a special return value by various methods jaroslav@597: * to indicated that the deque is empty. jaroslav@597: * jaroslav@597: *
Deque implementations generally do not define jaroslav@597: * element-based versions of the equals and hashCode jaroslav@597: * methods, but instead inherit the identity-based versions from class jaroslav@597: * Object. jaroslav@597: * jaroslav@597: *
This interface is a member of the Java Collections
jaroslav@597: * Framework.
jaroslav@597: *
jaroslav@597: * @author Doug Lea
jaroslav@597: * @author Josh Bloch
jaroslav@597: * @since 1.6
jaroslav@597: * @param This method is equivalent to {@link #add}.
jaroslav@597: *
jaroslav@597: * @param e the element to add
jaroslav@597: * @throws IllegalStateException if the element cannot be added at this
jaroslav@597: * time due to capacity restrictions
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * prevents it from being added to this deque
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * @throws IllegalArgumentException if some property of the specified
jaroslav@597: * element prevents it from being added to this deque
jaroslav@597: */
jaroslav@597: void addLast(E e);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Inserts the specified element at the front of this deque unless it would
jaroslav@597: * violate capacity restrictions. When using a capacity-restricted deque,
jaroslav@597: * this method is generally preferable to the {@link #addFirst} method,
jaroslav@597: * which can fail to insert an element only by throwing an exception.
jaroslav@597: *
jaroslav@597: * @param e the element to add
jaroslav@597: * @return true if the element was added to this deque, else
jaroslav@597: * false
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * prevents it from being added to this deque
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * @throws IllegalArgumentException if some property of the specified
jaroslav@597: * element prevents it from being added to this deque
jaroslav@597: */
jaroslav@597: boolean offerFirst(E e);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Inserts the specified element at the end of this deque unless it would
jaroslav@597: * violate capacity restrictions. When using a capacity-restricted deque,
jaroslav@597: * this method is generally preferable to the {@link #addLast} method,
jaroslav@597: * which can fail to insert an element only by throwing an exception.
jaroslav@597: *
jaroslav@597: * @param e the element to add
jaroslav@597: * @return true if the element was added to this deque, else
jaroslav@597: * false
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * prevents it from being added to this deque
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * @throws IllegalArgumentException if some property of the specified
jaroslav@597: * element prevents it from being added to this deque
jaroslav@597: */
jaroslav@597: boolean offerLast(E e);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves and removes the first element of this deque. This method
jaroslav@597: * differs from {@link #pollFirst pollFirst} only in that it throws an
jaroslav@597: * exception if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the head of this deque
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E removeFirst();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves and removes the last element of this deque. This method
jaroslav@597: * differs from {@link #pollLast pollLast} only in that it throws an
jaroslav@597: * exception if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the tail of this deque
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E removeLast();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves and removes the first element of this deque,
jaroslav@597: * or returns null if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the head of this deque, or null if this deque is empty
jaroslav@597: */
jaroslav@597: E pollFirst();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves and removes the last element of this deque,
jaroslav@597: * or returns null if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the tail of this deque, or null if this deque is empty
jaroslav@597: */
jaroslav@597: E pollLast();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves, but does not remove, the first element of this deque.
jaroslav@597: *
jaroslav@597: * This method differs from {@link #peekFirst peekFirst} only in that it
jaroslav@597: * throws an exception if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the head of this deque
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E getFirst();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves, but does not remove, the last element of this deque.
jaroslav@597: * This method differs from {@link #peekLast peekLast} only in that it
jaroslav@597: * throws an exception if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the tail of this deque
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E getLast();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves, but does not remove, the first element of this deque,
jaroslav@597: * or returns null if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the head of this deque, or null if this deque is empty
jaroslav@597: */
jaroslav@597: E peekFirst();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves, but does not remove, the last element of this deque,
jaroslav@597: * or returns null if this deque is empty.
jaroslav@597: *
jaroslav@597: * @return the tail of this deque, or null if this deque is empty
jaroslav@597: */
jaroslav@597: E peekLast();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Removes the first occurrence of the specified element from this deque.
jaroslav@597: * If the deque does not contain the element, it is unchanged.
jaroslav@597: * More formally, removes the first element e such that
jaroslav@597: * (o==null ? e==null : o.equals(e))
jaroslav@597: * (if such an element exists).
jaroslav@597: * Returns true if this deque contained the specified element
jaroslav@597: * (or equivalently, if this deque changed as a result of the call).
jaroslav@597: *
jaroslav@597: * @param o element to be removed from this deque, if present
jaroslav@597: * @return true if an element was removed as a result of this call
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * is incompatible with this deque
jaroslav@597: * (optional)
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * (optional)
jaroslav@597: */
jaroslav@597: boolean removeFirstOccurrence(Object o);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Removes the last occurrence of the specified element from this deque.
jaroslav@597: * If the deque does not contain the element, it is unchanged.
jaroslav@597: * More formally, removes the last element e such that
jaroslav@597: * (o==null ? e==null : o.equals(e))
jaroslav@597: * (if such an element exists).
jaroslav@597: * Returns true if this deque contained the specified element
jaroslav@597: * (or equivalently, if this deque changed as a result of the call).
jaroslav@597: *
jaroslav@597: * @param o element to be removed from this deque, if present
jaroslav@597: * @return true if an element was removed as a result of this call
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * is incompatible with this deque
jaroslav@597: * (optional)
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * (optional)
jaroslav@597: */
jaroslav@597: boolean removeLastOccurrence(Object o);
jaroslav@597:
jaroslav@597: // *** Queue methods ***
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Inserts the specified element into the queue represented by this deque
jaroslav@597: * (in other words, at the tail of this deque) if it is possible to do so
jaroslav@597: * immediately without violating capacity restrictions, returning
jaroslav@597: * true upon success and throwing an
jaroslav@597: * IllegalStateException if no space is currently available.
jaroslav@597: * When using a capacity-restricted deque, it is generally preferable to
jaroslav@597: * use {@link #offer(Object) offer}.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #addLast}.
jaroslav@597: *
jaroslav@597: * @param e the element to add
jaroslav@597: * @return true (as specified by {@link Collection#add})
jaroslav@597: * @throws IllegalStateException if the element cannot be added at this
jaroslav@597: * time due to capacity restrictions
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * prevents it from being added to this deque
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * @throws IllegalArgumentException if some property of the specified
jaroslav@597: * element prevents it from being added to this deque
jaroslav@597: */
jaroslav@597: boolean add(E e);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Inserts the specified element into the queue represented by this deque
jaroslav@597: * (in other words, at the tail of this deque) if it is possible to do so
jaroslav@597: * immediately without violating capacity restrictions, returning
jaroslav@597: * true upon success and false if no space is currently
jaroslav@597: * available. When using a capacity-restricted deque, this method is
jaroslav@597: * generally preferable to the {@link #add} method, which can fail to
jaroslav@597: * insert an element only by throwing an exception.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #offerLast}.
jaroslav@597: *
jaroslav@597: * @param e the element to add
jaroslav@597: * @return true if the element was added to this deque, else
jaroslav@597: * false
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * prevents it from being added to this deque
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * @throws IllegalArgumentException if some property of the specified
jaroslav@597: * element prevents it from being added to this deque
jaroslav@597: */
jaroslav@597: boolean offer(E e);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves and removes the head of the queue represented by this deque
jaroslav@597: * (in other words, the first element of this deque).
jaroslav@597: * This method differs from {@link #poll poll} only in that it throws an
jaroslav@597: * exception if this deque is empty.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #removeFirst()}.
jaroslav@597: *
jaroslav@597: * @return the head of the queue represented by this deque
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E remove();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves and removes the head of the queue represented by this deque
jaroslav@597: * (in other words, the first element of this deque), or returns
jaroslav@597: * null if this deque is empty.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #pollFirst()}.
jaroslav@597: *
jaroslav@597: * @return the first element of this deque, or null if
jaroslav@597: * this deque is empty
jaroslav@597: */
jaroslav@597: E poll();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves, but does not remove, the head of the queue represented by
jaroslav@597: * this deque (in other words, the first element of this deque).
jaroslav@597: * This method differs from {@link #peek peek} only in that it throws an
jaroslav@597: * exception if this deque is empty.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #getFirst()}.
jaroslav@597: *
jaroslav@597: * @return the head of the queue represented by this deque
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E element();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Retrieves, but does not remove, the head of the queue represented by
jaroslav@597: * this deque (in other words, the first element of this deque), or
jaroslav@597: * returns null if this deque is empty.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #peekFirst()}.
jaroslav@597: *
jaroslav@597: * @return the head of the queue represented by this deque, or
jaroslav@597: * null if this deque is empty
jaroslav@597: */
jaroslav@597: E peek();
jaroslav@597:
jaroslav@597:
jaroslav@597: // *** Stack methods ***
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Pushes an element onto the stack represented by this deque (in other
jaroslav@597: * words, at the head of this deque) if it is possible to do so
jaroslav@597: * immediately without violating capacity restrictions, returning
jaroslav@597: * true upon success and throwing an
jaroslav@597: * IllegalStateException if no space is currently available.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #addFirst}.
jaroslav@597: *
jaroslav@597: * @param e the element to push
jaroslav@597: * @throws IllegalStateException if the element cannot be added at this
jaroslav@597: * time due to capacity restrictions
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * prevents it from being added to this deque
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * @throws IllegalArgumentException if some property of the specified
jaroslav@597: * element prevents it from being added to this deque
jaroslav@597: */
jaroslav@597: void push(E e);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Pops an element from the stack represented by this deque. In other
jaroslav@597: * words, removes and returns the first element of this deque.
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #removeFirst()}.
jaroslav@597: *
jaroslav@597: * @return the element at the front of this deque (which is the top
jaroslav@597: * of the stack represented by this deque)
jaroslav@597: * @throws NoSuchElementException if this deque is empty
jaroslav@597: */
jaroslav@597: E pop();
jaroslav@597:
jaroslav@597:
jaroslav@597: // *** Collection methods ***
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Removes the first occurrence of the specified element from this deque.
jaroslav@597: * If the deque does not contain the element, it is unchanged.
jaroslav@597: * More formally, removes the first element e such that
jaroslav@597: * (o==null ? e==null : o.equals(e))
jaroslav@597: * (if such an element exists).
jaroslav@597: * Returns true if this deque contained the specified element
jaroslav@597: * (or equivalently, if this deque changed as a result of the call).
jaroslav@597: *
jaroslav@597: * This method is equivalent to {@link #removeFirstOccurrence}.
jaroslav@597: *
jaroslav@597: * @param o element to be removed from this deque, if present
jaroslav@597: * @return true if an element was removed as a result of this call
jaroslav@597: * @throws ClassCastException if the class of the specified element
jaroslav@597: * is incompatible with this deque
jaroslav@597: * (optional)
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * (optional)
jaroslav@597: */
jaroslav@597: boolean remove(Object o);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns true if this deque contains the specified element.
jaroslav@597: * More formally, returns true if and only if this deque contains
jaroslav@597: * at least one element e such that
jaroslav@597: * (o==null ? e==null : o.equals(e)).
jaroslav@597: *
jaroslav@597: * @param o element whose presence in this deque is to be tested
jaroslav@597: * @return true if this deque contains the specified element
jaroslav@597: * @throws ClassCastException if the type of the specified element
jaroslav@597: * is incompatible with this deque
jaroslav@597: * (optional)
jaroslav@597: * @throws NullPointerException if the specified element is null and this
jaroslav@597: * deque does not permit null elements
jaroslav@597: * (optional)
jaroslav@597: */
jaroslav@597: boolean contains(Object o);
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns the number of elements in this deque.
jaroslav@597: *
jaroslav@597: * @return the number of elements in this deque
jaroslav@597: */
jaroslav@597: public int size();
jaroslav@597:
jaroslav@597: /**
jaroslav@597: * Returns an iterator over the elements in this deque in proper sequence.
jaroslav@597: * The elements will be returned in order from first (head) to last (tail).
jaroslav@597: *
jaroslav@597: * @return an iterator over the elements in this deque in proper sequence
jaroslav@597: */
jaroslav@597: Iterator