jaroslav@1890: /* jaroslav@1890: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1890: * jaroslav@1890: * This code is free software; you can redistribute it and/or modify it jaroslav@1890: * under the terms of the GNU General Public License version 2 only, as jaroslav@1890: * published by the Free Software Foundation. Oracle designates this jaroslav@1890: * particular file as subject to the "Classpath" exception as provided jaroslav@1890: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1890: * jaroslav@1890: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1890: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1890: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1890: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1890: * accompanied this code). jaroslav@1890: * jaroslav@1890: * You should have received a copy of the GNU General Public License version jaroslav@1890: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1890: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1890: * jaroslav@1890: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1890: * or visit www.oracle.com if you need additional information or have any jaroslav@1890: * questions. jaroslav@1890: */ jaroslav@1890: jaroslav@1890: /* jaroslav@1890: * This file is available under and governed by the GNU General Public jaroslav@1890: * License version 2 only, as published by the Free Software Foundation. jaroslav@1890: * However, the following notice accompanied the original version of this jaroslav@1890: * file: jaroslav@1890: * jaroslav@1890: * Written by Doug Lea with assistance from members of JCP JSR-166 jaroslav@1890: * Expert Group and released to the public domain, as explained at jaroslav@1890: * http://creativecommons.org/publicdomain/zero/1.0/ jaroslav@1890: */ jaroslav@1890: jaroslav@1890: package java.util.concurrent; jaroslav@1890: import java.util.*; jaroslav@1890: jaroslav@1890: /** jaroslav@1890: * A {@link Deque} that additionally supports blocking operations that wait jaroslav@1890: * for the deque to become non-empty when retrieving an element, and wait for jaroslav@1890: * space to become available in the deque when storing an element. jaroslav@1890: * jaroslav@1890: *
BlockingDeque methods come in four forms, with different ways jaroslav@1890: * of handling operations that cannot be satisfied immediately, but may be jaroslav@1890: * satisfied at some point in the future: jaroslav@1890: * one throws an exception, the second returns a special value (either jaroslav@1890: * null or false, depending on the operation), the third jaroslav@1890: * blocks the current thread indefinitely until the operation can succeed, jaroslav@1890: * and the fourth blocks for only a given maximum time limit before giving jaroslav@1890: * up. These methods are summarized in the following table: jaroslav@1890: * jaroslav@1890: *
jaroslav@1890: *
First Element (Head) | jaroslav@1890: *||||
jaroslav@1890: * | Throws exception | jaroslav@1890: *Special value | jaroslav@1890: *Blocks | jaroslav@1890: *Times out | jaroslav@1890: *
Insert | jaroslav@1890: *{@link #addFirst addFirst(e)} | jaroslav@1890: *{@link #offerFirst(Object) offerFirst(e)} | jaroslav@1890: *{@link #putFirst putFirst(e)} | jaroslav@1890: *{@link #offerFirst(Object, long, TimeUnit) offerFirst(e, time, unit)} | jaroslav@1890: *
Remove | jaroslav@1890: *{@link #removeFirst removeFirst()} | jaroslav@1890: *{@link #pollFirst pollFirst()} | jaroslav@1890: *{@link #takeFirst takeFirst()} | jaroslav@1890: *{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)} | jaroslav@1890: *
Examine | jaroslav@1890: *{@link #getFirst getFirst()} | jaroslav@1890: *{@link #peekFirst peekFirst()} | jaroslav@1890: *not applicable | jaroslav@1890: *not applicable | jaroslav@1890: *
Last Element (Tail) | jaroslav@1890: *||||
jaroslav@1890: * | Throws exception | jaroslav@1890: *Special value | jaroslav@1890: *Blocks | jaroslav@1890: *Times out | jaroslav@1890: *
Insert | jaroslav@1890: *{@link #addLast addLast(e)} | jaroslav@1890: *{@link #offerLast(Object) offerLast(e)} | jaroslav@1890: *{@link #putLast putLast(e)} | jaroslav@1890: *{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)} | jaroslav@1890: *
Remove | jaroslav@1890: *{@link #removeLast() removeLast()} | jaroslav@1890: *{@link #pollLast() pollLast()} | jaroslav@1890: *{@link #takeLast takeLast()} | jaroslav@1890: *{@link #pollLast(long, TimeUnit) pollLast(time, unit)} | jaroslav@1890: *
Examine | jaroslav@1890: *{@link #getLast getLast()} | jaroslav@1890: *{@link #peekLast peekLast()} | jaroslav@1890: *not applicable | jaroslav@1890: *not applicable | jaroslav@1890: *
Like any {@link BlockingQueue}, a BlockingDeque is thread safe, jaroslav@1890: * does not permit null elements, and may (or may not) be jaroslav@1890: * capacity-constrained. jaroslav@1890: * jaroslav@1890: *
A BlockingDeque implementation may be used directly as a FIFO jaroslav@1890: * BlockingQueue. The methods inherited from the jaroslav@1890: * BlockingQueue interface are precisely equivalent to jaroslav@1890: * BlockingDeque methods as indicated in the following table: jaroslav@1890: * jaroslav@1890: *
jaroslav@1890: *
BlockingQueue Method | jaroslav@1890: *Equivalent BlockingDeque Method | jaroslav@1890: *
Insert | jaroslav@1890: *|
{@link #add(Object) add(e)} | jaroslav@1890: *{@link #addLast(Object) addLast(e)} | jaroslav@1890: *
{@link #offer(Object) offer(e)} | jaroslav@1890: *{@link #offerLast(Object) offerLast(e)} | jaroslav@1890: *
{@link #put(Object) put(e)} | jaroslav@1890: *{@link #putLast(Object) putLast(e)} | jaroslav@1890: *
{@link #offer(Object, long, TimeUnit) offer(e, time, unit)} | jaroslav@1890: *{@link #offerLast(Object, long, TimeUnit) offerLast(e, time, unit)} | jaroslav@1890: *
Remove | jaroslav@1890: *|
{@link #remove() remove()} | jaroslav@1890: *{@link #removeFirst() removeFirst()} | jaroslav@1890: *
{@link #poll() poll()} | jaroslav@1890: *{@link #pollFirst() pollFirst()} | jaroslav@1890: *
{@link #take() take()} | jaroslav@1890: *{@link #takeFirst() takeFirst()} | jaroslav@1890: *
{@link #poll(long, TimeUnit) poll(time, unit)} | jaroslav@1890: *{@link #pollFirst(long, TimeUnit) pollFirst(time, unit)} | jaroslav@1890: *
Examine | jaroslav@1890: *|
{@link #element() element()} | jaroslav@1890: *{@link #getFirst() getFirst()} | jaroslav@1890: *
{@link #peek() peek()} | jaroslav@1890: *{@link #peekFirst() peekFirst()} | jaroslav@1890: *
Memory consistency effects: As with other concurrent jaroslav@1890: * collections, actions in a thread prior to placing an object into a jaroslav@1890: * {@code BlockingDeque} jaroslav@1890: * happen-before jaroslav@1890: * actions subsequent to the access or removal of that element from jaroslav@1890: * the {@code BlockingDeque} in another thread. jaroslav@1890: * jaroslav@1890: *
This interface is a member of the
jaroslav@1890: *
jaroslav@1890: * Java Collections Framework.
jaroslav@1890: *
jaroslav@1890: * @since 1.6
jaroslav@1890: * @author Doug Lea
jaroslav@1890: * @param This method is equivalent to {@link #addLast(Object) addLast}.
jaroslav@1890: *
jaroslav@1890: * @param e the element to add
jaroslav@1890: * @throws IllegalStateException {@inheritDoc}
jaroslav@1890: * @throws ClassCastException if the class of the specified element
jaroslav@1890: * prevents it from being added to this deque
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * @throws IllegalArgumentException if some property of the specified
jaroslav@1890: * element prevents it from being added to this deque
jaroslav@1890: */
jaroslav@1890: boolean add(E e);
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Inserts the specified element into the queue represented by this deque
jaroslav@1890: * (in other words, at the tail of this deque) if it is possible to do so
jaroslav@1890: * immediately without violating capacity restrictions, returning
jaroslav@1890: * true upon success and false if no space is currently
jaroslav@1890: * available. When using a capacity-restricted deque, this method is
jaroslav@1890: * generally preferable to the {@link #add} method, which can fail to
jaroslav@1890: * insert an element only by throwing an exception.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #offerLast(Object) offerLast}.
jaroslav@1890: *
jaroslav@1890: * @param e the element to add
jaroslav@1890: * @throws ClassCastException if the class of the specified element
jaroslav@1890: * prevents it from being added to this deque
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * @throws IllegalArgumentException if some property of the specified
jaroslav@1890: * element prevents it from being added to this deque
jaroslav@1890: */
jaroslav@1890: boolean offer(E e);
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Inserts the specified element into the queue represented by this deque
jaroslav@1890: * (in other words, at the tail of this deque), waiting if necessary for
jaroslav@1890: * space to become available.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #putLast(Object) putLast}.
jaroslav@1890: *
jaroslav@1890: * @param e the element to add
jaroslav@1890: * @throws InterruptedException {@inheritDoc}
jaroslav@1890: * @throws ClassCastException if the class of the specified element
jaroslav@1890: * prevents it from being added to this deque
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * @throws IllegalArgumentException if some property of the specified
jaroslav@1890: * element prevents it from being added to this deque
jaroslav@1890: */
jaroslav@1890: void put(E e) throws InterruptedException;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Inserts the specified element into the queue represented by this deque
jaroslav@1890: * (in other words, at the tail of this deque), waiting up to the
jaroslav@1890: * specified wait time if necessary for space to become available.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to
jaroslav@1890: * {@link #offerLast(Object,long,TimeUnit) offerLast}.
jaroslav@1890: *
jaroslav@1890: * @param e the element to add
jaroslav@1890: * @return true if the element was added to this deque, else
jaroslav@1890: * false
jaroslav@1890: * @throws InterruptedException {@inheritDoc}
jaroslav@1890: * @throws ClassCastException if the class of the specified element
jaroslav@1890: * prevents it from being added to this deque
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * @throws IllegalArgumentException if some property of the specified
jaroslav@1890: * element prevents it from being added to this deque
jaroslav@1890: */
jaroslav@1890: boolean offer(E e, long timeout, TimeUnit unit)
jaroslav@1890: throws InterruptedException;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890: * (in other words, the first element of this deque).
jaroslav@1890: * This method differs from {@link #poll poll} only in that it
jaroslav@1890: * throws an exception if this deque is empty.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #removeFirst() removeFirst}.
jaroslav@1890: *
jaroslav@1890: * @return the head of the queue represented by this deque
jaroslav@1890: * @throws NoSuchElementException if this deque is empty
jaroslav@1890: */
jaroslav@1890: E remove();
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890: * (in other words, the first element of this deque), or returns
jaroslav@1890: * null if this deque is empty.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #pollFirst()}.
jaroslav@1890: *
jaroslav@1890: * @return the head of this deque, or null if this deque is empty
jaroslav@1890: */
jaroslav@1890: E poll();
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890: * (in other words, the first element of this deque), waiting if
jaroslav@1890: * necessary until an element becomes available.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #takeFirst() takeFirst}.
jaroslav@1890: *
jaroslav@1890: * @return the head of this deque
jaroslav@1890: * @throws InterruptedException if interrupted while waiting
jaroslav@1890: */
jaroslav@1890: E take() throws InterruptedException;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Retrieves and removes the head of the queue represented by this deque
jaroslav@1890: * (in other words, the first element of this deque), waiting up to the
jaroslav@1890: * specified wait time if necessary for an element to become available.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to
jaroslav@1890: * {@link #pollFirst(long,TimeUnit) pollFirst}.
jaroslav@1890: *
jaroslav@1890: * @return the head of this deque, or null if the
jaroslav@1890: * specified waiting time elapses before an element is available
jaroslav@1890: * @throws InterruptedException if interrupted while waiting
jaroslav@1890: */
jaroslav@1890: E poll(long timeout, TimeUnit unit)
jaroslav@1890: throws InterruptedException;
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Retrieves, but does not remove, the head of the queue represented by
jaroslav@1890: * this deque (in other words, the first element of this deque).
jaroslav@1890: * This method differs from {@link #peek peek} only in that it throws an
jaroslav@1890: * exception if this deque is empty.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #getFirst() getFirst}.
jaroslav@1890: *
jaroslav@1890: * @return the head of this deque
jaroslav@1890: * @throws NoSuchElementException if this deque is empty
jaroslav@1890: */
jaroslav@1890: E element();
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Retrieves, but does not remove, the head of the queue represented by
jaroslav@1890: * this deque (in other words, the first element of this deque), or
jaroslav@1890: * returns null if this deque is empty.
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to {@link #peekFirst() peekFirst}.
jaroslav@1890: *
jaroslav@1890: * @return the head of this deque, or null if this deque is empty
jaroslav@1890: */
jaroslav@1890: E peek();
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Removes the first occurrence of the specified element from this deque.
jaroslav@1890: * If the deque does not contain the element, it is unchanged.
jaroslav@1890: * More formally, removes the first element e such that
jaroslav@1890: * o.equals(e) (if such an element exists).
jaroslav@1890: * Returns true if this deque contained the specified element
jaroslav@1890: * (or equivalently, if this deque changed as a result of the call).
jaroslav@1890: *
jaroslav@1890: * This method is equivalent to
jaroslav@1890: * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
jaroslav@1890: *
jaroslav@1890: * @param o element to be removed from this deque, if present
jaroslav@1890: * @return true if this deque changed as a result of the call
jaroslav@1890: * @throws ClassCastException if the class of the specified element
jaroslav@1890: * is incompatible with this deque
jaroslav@1890: * (optional)
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * (optional)
jaroslav@1890: */
jaroslav@1890: boolean remove(Object o);
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns true if this deque contains the specified element.
jaroslav@1890: * More formally, returns true if and only if this deque contains
jaroslav@1890: * at least one element e such that o.equals(e).
jaroslav@1890: *
jaroslav@1890: * @param o object to be checked for containment in this deque
jaroslav@1890: * @return true if this deque contains the specified element
jaroslav@1890: * @throws ClassCastException if the class of the specified element
jaroslav@1890: * is incompatible with this deque
jaroslav@1890: * (optional)
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * (optional)
jaroslav@1890: */
jaroslav@1890: public boolean contains(Object o);
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns the number of elements in this deque.
jaroslav@1890: *
jaroslav@1890: * @return the number of elements in this deque
jaroslav@1890: */
jaroslav@1890: public int size();
jaroslav@1890:
jaroslav@1890: /**
jaroslav@1890: * Returns an iterator over the elements in this deque in proper sequence.
jaroslav@1890: * The elements will be returned in order from first (head) to last (tail).
jaroslav@1890: *
jaroslav@1890: * @return an iterator over the elements in this deque in proper sequence
jaroslav@1890: */
jaroslav@1890: Iterator This method is equivalent to {@link #addFirst(Object) addFirst}.
jaroslav@1890: *
jaroslav@1890: * @throws IllegalStateException {@inheritDoc}
jaroslav@1890: * @throws ClassCastException {@inheritDoc}
jaroslav@1890: * @throws NullPointerException if the specified element is null
jaroslav@1890: * @throws IllegalArgumentException {@inheritDoc}
jaroslav@1890: */
jaroslav@1890: void push(E e);
jaroslav@1890: }