/*1* Written by Doug Lea and Josh Bloch with assistance from members of2* JCP JSR-166 Expert Group and released to the public domain, as explained3* at http://creativecommons.org/publicdomain/zero/1.0/4*/56package com.artifex.mupdfdemo;78import java.util.Collection;9import java.util.Iterator;10import java.util.List;11import java.util.NoSuchElementException;12import java.util.Queue;13import java.util.Stack;1415// BEGIN android-note16// removed link to collections framework docs17// END android-note1819/**20* A linear collection that supports element insertion and removal at21* both ends. The name <i>deque</i> is short for "double ended queue"22* and is usually pronounced "deck". Most <tt>Deque</tt>23* implementations place no fixed limits on the number of elements24* they may contain, but this interface supports capacity-restricted25* deques as well as those with no fixed size limit.26*27* <p>This interface defines methods to access the elements at both28* ends of the deque. Methods are provided to insert, remove, and29* examine the element. Each of these methods exists in two forms:30* one throws an exception if the operation fails, the other returns a31* special value (either <tt>null</tt> or <tt>false</tt>, depending on32* the operation). The latter form of the insert operation is33* designed specifically for use with capacity-restricted34* <tt>Deque</tt> implementations; in most implementations, insert35* operations cannot fail.36*37* <p>The twelve methods described above are summarized in the38* following table:39*40* <p>41* <table BORDER CELLPADDING=3 CELLSPACING=1>42* <tr>43* <td></td>44* <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>45* <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>46* </tr>47* <tr>48* <td></td>49* <td ALIGN=CENTER><em>Throws exception</em></td>50* <td ALIGN=CENTER><em>Special value</em></td>51* <td ALIGN=CENTER><em>Throws exception</em></td>52* <td ALIGN=CENTER><em>Special value</em></td>53* </tr>54* <tr>55* <td><b>Insert</b></td>56* <td>{@link #addFirst addFirst(e)}</td>57* <td>{@link #offerFirst offerFirst(e)}</td>58* <td>{@link #addLast addLast(e)}</td>59* <td>{@link #offerLast offerLast(e)}</td>60* </tr>61* <tr>62* <td><b>Remove</b></td>63* <td>{@link #removeFirst removeFirst()}</td>64* <td>{@link #pollFirst pollFirst()}</td>65* <td>{@link #removeLast removeLast()}</td>66* <td>{@link #pollLast pollLast()}</td>67* </tr>68* <tr>69* <td><b>Examine</b></td>70* <td>{@link #getFirst getFirst()}</td>71* <td>{@link #peekFirst peekFirst()}</td>72* <td>{@link #getLast getLast()}</td>73* <td>{@link #peekLast peekLast()}</td>74* </tr>75* </table>76*77* <p>This interface extends the {@link Queue} interface. When a deque is78* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are79* added at the end of the deque and removed from the beginning. The methods80* inherited from the <tt>Queue</tt> interface are precisely equivalent to81* <tt>Deque</tt> methods as indicated in the following table:82*83* <p>84* <table BORDER CELLPADDING=3 CELLSPACING=1>85* <tr>86* <td ALIGN=CENTER> <b><tt>Queue</tt> Method</b></td>87* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>88* </tr>89* <tr>90* <td>{@link java.util.Queue#add add(e)}</td>91* <td>{@link #addLast addLast(e)}</td>92* </tr>93* <tr>94* <td>{@link java.util.Queue#offer offer(e)}</td>95* <td>{@link #offerLast offerLast(e)}</td>96* </tr>97* <tr>98* <td>{@link java.util.Queue#remove remove()}</td>99* <td>{@link #removeFirst removeFirst()}</td>100* </tr>101* <tr>102* <td>{@link java.util.Queue#poll poll()}</td>103* <td>{@link #pollFirst pollFirst()}</td>104* </tr>105* <tr>106* <td>{@link java.util.Queue#element element()}</td>107* <td>{@link #getFirst getFirst()}</td>108* </tr>109* <tr>110* <td>{@link java.util.Queue#peek peek()}</td>111* <td>{@link #peek peekFirst()}</td>112* </tr>113* </table>114*115* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This116* interface should be used in preference to the legacy {@link Stack} class.117* When a deque is used as a stack, elements are pushed and popped from the118* beginning of the deque. Stack methods are precisely equivalent to119* <tt>Deque</tt> methods as indicated in the table below:120*121* <p>122* <table BORDER CELLPADDING=3 CELLSPACING=1>123* <tr>124* <td ALIGN=CENTER> <b>Stack Method</b></td>125* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>126* </tr>127* <tr>128* <td>{@link #push push(e)}</td>129* <td>{@link #addFirst addFirst(e)}</td>130* </tr>131* <tr>132* <td>{@link #pop pop()}</td>133* <td>{@link #removeFirst removeFirst()}</td>134* </tr>135* <tr>136* <td>{@link #peek peek()}</td>137* <td>{@link #peekFirst peekFirst()}</td>138* </tr>139* </table>140*141* <p>Note that the {@link #peek peek} method works equally well when142* a deque is used as a queue or a stack; in either case, elements are143* drawn from the beginning of the deque.144*145* <p>This interface provides two methods to remove interior146* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and147* {@link #removeLastOccurrence removeLastOccurrence}.148*149* <p>Unlike the {@link List} interface, this interface does not150* provide support for indexed access to elements.151*152* <p>While <tt>Deque</tt> implementations are not strictly required153* to prohibit the insertion of null elements, they are strongly154* encouraged to do so. Users of any <tt>Deque</tt> implementations155* that do allow null elements are strongly encouraged <i>not</i> to156* take advantage of the ability to insert nulls. This is so because157* <tt>null</tt> is used as a special return value by various methods158* to indicated that the deque is empty.159*160* <p><tt>Deque</tt> implementations generally do not define161* element-based versions of the <tt>equals</tt> and <tt>hashCode</tt>162* methods, but instead inherit the identity-based versions from class163* <tt>Object</tt>.164*165* @author Doug Lea166* @author Josh Bloch167* @since 1.6168* @param <E> the type of elements held in this collection169*/170171public interface Deque<E> extends Queue<E> {172/**173* Inserts the specified element at the front of this deque if it is174* possible to do so immediately without violating capacity restrictions.175* When using a capacity-restricted deque, it is generally preferable to176* use method {@link #offerFirst}.177*178* @param e the element to add179* @throws IllegalStateException if the element cannot be added at this180* time due to capacity restrictions181* @throws ClassCastException if the class of the specified element182* prevents it from being added to this deque183* @throws NullPointerException if the specified element is null and this184* deque does not permit null elements185* @throws IllegalArgumentException if some property of the specified186* element prevents it from being added to this deque187*/188void addFirst(E e);189190/**191* Inserts the specified element at the end of this deque if it is192* possible to do so immediately without violating capacity restrictions.193* When using a capacity-restricted deque, it is generally preferable to194* use method {@link #offerLast}.195*196* <p>This method is equivalent to {@link #add}.197*198* @param e the element to add199* @throws IllegalStateException if the element cannot be added at this200* time due to capacity restrictions201* @throws ClassCastException if the class of the specified element202* prevents it from being added to this deque203* @throws NullPointerException if the specified element is null and this204* deque does not permit null elements205* @throws IllegalArgumentException if some property of the specified206* element prevents it from being added to this deque207*/208void addLast(E e);209210/**211* Inserts the specified element at the front of this deque unless it would212* violate capacity restrictions. When using a capacity-restricted deque,213* this method is generally preferable to the {@link #addFirst} method,214* which can fail to insert an element only by throwing an exception.215*216* @param e the element to add217* @return <tt>true</tt> if the element was added to this deque, else218* <tt>false</tt>219* @throws ClassCastException if the class of the specified element220* prevents it from being added to this deque221* @throws NullPointerException if the specified element is null and this222* deque does not permit null elements223* @throws IllegalArgumentException if some property of the specified224* element prevents it from being added to this deque225*/226boolean offerFirst(E e);227228/**229* Inserts the specified element at the end of this deque unless it would230* violate capacity restrictions. When using a capacity-restricted deque,231* this method is generally preferable to the {@link #addLast} method,232* which can fail to insert an element only by throwing an exception.233*234* @param e the element to add235* @return <tt>true</tt> if the element was added to this deque, else236* <tt>false</tt>237* @throws ClassCastException if the class of the specified element238* prevents it from being added to this deque239* @throws NullPointerException if the specified element is null and this240* deque does not permit null elements241* @throws IllegalArgumentException if some property of the specified242* element prevents it from being added to this deque243*/244boolean offerLast(E e);245246/**247* Retrieves and removes the first element of this deque. This method248* differs from {@link #pollFirst pollFirst} only in that it throws an249* exception if this deque is empty.250*251* @return the head of this deque252* @throws NoSuchElementException if this deque is empty253*/254E removeFirst();255256/**257* Retrieves and removes the last element of this deque. This method258* differs from {@link #pollLast pollLast} only in that it throws an259* exception if this deque is empty.260*261* @return the tail of this deque262* @throws NoSuchElementException if this deque is empty263*/264E removeLast();265266/**267* Retrieves and removes the first element of this deque,268* or returns <tt>null</tt> if this deque is empty.269*270* @return the head of this deque, or <tt>null</tt> if this deque is empty271*/272E pollFirst();273274/**275* Retrieves and removes the last element of this deque,276* or returns <tt>null</tt> if this deque is empty.277*278* @return the tail of this deque, or <tt>null</tt> if this deque is empty279*/280E pollLast();281282/**283* Retrieves, but does not remove, the first element of this deque.284*285* This method differs from {@link #peekFirst peekFirst} only in that it286* throws an exception if this deque is empty.287*288* @return the head of this deque289* @throws NoSuchElementException if this deque is empty290*/291E getFirst();292293/**294* Retrieves, but does not remove, the last element of this deque.295* This method differs from {@link #peekLast peekLast} only in that it296* throws an exception if this deque is empty.297*298* @return the tail of this deque299* @throws NoSuchElementException if this deque is empty300*/301E getLast();302303/**304* Retrieves, but does not remove, the first element of this deque,305* or returns <tt>null</tt> if this deque is empty.306*307* @return the head of this deque, or <tt>null</tt> if this deque is empty308*/309E peekFirst();310311/**312* Retrieves, but does not remove, the last element of this deque,313* or returns <tt>null</tt> if this deque is empty.314*315* @return the tail of this deque, or <tt>null</tt> if this deque is empty316*/317E peekLast();318319/**320* Removes the first occurrence of the specified element from this deque.321* If the deque does not contain the element, it is unchanged.322* More formally, removes the first element <tt>e</tt> such that323* <tt>(o==null ? e==null : o.equals(e))</tt>324* (if such an element exists).325* Returns <tt>true</tt> if this deque contained the specified element326* (or equivalently, if this deque changed as a result of the call).327*328* @param o element to be removed from this deque, if present329* @return <tt>true</tt> if an element was removed as a result of this call330* @throws ClassCastException if the class of the specified element331* is incompatible with this deque (optional)332* @throws NullPointerException if the specified element is null and this333* deque does not permit null elements (optional)334*/335boolean removeFirstOccurrence(Object o);336337/**338* Removes the last occurrence of the specified element from this deque.339* If the deque does not contain the element, it is unchanged.340* More formally, removes the last element <tt>e</tt> such that341* <tt>(o==null ? e==null : o.equals(e))</tt>342* (if such an element exists).343* Returns <tt>true</tt> if this deque contained the specified element344* (or equivalently, if this deque changed as a result of the call).345*346* @param o element to be removed from this deque, if present347* @return <tt>true</tt> if an element was removed as a result of this call348* @throws ClassCastException if the class of the specified element349* is incompatible with this deque (optional)350* @throws NullPointerException if the specified element is null and this351* deque does not permit null elements (optional)352*/353boolean removeLastOccurrence(Object o);354355// *** Queue methods ***356357/**358* Inserts the specified element into the queue represented by this deque359* (in other words, at the tail of this deque) if it is possible to do so360* immediately without violating capacity restrictions, returning361* <tt>true</tt> upon success and throwing an362* <tt>IllegalStateException</tt> if no space is currently available.363* When using a capacity-restricted deque, it is generally preferable to364* use {@link #offer(Object) offer}.365*366* <p>This method is equivalent to {@link #addLast}.367*368* @param e the element to add369* @return <tt>true</tt> (as specified by {@link Collection#add})370* @throws IllegalStateException if the element cannot be added at this371* time due to capacity restrictions372* @throws ClassCastException if the class of the specified element373* prevents it from being added to this deque374* @throws NullPointerException if the specified element is null and this375* deque does not permit null elements376* @throws IllegalArgumentException if some property of the specified377* element prevents it from being added to this deque378*/379boolean add(E e);380381/**382* Inserts the specified element into the queue represented by this deque383* (in other words, at the tail of this deque) if it is possible to do so384* immediately without violating capacity restrictions, returning385* <tt>true</tt> upon success and <tt>false</tt> if no space is currently386* available. When using a capacity-restricted deque, this method is387* generally preferable to the {@link #add} method, which can fail to388* insert an element only by throwing an exception.389*390* <p>This method is equivalent to {@link #offerLast}.391*392* @param e the element to add393* @return <tt>true</tt> if the element was added to this deque, else394* <tt>false</tt>395* @throws ClassCastException if the class of the specified element396* prevents it from being added to this deque397* @throws NullPointerException if the specified element is null and this398* deque does not permit null elements399* @throws IllegalArgumentException if some property of the specified400* element prevents it from being added to this deque401*/402boolean offer(E e);403404/**405* Retrieves and removes the head of the queue represented by this deque406* (in other words, the first element of this deque).407* This method differs from {@link #poll poll} only in that it throws an408* exception if this deque is empty.409*410* <p>This method is equivalent to {@link #removeFirst()}.411*412* @return the head of the queue represented by this deque413* @throws NoSuchElementException if this deque is empty414*/415E remove();416417/**418* Retrieves and removes the head of the queue represented by this deque419* (in other words, the first element of this deque), or returns420* <tt>null</tt> if this deque is empty.421*422* <p>This method is equivalent to {@link #pollFirst()}.423*424* @return the first element of this deque, or <tt>null</tt> if425* this deque is empty426*/427E poll();428429/**430* Retrieves, but does not remove, the head of the queue represented by431* this deque (in other words, the first element of this deque).432* This method differs from {@link #peek peek} only in that it throws an433* exception if this deque is empty.434*435* <p>This method is equivalent to {@link #getFirst()}.436*437* @return the head of the queue represented by this deque438* @throws NoSuchElementException if this deque is empty439*/440E element();441442/**443* Retrieves, but does not remove, the head of the queue represented by444* this deque (in other words, the first element of this deque), or445* returns <tt>null</tt> if this deque is empty.446*447* <p>This method is equivalent to {@link #peekFirst()}.448*449* @return the head of the queue represented by this deque, or450* <tt>null</tt> if this deque is empty451*/452E peek();453454455// *** Stack methods ***456457/**458* Pushes an element onto the stack represented by this deque (in other459* words, at the head of this deque) if it is possible to do so460* immediately without violating capacity restrictions, returning461* <tt>true</tt> upon success and throwing an462* <tt>IllegalStateException</tt> if no space is currently available.463*464* <p>This method is equivalent to {@link #addFirst}.465*466* @param e the element to push467* @throws IllegalStateException if the element cannot be added at this468* time due to capacity restrictions469* @throws ClassCastException if the class of the specified element470* prevents it from being added to this deque471* @throws NullPointerException if the specified element is null and this472* deque does not permit null elements473* @throws IllegalArgumentException if some property of the specified474* element prevents it from being added to this deque475*/476void push(E e);477478/**479* Pops an element from the stack represented by this deque. In other480* words, removes and returns the first element of this deque.481*482* <p>This method is equivalent to {@link #removeFirst()}.483*484* @return the element at the front of this deque (which is the top485* of the stack represented by this deque)486* @throws NoSuchElementException if this deque is empty487*/488E pop();489490491// *** Collection methods ***492493/**494* Removes the first occurrence of the specified element from this deque.495* If the deque does not contain the element, it is unchanged.496* More formally, removes the first element <tt>e</tt> such that497* <tt>(o==null ? e==null : o.equals(e))</tt>498* (if such an element exists).499* Returns <tt>true</tt> if this deque contained the specified element500* (or equivalently, if this deque changed as a result of the call).501*502* <p>This method is equivalent to {@link #removeFirstOccurrence}.503*504* @param o element to be removed from this deque, if present505* @return <tt>true</tt> if an element was removed as a result of this call506* @throws ClassCastException if the class of the specified element507* is incompatible with this deque (optional)508* @throws NullPointerException if the specified element is null and this509* deque does not permit null elements (optional)510*/511boolean remove(Object o);512513/**514* Returns <tt>true</tt> if this deque contains the specified element.515* More formally, returns <tt>true</tt> if and only if this deque contains516* at least one element <tt>e</tt> such that517* <tt>(o==null ? e==null : o.equals(e))</tt>.518*519* @param o element whose presence in this deque is to be tested520* @return <tt>true</tt> if this deque contains the specified element521* @throws ClassCastException if the type of the specified element522* is incompatible with this deque (optional)523* @throws NullPointerException if the specified element is null and this524* deque does not permit null elements (optional)525*/526boolean contains(Object o);527528/**529* Returns the number of elements in this deque.530*531* @return the number of elements in this deque532*/533public int size();534535/**536* Returns an iterator over the elements in this deque in proper sequence.537* The elements will be returned in order from first (head) to last (tail).538*539* @return an iterator over the elements in this deque in proper sequence540*/541Iterator<E> iterator();542543/**544* Returns an iterator over the elements in this deque in reverse545* sequential order. The elements will be returned in order from546* last (tail) to first (head).547*548* @return an iterator over the elements in this deque in reverse549* sequence550*/551Iterator<E> descendingIterator();552553}554555556