Deque.java revision 0
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 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun 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. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 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 and Josh Bloch with assistance from members of 0N/A * JCP JSR-166 Expert Group and released to the public domain, as explained 0N/A * A linear collection that supports element insertion and removal at 0N/A * both ends. The name <i>deque</i> is short for "double ended queue" 0N/A * and is usually pronounced "deck". Most <tt>Deque</tt> 0N/A * implementations place no fixed limits on the number of elements 0N/A * they may contain, but this interface supports capacity-restricted 0N/A * deques as well as those with no fixed size limit. 0N/A * <p>This interface defines methods to access the elements at both 0N/A * ends of the deque. Methods are provided to insert, remove, and 0N/A * examine the element. Each of these methods exists in two forms: 0N/A * one throws an exception if the operation fails, the other returns a 0N/A * special value (either <tt>null</tt> or <tt>false</tt>, depending on 0N/A * the operation). The latter form of the insert operation is 0N/A * designed specifically for use with capacity-restricted 0N/A * <tt>Deque</tt> implementations; in most implementations, insert 0N/A * operations cannot fail. 0N/A * <p>The twelve methods described above are summarized in the 0N/A * <table BORDER CELLPADDING=3 CELLSPACING=1> 0N/A * <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td> 0N/A * <td ALIGN=CENTER COLSPAN = 2> <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>Throws exception</em></td> 0N/A * <td ALIGN=CENTER><em>Special value</em></td> 0N/A * <td><b>Insert</b></td> 0N/A * <td>{@link #addFirst addFirst(e)}</td> 0N/A * <td>{@link #offerFirst offerFirst(e)}</td> 0N/A * <td>{@link #addLast addLast(e)}</td> 0N/A * <td>{@link #offerLast offerLast(e)}</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 #removeLast removeLast()}</td> 0N/A * <td>{@link #pollLast pollLast()}</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>{@link #getLast getLast()}</td> 0N/A * <td>{@link #peekLast peekLast()}</td> 0N/A * <p>This interface extends the {@link Queue} interface. When a deque is 0N/A * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are 0N/A * added at the end of the deque and removed from the beginning. The methods 0N/A * inherited from the <tt>Queue</tt> interface are precisely equivalent to 0N/A * <tt>Deque</tt> methods as indicated in the following table: 0N/A * <table BORDER CELLPADDING=3 CELLSPACING=1> 0N/A * <td ALIGN=CENTER> <b><tt>Queue</tt> Method</b></td> 0N/A * <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td> 0N/A * <td>{@link java.util.Queue#add add(e)}</td> 0N/A * <td>{@link #addLast addLast(e)}</td> 0N/A * <td>{@link java.util.Queue#offer offer(e)}</td> 0N/A * <td>{@link #offerLast offerLast(e)}</td> 0N/A * <td>{@link java.util.Queue#remove remove()}</td> 0N/A * <td>{@link #removeFirst removeFirst()}</td> 0N/A * <td>{@link java.util.Queue#poll poll()}</td> 0N/A * <td>{@link #pollFirst pollFirst()}</td> 0N/A * <td>{@link java.util.Queue#element element()}</td> 0N/A * <td>{@link #getFirst getFirst()}</td> 0N/A * <td>{@link java.util.Queue#peek peek()}</td> 0N/A * <td>{@link #peek peekFirst()}</td> 0N/A * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This 0N/A * interface should be used in preference to the legacy {@link Stack} class. 0N/A * When a deque is used as a stack, elements are pushed and popped from the 0N/A * beginning of the deque. Stack methods are precisely equivalent to 0N/A * <tt>Deque</tt> methods as indicated in the table below: 0N/A * <table BORDER CELLPADDING=3 CELLSPACING=1> 0N/A * <td ALIGN=CENTER> <b>Stack Method</b></td> 0N/A * <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td> 0N/A * <td>{@link #push push(e)}</td> 0N/A * <td>{@link #addFirst addFirst(e)}</td> 0N/A * <td>{@link #pop pop()}</td> 0N/A * <td>{@link #removeFirst removeFirst()}</td> 0N/A * <td>{@link #peek peek()}</td> 0N/A * <td>{@link #peekFirst peekFirst()}</td> 0N/A * <p>Note that the {@link #peek peek} method works equally well when 0N/A * a deque is used as a queue or a stack; in either case, elements are 0N/A * drawn from the beginning of the deque. 0N/A * <p>This interface provides two methods to remove interior 0N/A * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and 0N/A * {@link #removeLastOccurrence removeLastOccurrence}. 0N/A * <p>Unlike the {@link List} interface, this interface does not 0N/A * provide support for indexed access to elements. 0N/A * <p>While <tt>Deque</tt> implementations are not strictly required 0N/A * to prohibit the insertion of null elements, they are strongly 0N/A * encouraged to do so. Users of any <tt>Deque</tt> implementations 0N/A * that do allow null elements are strongly encouraged <i>not</i> to 0N/A * take advantage of the ability to insert nulls. This is so because 0N/A * <tt>null</tt> is used as a special return value by various methods 0N/A * to indicated that the deque is empty. 0N/A * <p><tt>Deque</tt> implementations generally do not define 0N/A * element-based versions of the <tt>equals</tt> and <tt>hashCode</tt> 0N/A * methods, but instead inherit the identity-based versions from class 0N/A * <p>This interface is a member of the <a 0N/A * @author Josh Bloch 0N/A * @param <E> the type of elements held in this collection 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 * When using a capacity-restricted deque, it is generally preferable to 0N/A * use method {@link #offerFirst}. 0N/A * @param e the element to add 0N/A * @throws IllegalStateException if the element cannot be added at this 0N/A * time due to capacity restrictions 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 and this 0N/A * deque does not permit null elements 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 if it is 0N/A * possible to do so immediately without violating capacity restrictions. 0N/A * When using a capacity-restricted deque, it is generally preferable to 0N/A * use method {@link #offerLast}. 0N/A * <p>This method is equivalent to {@link #add}. 0N/A * @param e the element to add 0N/A * @throws IllegalStateException if the element cannot be added at this 0N/A * time due to capacity restrictions 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 and this 0N/A * deque does not permit null elements 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 unless it would 0N/A * violate capacity restrictions. When using a capacity-restricted deque, 0N/A * this method is generally preferable to the {@link #addFirst} method, 0N/A * which can fail to insert an element only by throwing an exception. 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 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 and this 0N/A * deque does not permit null elements 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 unless it would 0N/A * violate capacity restrictions. When using a capacity-restricted deque, 0N/A * this method is generally preferable to the {@link #addLast} method, 0N/A * which can fail to insert an element only by throwing an exception. 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 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 and this 0N/A * deque does not permit null elements 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. This method 0N/A * differs from {@link #pollFirst pollFirst} only in that it throws an 0N/A * exception if this deque is empty. 0N/A * @return the head of this deque 0N/A * @throws NoSuchElementException if this deque is empty 0N/A * Retrieves and removes the last element of this deque. This method 0N/A * differs from {@link #pollLast pollLast} only in that it throws an 0N/A * exception if this deque is empty. 0N/A * @return the tail of this deque 0N/A * @throws NoSuchElementException if this deque is empty 0N/A * Retrieves and removes the first element of this deque, 0N/A * or returns <tt>null</tt> if this deque is empty. 0N/A * @return the head of this deque, or <tt>null</tt> if this deque is empty 0N/A * Retrieves and removes the last element of this deque, 0N/A * or returns <tt>null</tt> if this deque is empty. 0N/A * @return the tail of this deque, or <tt>null</tt> if this deque is empty 0N/A * Retrieves, but does not remove, the first element of this deque. 0N/A * This method differs from {@link #peekFirst peekFirst} only in that it 0N/A * throws an exception if this deque is empty. 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 last element of this deque. 0N/A * This method differs from {@link #peekLast peekLast} only in that it 0N/A * throws an exception if this deque is empty. 0N/A * @return the tail of this deque 0N/A * @throws NoSuchElementException if this deque is empty 0N/A * Retrieves, but does not remove, the first element of this deque, 0N/A * or returns <tt>null</tt> if this deque is empty. 0N/A * @return the head of this deque, or <tt>null</tt> if this deque is empty 0N/A * Retrieves, but does not remove, the last element of this deque, 0N/A * or returns <tt>null</tt> if this deque is empty. 0N/A * @return the tail 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==null ? e==null : o.equals(e))</tt> 0N/A * (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 0N/A * is incompatible with this deque (optional) 0N/A * @throws NullPointerException if the specified element is null and this 0N/A * deque does not permit null elements (optional) 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==null ? e==null : o.equals(e))</tt> 0N/A * (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 0N/A * is incompatible with this deque (optional) 0N/A * @throws NullPointerException if the specified element is null and this 0N/A * deque does not permit null elements (optional) 0N/A // *** Queue 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}. 0N/A * @param e the element to add 0N/A * @return <tt>true</tt> (as specified by {@link Collection#add}) 0N/A * @throws IllegalStateException if the element cannot be added at this 0N/A * time due to capacity restrictions 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 and this 0N/A * deque does not permit null elements 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}. 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 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 and this 0N/A * deque does not permit null elements 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 throws an 0N/A * exception if this deque is empty. 0N/A * <p>This method is equivalent to {@link #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 first element of this deque, or <tt>null</tt> if 0N/A * 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). 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()}. 0N/A * @return the head of the queue represented by 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()}. 0N/A * @return the head of the queue represented by this deque, or 0N/A * <tt>null</tt> if this deque is empty 0N/A // *** Stack methods *** 0N/A * Pushes an element onto the stack represented by this deque (in other 0N/A * words, at the head 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 * <p>This method is equivalent to {@link #addFirst}. 0N/A * @param e the element to push 0N/A * @throws IllegalStateException if the element cannot be added at this 0N/A * time due to capacity restrictions 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 and this 0N/A * deque does not permit null elements 0N/A * @throws IllegalArgumentException if some property of the specified 0N/A * element prevents it from being added to this deque 0N/A * Pops an element from the stack represented by this deque. In other 0N/A * words, removes and returns the first element of this deque. 0N/A * <p>This method is equivalent to {@link #removeFirst()}. 0N/A * @return the element at the front of this deque (which is the top 0N/A * of the stack represented by this deque) 0N/A * @throws NoSuchElementException if this deque is empty 0N/A // *** Collection methods *** 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==null ? e==null : o.equals(e))</tt> 0N/A * (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 {@link #removeFirstOccurrence}. 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 0N/A * is incompatible with this deque (optional) 0N/A * @throws NullPointerException if the specified element is null and this 0N/A * deque does not permit null elements (optional) 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 0N/A * <tt>(o==null ? e==null : o.equals(e))</tt>. 0N/A * @param o element whose presence in this deque is to be tested 0N/A * @return <tt>true</tt> if this deque contains the specified element 0N/A * @throws ClassCastException if the type of the specified element 0N/A * is incompatible with this deque (optional) 0N/A * @throws NullPointerException if the specified element is null and this 0N/A * deque does not permit null elements (optional) 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 * Returns an iterator over the elements in this deque in reverse 0N/A * sequential order. The elements will be returned in order from 0N/A * last (tail) to first (head). 0N/A * @return an iterator over the elements in this deque in reverse