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