ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166x/Deque.java
(Generate patch)

Comparing jsr166/src/jsr166x/Deque.java (file contents):
Revision 1.2 by dl, Tue Sep 7 10:37:29 2004 UTC vs.
Revision 1.3 by dl, Sun Dec 5 21:15:31 2004 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
2 > * Written by Doug Lea and Josh Bloch with assistance from members of
3 > * JCP JSR-166 Expert Group and released to the public domain, as explained
4 > * at http://creativecommons.org/licenses/publicdomain
5   */
6  
7 < package jsr166x;
8 < import java.util.*;
7 > package jsr166x;     // XXX This belongs in java.util!!! XXX
8 > import java.util.*;    // XXX This import goes away        XXX
9  
10   /**
11 < * A linear collection in which elements may be inserted and removed
12 < * from both the beginning and end. A <tt>Deque</tt> (short for
13 < * "double ended queue") provides uniformly named methods to
14 < * <tt>get</tt>, <tt>peek</tt>, <tt>poll</tt>, <tt>remove</tt>,
15 < * <tt>offer</tt>, and <tt>add</tt> the <tt>first</tt> and
16 < * <tt>last</tt> element of the collection (for example, methods
17 < * <tt>addFirst</tt>, <tt>pollLast</tt>). Unlike interface {@link
18 < * List} the Deque interface does not define support for indexed
19 < * operations or sublists.
20 < *
21 < * <p>A view of a subset of Deque operations can be obtained using
22 < * method {@link #asFifoQueue} to support only Last-In-First-Out (LIFO)
23 < * stack behavior, as well as method {@link #asFifoQueue} to support only
24 < * First-in-First-Out (FIFO) queue behavior.  More commonly, a Deque
25 < * is used when various mixtures of LIFO and FIFO operations are
26 < * required.
27 < *
28 < * <p>Deques additionally provide a few methods to remove elements
29 < * embedded within a deque, proceding from either direction using
30 < * <tt>removeFirstOccurrence</tt> and <tt>removeLastOccurrence</tt>.
31 < * They also support {@link Collection} operations including
32 < * <tt>contains</tt>, <tt>iterator</tt>, and so on.
33 < *
34 < * <p>The {@link #offerFirst} and {@link #offerLast} methods insert an
35 < * element if possible, otherwise returning <tt>false</tt>.  They
36 < * differ from {@link java.util.Collection#add Collection.add}, as
37 < * well as {@link #addFirst} and {@link #addLast} methods, which can
38 < * fail to add an element only by throwing an unchecked exception.
39 < * The <tt>offer</tt> methods are designed for use when failure is a
40 < * normal, rather than exceptional occurrence, for example, in
41 < * fixed-capacity (or &quot;bounded&quot;) deques.
42 < *
43 < * <p><tt>Deque</tt> implementations generally do not allow insertion
44 < * of <tt>null</tt> elements.  Even in implementations that permit it,
45 < * <tt>null</tt> should not be inserted into a <tt>Deque</tt>, as
46 < * <tt>null</tt> is also used as a special return value by the poll
47 < * methods to indicate that the deque contains no elements.
11 > * A linear collection that supports element insertion and removal
12 > * at both ends.  The name <i>deque</i> is short for "double ended
13 > * queue" and is usually pronounced "deck".  Most <tt>Deque</tt>
14 > * implementations place no fixed limits on the number of elements
15 > * they may contain, but this interface supports capacity-restricted
16 > * deques as well as those with no fixed size limit.
17 > *
18 > * <p>This interface defines methods to access the elements at both ends of
19 > * the deque.  Methods are provided to insert, remove, and examine the
20 > * element.  Each of these methods exists in two forms: one throws an
21 > * exception if the operation fails, the other returns a special value (either
22 > * <tt>null</tt> or <tt>false</tt>, depending on the operation).  The latter
23 > * form of the insert operation is designed specifically for use with
24 > * capacity-restricted <tt>Deque</tt> implementations; in most implementations,
25 > * insert operations cannot fail.  
26 > *
27 > * <p>The twelve methods described above are are summarized in the
28 > * follwoing table:<p>
29 > *
30 > * <table BORDER CELLPADDING=3 CELLSPACING=1>
31 > *  <tr>
32 > *    <td></td>
33 > *    <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>
34 > *    <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>
35 > *  </tr>
36 > *  <tr>
37 > *    <td></td>
38 > *    <td ALIGN=CENTER><em>Throws exception</em></td>
39 > *    <td ALIGN=CENTER><em>Returns special value</em></td>
40 > *    <td ALIGN=CENTER><em>Throws exception</em></td>
41 > *    <td ALIGN=CENTER><em>Returns special value</em></td>
42 > *  </tr>
43 > *  <tr>
44 > *    <td><b>Insert</b></td>
45 > *    <td>{@link #addFirst addFirst(e)}</td>
46 > *    <td>{@link #offerFirst offerFirst(e)}</td>
47 > *    <td>{@link #addLast addLast(e)}</td>
48 > *    <td>{@link #offerLast offerLast(e)}</td>
49 > *  </tr>
50 > *  <tr>
51 > *    <td><b>Remove</b></td>
52 > *    <td>{@link #removeFirst removeFirst()}</td>
53 > *    <td>{@link #pollFirst pollFirst()}</td>
54 > *    <td>{@link #removeLast removeLast()}</td>
55 > *    <td>{@link #pollLast pollLast()}</td>
56 > *  </tr>
57 > *  <tr>
58 > *    <td><b>Examine</b></td>
59 > *    <td>{@link #firstElement firstElement()}</td>
60 > *    <td>{@link #peekFirst peekFirst()}</td>
61 > *    <td>{@link #lastElement lastElement()}</td>
62 > *    <td>{@link #peekLast peekLast()}</td>
63 > *  </tr>
64 > * </table>
65 > *
66 > * <p>This interface extends the {@link Queue} interface.  When a deque is
67 > * used as a queue, FIFO (First-In-First-Out) behavior results.  Elements are
68 > * added to the end of the deque and removed from the beginning.  The methods
69 > * inherited from the <tt>Queue</tt> interface are precisely equivalent to
70 > * <tt>Deque</tt> methods as indicated in the following table:<p>
71 > *
72 > * <table BORDER CELLPADDING=3 CELLSPACING=1>
73 > *  <tr>
74 > *    <td ALIGN=CENTER> <b><tt>Queue</tt> Method</b></td>
75 > *    <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
76 > *  </tr>
77 > *  <tr>
78 > *   <tr>
79 > *    <td>{@link java.util.Queue#offer offer(e)}</td>
80 > *    <td>{@link #offerLast offerLast(e)}</td>
81 > *   </tr>
82 > *   <tr>
83 > *    <td>{@link java.util.Queue#add add(e)}</td>
84 > *    <td>{@link #addLast addLast(e)}</td>
85 > *   </tr>
86 > *   <tr>
87 > *    <td>{@link java.util.Queue#poll poll()}</td>
88 > *    <td>{@link #pollFirst pollFirst()}</td>
89 > *   </tr>
90 > *   <tr>
91 > *    <td>{@link java.util.Queue#remove remove()}</td>
92 > *    <td>{@link #removeFirst removeFirst()}</td>
93 > *   </tr>
94 > *   <tr>
95 > *    <td>{@link java.util.Queue#peek peek()}</td>
96 > *    <td>{@link #peek peekFirst()}</td>
97 > *   </tr>
98 > *   <tr>
99 > *    <td>{@link java.util.Queue#element element()}</td>
100 > *    <td>{@link #firstElement firstElement()}</td>
101 > *   </tr>
102 > * </table>
103 > *
104 > * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks.  This
105 > * interface should be used in preference to the legacy {@link Stack} class.
106 > * When a dequeue is used as a stack, elements are pushed and popped from the
107 > * beginning of the deque.  Stack methods are precisely equivalent to
108 > * <tt>Deque</tt> methods as indicated in the table below:<p>
109 > *
110 > * <table BORDER CELLPADDING=3 CELLSPACING=1>
111 > *  <tr>
112 > *    <td ALIGN=CENTER> <b>Stack Method</b></td>
113 > *    <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
114 > *  </tr>
115 > *  <tr>
116 > *   <tr>
117 > *    <td>{@link #push push(e)}</td>
118 > *    <td>{@link #addFirst addFirst(e)}</td>
119 > *   </tr>
120 > *   <tr>
121 > *    <td>{@link #pop pop()}</td>
122 > *    <td>{@link #removeFirst removeFirst()}</td>
123 > *   </tr>
124 > *   <tr>
125 > *    <td>{@link #peek peek()}</td>
126 > *    <td>{@link #peekFirst peekFirst()}</td>
127 > *   </tr>
128 > * </table>
129 > * <p>Note that the {@link #peek peek} method works equally well when a deque
130 > * is used as a queue or a stack; in either case, elements are drawn from the
131 > * beginning of the deque.
132 > *
133 > * <p>This inteface provides two methods to to remove interior elements,
134 > * {@link #removeFirstOccurrence removeFirstOccurrence} and {@link
135 > * #removeLastOccurrence removeLastOccurrence}.  Unlike the {@link List}
136 > * interface, this interface does not provide support for indexed access to
137 > * elements.
138 > *
139 > * <p>While <tt>Deque</tt> implementations are not strictly required to
140 > * prohibit the insertion of null elements, they are strongly encouraged to do
141 > * so.  Users of any <tt>Deque</tt> implementations that do allow null
142 > * elements are strongly encouraged <i>not</i> to take advantage of the
143 > * ability to insert nulls.  This is so because <tt>null</tt> is used as a
144 > * special return value by various methods to indicated that the deque is
145 > * empty.
146   *
147   * <p><tt>Deque</tt> implementations generally do not define
148 < * element-based versions of methods <tt>equals</tt> and
149 < * <tt>hashCode</tt> but instead inherit the identity based versions
150 < * from class <tt>Object</tt>.
148 > * element-based versions of the <tt>equals</tt> and <tt>hashCode</tt>
149 > * methods, but instead inherit the identity-based versions from class
150 > * <tt>Object</tt>.
151   *
152   * <p>This interface is a member of the <a
153   * href="{@docRoot}/../guide/collections/index.html"> Java Collections
154   * Framework</a>.
155   *
156   * @author Doug Lea
157 + * @author Josh Bloch
158 + * @since  1.6
159   * @param <E> the type of elements held in this collection
160   */
161 < public interface Deque<E> extends Collection<E> {
62 <
161 > public interface Deque<E> extends Queue<E> {
162      /**
163 <     * Inserts the specified element to the front this deque, if
164 <     * possible.  When using deques that may impose insertion
165 <     * restrictions (for example capacity bounds), method
166 <     * <tt>offerFirst</tt> is generally preferable to method
68 <     * <tt>addFirst</tt> which can fail to insert a non-duplicate
69 <     * element only by throwing an exception.
163 >     * Inserts the specified element to the front this deque unless it would
164 >     * violate capacity restrictions.  When using a capacity-restricted deque,
165 >     * this method is generally preferable to method <tt>addFirst</tt>, which
166 >     * can fail to insert an element only by throwing an exception.
167       *
168 <     * @param o the element to insert.
169 <     * @return <tt>true</tt> if it was possible to add the element to
170 <     * this deque, else <tt>false</tt>
168 >     * @param e the element to insert
169 >     * @return <tt>true</tt> if it was possible to insert the element,
170 >     *     else <tt>false</tt>
171 >     * @throws NullPointerException if <tt>e</tt> is null and this
172 >     *     deque does not permit null elements
173       */
174 <    boolean offerFirst(E o);
174 >    boolean offerFirst(E e);
175  
176      /**
177 <     * Inserts the specified element to the end this deque, if
178 <     * possible.  When using deques that may impose insertion
179 <     * restrictions (for example capacity bounds), method
180 <     * <tt>offerFirst</tt> is generally preferable to method
82 <     * <tt>addLast</tt> which can fail to insert a non-duplicate
83 <     * element only by throwing an exception.
177 >     * Inserts the specified element to the end of this deque unless it would
178 >     * violate capacity restrictions.  When using a capacity-restricted deque,
179 >     * this method is generally preferable to method <tt>addLast</tt> which
180 >     * can fail to insert an element only by throwing an exception.
181       *
182 <     * @param o the element to insert.
183 <     * @return <tt>true</tt> if it was possible to add the element to
184 <     * this deque, else <tt>false</tt>
182 >     * @param e the element to insert
183 >     * @return <tt>true</tt> if it was possible to insert the element,
184 >     *     else <tt>false</tt>
185 >     * @throws NullPointerException if <tt>e</tt> is null and this
186 >     *     deque does not permit null elements
187       */
188 <    boolean offerLast(E o);
188 >    boolean offerLast(E e);
189  
190      /**
191 <     * Inserts the specified element to the front this deque, if
192 <     * this deque permits insertion of the given element.  This
94 <     * method has the same semantics as {@link Collection#add}.
191 >     * Inserts the specified element to the front of this deque unless it
192 >     * would violate capacity restrictions.
193       *
194 <     * @param o the element to insert.
195 <     * @return <tt>true</tt> if this deque changed as a result of this
196 <     * call, else <tt>false</tt>
194 >     * @param e the element to insert
195 >     * @throws IllegalStateException if it was not possible to insert
196 >     *    the element due to capacity restrictions
197 >     * @throws NullPointerException if <tt>e</tt> is null and this
198 >     *     deque does not permit null elements
199       */
200 <    boolean addFirst(E o);
200 >    void addFirst(E e);
201  
202      /**
203 <     * Inserts the specified element to the end this deque, if
204 <     * this deque permits insertion of the given element.  This
105 <     * method has the same semantics as {@link Collection#add}.
203 >     * Inserts the specified element to the end of this deque unless it would
204 >     * violate capacity restrictions.
205       *
206 <     * @param o the element to insert.
207 <     * @return <tt>true</tt> if this deque changed as a result of this
208 <     * call, else <tt>false</tt>
206 >     * @param e the element to insert
207 >     * @throws IllegalStateException if it was not possible to insert
208 >     *    the element due to capacity restrictions
209 >     * @throws NullPointerException if <tt>e</tt> is null and this
210 >     *     deque does not permit null elements
211       */
212 <    boolean addLast(E o);
212 >    void addLast(E e);
213  
214      /**
215       * Retrieves and removes the first element of this deque, or
216       * <tt>null</tt> if this deque is empty.
217       *
218       * @return the first element of this deque, or <tt>null</tt> if
219 <     * this deque is empty.
219 >     *     this deque is empty
220       */
221      E pollFirst();
222  
# Line 124 | Line 225 | public interface Deque<E> extends Collec
225       * <tt>null</tt> if this deque is empty.
226       *
227       * @return the last element of this deque, or <tt>null</tt> if
228 <     * this deque is empty.
228 >     *     this deque is empty
229       */
230      E pollLast();
231  
232      /**
233 <     * Retrieves and removes the first element of this deque.  This method
234 <     * differs from the <tt>pollFirst</tt> method in that it throws an
233 >     * Removes and returns the first element of this deque.  This method
234 >     * differs from the <tt>pollFirst</tt> method only in that it throws an
235       * exception if this deque is empty.
236       *
237 <     * @return the first element of this deque.
238 <     * @throws NoSuchElementException if this deque is empty.
237 >     * @return the first element of this deque
238 >     * @throws NoSuchElementException if this deque is empty
239       */
240      E removeFirst();
241  
242      /**
243       * Retrieves and removes the last element of this deque.  This method
244 <     * differs from the <tt>pollLast</tt> method in that it throws an
244 >     * differs from the <tt>pollLast</tt> method only in that it throws an
245       * exception if this deque is empty.
246       *
247 <     * @return the last element of this deque.
248 <     * @throws NoSuchElementException if this deque is empty.
247 >     * @return the last element of this deque
248 >     * @throws NoSuchElementException if this deque is empty
249       */
250      E removeLast();
251  
# Line 153 | Line 254 | public interface Deque<E> extends Collec
254       * returning <tt>null</tt> if this deque is empty.
255       *
256       * @return the first element of this deque, or <tt>null</tt> if
257 <     * this deque is empty.
257 >     *     this deque is empty
258       */
259      E peekFirst();
260  
# Line 162 | Line 263 | public interface Deque<E> extends Collec
263       * returning <tt>null</tt> if this deque is empty.
264       *
265       * @return the last element of this deque, or <tt>null</tt> if this deque
266 <     * is empty.
266 >     *     is empty
267       */
268      E peekLast();
269  
# Line 171 | Line 272 | public interface Deque<E> extends Collec
272       * deque.  This method differs from the <tt>peek</tt> method only
273       * in that it throws an exception if this deque is empty.
274       *
275 <     * @return the first element of this deque.
276 <     * @throws NoSuchElementException if this deque is empty.
275 >     * @return the first element of this deque
276 >     * @throws NoSuchElementException if this deque is empty
277       */
278 <    E getFirst();
278 >    E firstElement();
279  
280      /**
281       * Retrieves, but does not remove, the last element of this
282       * deque.  This method differs from the <tt>peek</tt> method only
283       * in that it throws an exception if this deque is empty.
284       *
285 <     * @return the last element of this deque.
286 <     * @throws NoSuchElementException if this deque is empty.
285 >     * @return the last element of this deque
286 >     * @throws NoSuchElementException if this deque is empty
287       */
288 <    E getLast();
288 >    E lastElement();
289  
290      /**
291       * Removes the first occurrence of the specified element in this
# Line 193 | Line 294 | public interface Deque<E> extends Collec
294       * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
295       * such an element exists).
296       *
297 <     * @param o element to be removed from this deque, if present.
298 <     * @return <tt>true</tt> if the deque contained the specified element.
297 >     * @param e element to be removed from this deque, if present
298 >     * @return <tt>true</tt> if the deque contained the specified element
299       * @throws NullPointerException if the specified element is <tt>null</tt>
300       */
301 <    boolean removeFirstOccurrence(E o);
301 >    boolean removeFirstOccurrence(Object e);
302  
303      /**
304       * Removes the last occurrence of the specified element in this
# Line 206 | Line 307 | public interface Deque<E> extends Collec
307       * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
308       * such an element exists).
309       *
310 <     * @param o element to be removed from this deque, if present.
311 <     * @return <tt>true</tt> if the deque contained the specified element.
310 >     * @param e element to be removed from this deque, if present
311 >     * @return <tt>true</tt> if the deque contained the specified element
312       * @throws NullPointerException if the specified element is <tt>null</tt>
313       */
314 <    boolean removeLastOccurrence(E o);
314 >    boolean removeLastOccurrence(Object e);
315 >
316 >
317 >    // *** Queue methods ***
318  
319      /**
320 <     * Returns a view of this deque as a first-in-first-out queue,
321 <     * mapping {@link Queue#offer} to <tt>offerLast</tt>, {@link
322 <     * Queue#poll} to <tt>pollFirst</tt>, and other operations
323 <     * accordingly.
324 <     * @return a first-in-first-out view of this deque.
320 >     * Inserts the specified element into the queue represented by this deque
321 >     * unless it would violate capacity restrictions.  In other words, inserts
322 >     * the specified element to the end of this deque.  When using a
323 >     * capacity-restricted deque, this method is generally preferable to the
324 >     * {@link #add} method, which can fail to insert an element only by
325 >     * throwing an exception.
326 >     *
327 >     * <p>This method is equivalent to {@link #offerLast}.
328 >     *
329 >     * @param e the element to insert
330 >     * @return <tt>true</tt> if it was possible to insert the element,
331 >     *     else <tt>false</tt>
332 >     * @throws NullPointerException if <tt>e</tt> is null and this
333 >     *     deque does not permit null elements
334       */
335 <    public Queue<E> asFifoQueue();
335 >    boolean offer(E e);
336  
337      /**
338 <     * Returns a view of this deque as a last-in-first-out stack,
339 <     * mapping {@link Queue#offer} to <tt>offerFirst</tt>, {@link
340 <     * Queue#poll} to <tt>pollFirst</tt>, and other operations
341 <     * accordingly.
342 <     * @return a first-in-last-out view of this deque.
338 >     * Inserts the specified element into the queue represented by this
339 >     * deque unless it would violate capacity restrictions.  In other words,
340 >     * inserts the specified element as the last element of this deque.
341 >     *
342 >     * <p>This method is equivalent to {@link #addLast}.
343 >     *
344 >     * @param e the element to insert
345 >     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
346 >     * @throws IllegalStateException if it was not possible to insert
347 >     *    the element due to capacity restrictions
348 >     * @throws NullPointerException if <tt>e</tt> is null and this
349 >     *     deque does not permit null elements
350 >     */
351 >    boolean add(E e);
352 >
353 >    /**
354 >     * Retrieves and removes the head of the queue represented by
355 >     * this deque, or <tt>null</tt> if this deque is empty.  In other words,
356 >     * retrieves and removes the first element of this deque, or <tt>null</tt>
357 >     * if this deque is empty.
358 >     *
359 >     * <p>This method is equivalent to {@link #pollFirst()}.
360 >     *
361 >     * @return the first element of this deque, or <tt>null</tt> if
362 >     *     this deque is empty
363 >     */
364 >    E poll();
365 >
366 >    /**
367 >     * Retrieves and removes the head of the queue represented by this deque.
368 >     * This method differs from the <tt>poll</tt> method only in that it
369 >     * throws an exception if this deque is empty.
370 >     *
371 >     * <p>This method is equivalent to {@link #removeFirst()}.
372 >     *
373 >     * @return the head of the queue represented by this deque
374 >     * @throws NoSuchElementException if this deque is empty
375       */
376 <    public Queue<E> asLifoQueue();
376 >    E remove();
377  
378 +    /**
379 +     * Retrieves, but does not remove, the head of the queue represented by
380 +     * this deque, returning <tt>null</tt> if this deque is empty.
381 +     *
382 +     * <p>This method is equivalent to {@link #peekFirst()}
383 +     *
384 +     * @return the head of the queue represented by this deque, or
385 +     *     <tt>null</tt> if this deque is empty
386 +     */
387 +    E peek();
388 +
389 +    /**
390 +     * Retrieves, but does not remove, the head of the queue represented by
391 +     * this deque.  This method differs from the <tt>peek</tt> method only in
392 +     * that it throws an exception if this deque is empty.
393 +     *
394 +     * <p>This method is equivalent to {@link #firstElement()}
395 +     *
396 +     * @return the head of the queue represented by this deque
397 +     * @throws NoSuchElementException if this deque is empty
398 +     */
399 +    E element();
400 +
401 +
402 +    // *** Stack methods ***
403 +
404 +    /**
405 +     * Pushes an element onto the stack represented by this deque.  In other
406 +     * words, inserts the element to the front this deque unless it would
407 +     * violate capacity restrictions.
408 +     *
409 +     * <p>This method is equivalent to {@link #addFirst}.
410 +     *
411 +     * @throws IllegalStateException if it was not possible to insert
412 +     *    the element due to capacity restrictions
413 +     * @throws NullPointerException if <tt>e</tt> is null and this
414 +     *     deque does not permit null elements
415 +     */
416 +    void push(E e);
417 +
418 +    /**
419 +     * Pops an element from the stack represented by this deque.  In other
420 +     * words, removes and returns the the first element of this deque.
421 +     *
422 +     * <p>This method is equivalent to {@link #removeFirst()}.
423 +     *
424 +     * @return the element at the front of this deque (which is the top
425 +     *     of the stack represented by this deque)
426 +     * @throws NoSuchElementException if this deque is empty
427 +     */
428 +    E pop();
429 +
430 +
431 +    // *** Collection Method ***
432 +
433 +    /**
434 +     * Returns an iterator over the elements in this deque.  The elements
435 +     * will be ordered from first (head) to last (tail).
436 +     *
437 +     * @return an <tt>Iterator</tt> over the elements in this deque
438 +     */
439 +    Iterator<E> iterator();
440   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines