/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/licenses/publicdomain
*/
package jsr166x;
import java.util.*;
/**
* A linear collection in which elements may be inserted and removed
* from both the beginning and end. A Deque (short for
* "double ended queue") provides uniformly named methods to
* get, peek, poll, remove,
* offer, and add the first and
* last element of the collection (for example, methods
* addFirst, pollLast). Unlike interface {@link
* List} the Deque interface does not define support for indexed
* operations or sublists.
*
*
A view of a subset of Deque operations can be obtained using
* method {@link #asFifoQueue} to support only Last-In-First-Out (LIFO)
* stack behavior, as well as method {@link #asFifoQueue} to support only
* First-in-First-Out (FIFO) queue behavior. More commonly, a Deque
* is used when various mixtures of LIFO and FIFO operations are
* required.
*
*
Deques additionally provide a few methods to remove elements
* embedded within a deque, proceding from either direction using
* removeFirstOccurrence and removeLastOccurrence.
* They also support {@link Collection} operations including
* contains, iterator, and so on.
*
*
The {@link #offerFirst} and {@link #offerLast} methods insert an
* element if possible, otherwise returning false. They
* differ from {@link java.util.Collection#add Collection.add}, as
* well as {@link #addFirst} and {@link #addLast} methods, which can
* fail to add an element only by throwing an unchecked exception.
* The offer methods are designed for use when failure is a
* normal, rather than exceptional occurrence, for example, in
* fixed-capacity (or "bounded") deques.
*
*
Deque implementations generally do not allow insertion
* of null elements. Even in implementations that permit it,
* null should not be inserted into a Deque, as
* null is also used as a special return value by the poll
* methods to indicate that the deque contains no elements.
*
*
Deque implementations generally do not define
* element-based versions of methods equals and
* hashCode but instead inherit the identity based versions
* from class Object.
*
*
This interface is a member of the Java Collections
* Framework.
*
* @author Doug Lea
* @param the type of elements held in this collection
*/
public interface Deque extends Collection {
/**
* Inserts the specified element to the front this deque, if
* possible. When using deques that may impose insertion
* restrictions (for example capacity bounds), method
* offerFirst is generally preferable to method
* addFirst which can fail to insert a non-duplicate
* element only by throwing an exception.
*
* @param o the element to insert.
* @return true if it was possible to add the element to
* this deque, else false
*/
boolean offerFirst(E o);
/**
* Inserts the specified element to the end this deque, if
* possible. When using deques that may impose insertion
* restrictions (for example capacity bounds), method
* offerFirst is generally preferable to method
* addLast which can fail to insert a non-duplicate
* element only by throwing an exception.
*
* @param o the element to insert.
* @return true if it was possible to add the element to
* this deque, else false
*/
boolean offerLast(E o);
/**
* Inserts the specified element to the front this deque, if
* this deque permits insertion of the given element. This
* method has the same semantics as {@link Collection#add}.
*
* @param o the element to insert.
* @return true if this deque changed as a result of this
* call, else false
*/
boolean addFirst(E o);
/**
* Inserts the specified element to the end this deque, if
* this deque permits insertion of the given element. This
* method has the same semantics as {@link Collection#add}.
*
* @param o the element to insert.
* @return true if this deque changed as a result of this
* call, else false
*/
boolean addLast(E o);
/**
* Retrieves and removes the first element of this deque, or
* null if this deque is empty.
*
* @return the first element of this deque, or null if
* this deque is empty.
*/
E pollFirst();
/**
* Retrieves and removes the last element of this deque, or
* null if this deque is empty.
*
* @return the last element of this deque, or null if
* this deque is empty.
*/
E pollLast();
/**
* Retrieves and removes the first element of this deque. This method
* differs from the pollFirst method in that it throws an
* exception if this deque is empty.
*
* @return the first element of this deque.
* @throws NoSuchElementException if this deque is empty.
*/
E removeFirst();
/**
* Retrieves and removes the last element of this deque. This method
* differs from the pollLast method in that it throws an
* exception if this deque is empty.
*
* @return the last element of this deque.
* @throws NoSuchElementException if this deque is empty.
*/
E removeLast();
/**
* Retrieves, but does not remove, the first element of this deque,
* returning null if this deque is empty.
*
* @return the first element of this deque, or null if
* this deque is empty.
*/
E peekFirst();
/**
* Retrieves, but does not remove, the last element of this deque,
* returning null if this deque is empty.
*
* @return the last element of this deque, or null if this deque
* is empty.
*/
E peekLast();
/**
* Retrieves, but does not remove, the first element of this
* deque. This method differs from the peek method only
* in that it throws an exception if this deque is empty.
*
* @return the first element of this deque.
* @throws NoSuchElementException if this deque is empty.
*/
E getFirst();
/**
* Retrieves, but does not remove, the last element of this
* deque. This method differs from the peek method only
* in that it throws an exception if this deque is empty.
*
* @return the last element of this deque.
* @throws NoSuchElementException if this deque is empty.
*/
E getLast();
/**
* Removes the first occurrence of the specified element in this
* deque. If the deque does not contain the element, it is
* unchanged. More formally, removes the first element e
* such that (o==null ? e==null : o.equals(e)) (if
* such an element exists).
*
* @param o element to be removed from this deque, if present.
* @return true if the deque contained the specified element.
* @throws NullPointerException if the specified element is null
*/
boolean removeFirstOccurrence(E o);
/**
* Removes the last occurrence of the specified element in this
* deque. If the deque does not contain the element, it is
* unchanged. More formally, removes the last element e
* such that (o==null ? e==null : o.equals(e)) (if
* such an element exists).
*
* @param o element to be removed from this deque, if present.
* @return true if the deque contained the specified element.
* @throws NullPointerException if the specified element is null
*/
boolean removeLastOccurrence(E o);
/**
* Returns a view of this deque as a first-in-first-out queue,
* mapping {@link Queue#offer} to offerLast, {@link
* Queue#poll} to pollFirst, and other operations
* accordingly.
* @return a first-in-first-out view of this deque.
*/
public Queue asFifoQueue();
/**
* Returns a view of this deque as a last-in-first-out stack,
* mapping {@link Queue#offer} to offerFirst, {@link
* Queue#poll} to pollFirst, and other operations
* accordingly.
* @return a first-in-last-out view of this deque.
*/
public Queue asLifoQueue();
}