ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Deque.java
Revision: 1.4
Committed: Tue Mar 22 16:48:32 2005 UTC (19 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.3: +8 -6 lines
Log Message:
Documentation improvements

File Contents

# User Rev Content
1 dl 1.1 /*
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 java.util;
8    
9     /**
10     * A linear collection that supports element insertion and removal at
11     * both ends. The name <i>deque</i> is short for "double ended queue"
12     * and is usually pronounced "deck". Most <tt>Deque</tt>
13     * implementations place no fixed limits on the number of elements
14     * they may contain, but this interface supports capacity-restricted
15     * deques as well as those with no fixed size limit.
16     *
17     * <p>This interface defines methods to access the elements at both
18     * ends of the deque. Methods are provided to insert, remove, and
19     * examine the element. Each of these methods exists in two forms:
20     * one throws an exception if the operation fails, the other returns a
21     * special value (either <tt>null</tt> or <tt>false</tt>, depending on
22     * the operation). The latter form of the insert operation is
23     * designed specifically for use with capacity-restricted
24     * <tt>Deque</tt> implementations; in most implementations, insert
25     * operations cannot fail.
26     *
27 dl 1.3 * <p>The twelve methods described above are summarized in the
28 dl 1.2 * following table:<p>
29 dl 1.3 *
30 dl 1.1 * <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 #getFirst getFirst()}</td>
60     * <td>{@link #peekFirst peekFirst()}</td>
61     * <td>{@link #getLast getLast()}</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 dl 1.4 * added at the end of the deque and removed from the beginning. The methods
69 dl 1.1 * 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 #getFirst getFirst()}</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 dl 1.3 * When a deque is used as a stack, elements are pushed and popped from the
107 dl 1.1 * 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     *
130     * <p>Note that the {@link #peek peek} method works equally well when
131     * a deque is used as a queue or a stack; in either case, elements are
132     * drawn from the beginning of the deque.
133     *
134 dl 1.2 * <p>This interface provides two methods to remove interior
135 dl 1.1 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
136     * {@link #removeLastOccurrence removeLastOccurrence}. Unlike the
137     * {@link List} interface, this interface does not provide support for
138     * indexed access to elements.
139     *
140     * <p>While <tt>Deque</tt> implementations are not strictly required
141     * to prohibit the insertion of null elements, they are strongly
142     * encouraged to do so. Users of any <tt>Deque</tt> implementations
143     * that do allow null elements are strongly encouraged <i>not</i> to
144     * take advantage of the ability to insert nulls. This is so because
145     * <tt>null</tt> is used as a special return value by various methods
146     * to indicated that the deque is empty.
147 dl 1.3 *
148 dl 1.1 * <p><tt>Deque</tt> implementations generally do not define
149     * element-based versions of the <tt>equals</tt> and <tt>hashCode</tt>
150     * methods, but instead inherit the identity-based versions from class
151     * <tt>Object</tt>.
152     *
153     * <p>This interface is a member of the <a
154     * href="{@docRoot}/../guide/collections/index.html"> Java Collections
155     * Framework</a>.
156     *
157     * @author Doug Lea
158     * @author Josh Bloch
159     * @since 1.6
160     * @param <E> the type of elements held in this collection
161     */
162    
163     public interface Deque<E> extends Queue<E> {
164     /**
165 dl 1.3 * Inserts the specified element at the front of this deque unless it would
166 dl 1.1 * violate capacity restrictions. When using a capacity-restricted deque,
167     * this method is generally preferable to method <tt>addFirst</tt>, which
168     * can fail to insert an element only by throwing an exception.
169     *
170     * @param e the element to insert
171     * @return <tt>true</tt> if it was possible to insert the element,
172     * else <tt>false</tt>
173 dl 1.3 * @throws NullPointerException if the specified element is null and this
174 dl 1.1 * deque does not permit null elements
175     */
176     boolean offerFirst(E e);
177    
178     /**
179 dl 1.4 * Inserts the specified element at the end of this deque unless it would
180 dl 1.1 * violate capacity restrictions. When using a capacity-restricted deque,
181     * this method is generally preferable to method <tt>addLast</tt> which
182     * can fail to insert an element only by throwing an exception.
183     *
184     * @param e the element to insert
185     * @return <tt>true</tt> if it was possible to insert the element,
186     * else <tt>false</tt>
187 dl 1.3 * @throws NullPointerException if the specified element is null and this
188 dl 1.1 * deque does not permit null elements
189     */
190     boolean offerLast(E e);
191    
192     /**
193 dl 1.3 * Inserts the specified element at the front of this deque unless it
194 dl 1.1 * would violate capacity restrictions.
195     *
196     * @param e the element to insert
197     * @throws IllegalStateException if it was not possible to insert
198     * the element due to capacity restrictions
199 dl 1.3 * @throws NullPointerException if the specified element is null and this
200 dl 1.1 * deque does not permit null elements
201     */
202     void addFirst(E e);
203    
204     /**
205 dl 1.4 * Inserts the specified element at the end of this deque unless it would
206 dl 1.1 * violate capacity restrictions.
207     *
208     * @param e the element to insert
209     * @throws IllegalStateException if it was not possible to insert
210     * the element due to capacity restrictions
211 dl 1.3 * @throws NullPointerException if the specified element is null and this
212 dl 1.1 * deque does not permit null elements
213     */
214     void addLast(E e);
215    
216     /**
217     * Retrieves and removes the first element of this deque, or
218     * <tt>null</tt> if this deque is empty.
219     *
220     * @return the first element of this deque, or <tt>null</tt> if
221     * this deque is empty
222     */
223     E pollFirst();
224    
225     /**
226     * Retrieves and removes the last element of this deque, or
227     * <tt>null</tt> if this deque is empty.
228     *
229     * @return the last element of this deque, or <tt>null</tt> if
230     * this deque is empty
231     */
232     E pollLast();
233    
234     /**
235     * Removes and returns the first element of this deque. This method
236     * differs from the <tt>pollFirst</tt> method only in that it throws an
237     * exception if this deque is empty.
238     *
239     * @return the first element of this deque
240     * @throws NoSuchElementException if this deque is empty
241     */
242     E removeFirst();
243    
244     /**
245     * Retrieves and removes the last element of this deque. This method
246     * differs from the <tt>pollLast</tt> method only in that it throws an
247     * exception if this deque is empty.
248     *
249     * @return the last element of this deque
250     * @throws NoSuchElementException if this deque is empty
251     */
252     E removeLast();
253    
254     /**
255     * Retrieves, but does not remove, the first element of this deque,
256     * returning <tt>null</tt> if this deque is empty.
257     *
258     * @return the first element of this deque, or <tt>null</tt> if
259     * this deque is empty
260     */
261     E peekFirst();
262    
263     /**
264     * Retrieves, but does not remove, the last element of this deque,
265     * returning <tt>null</tt> if this deque is empty.
266     *
267     * @return the last element of this deque, or <tt>null</tt> if this deque
268     * is empty
269     */
270     E peekLast();
271    
272     /**
273     * Retrieves, but does not remove, the first element of this
274 dl 1.3 * deque. This method differs from the <tt>peekFirst</tt> method only
275 dl 1.1 * in that it throws an exception if this deque is empty.
276     *
277     * @return the first element of this deque
278     * @throws NoSuchElementException if this deque is empty
279     */
280     E getFirst();
281    
282     /**
283     * Retrieves, but does not remove, the last element of this
284 dl 1.3 * deque. This method differs from the <tt>peekLast</tt> method only
285 dl 1.1 * in that it throws an exception if this deque is empty.
286     *
287     * @return the last element of this deque
288     * @throws NoSuchElementException if this deque is empty
289     */
290     E getLast();
291    
292     /**
293     * Removes the first occurrence of the specified element in this
294     * deque. If the deque does not contain the element, it is
295     * unchanged. More formally, removes the first element <tt>e</tt>
296     * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
297     * such an element exists).
298     *
299 dl 1.3 * @param o element to be removed from this deque, if present
300 dl 1.1 * @return <tt>true</tt> if the deque contained the specified element
301 dl 1.4 * @throws NullPointerException if the specified element is null and this
302     * deque does not permit null elements
303 dl 1.1 */
304 dl 1.3 boolean removeFirstOccurrence(Object o);
305 dl 1.1
306     /**
307     * Removes the last occurrence of the specified element in this
308     * deque. If the deque does not contain the element, it is
309     * unchanged. More formally, removes the last element <tt>e</tt>
310     * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
311     * such an element exists).
312     *
313 dl 1.3 * @param o element to be removed from this deque, if present
314 dl 1.1 * @return <tt>true</tt> if the deque contained the specified element
315 dl 1.4 * @throws NullPointerException if the specified element is null and this
316     * deque does not permit null elements
317 dl 1.1 */
318 dl 1.3 boolean removeLastOccurrence(Object o);
319 dl 1.1
320    
321     // *** Queue methods ***
322    
323     /**
324     * Inserts the specified element into the queue represented by this deque
325     * unless it would violate capacity restrictions. In other words, inserts
326 dl 1.4 * the specified element at the end of this deque. When using a
327 dl 1.1 * capacity-restricted deque, this method is generally preferable to the
328     * {@link #add} method, which can fail to insert an element only by
329     * throwing an exception.
330     *
331     * <p>This method is equivalent to {@link #offerLast}.
332     *
333     * @param e the element to insert
334     * @return <tt>true</tt> if it was possible to insert the element,
335     * else <tt>false</tt>
336 dl 1.3 * @throws NullPointerException if the specified element is null and this
337 dl 1.1 * deque does not permit null elements
338     */
339     boolean offer(E e);
340    
341     /**
342     * Inserts the specified element into the queue represented by this
343     * deque unless it would violate capacity restrictions. In other words,
344 dl 1.3 * inserts the specified element as the last element of this deque.
345 dl 1.1 *
346     * <p>This method is equivalent to {@link #addLast}.
347     *
348     * @param e the element to insert
349     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
350     * @throws IllegalStateException if it was not possible to insert
351     * the element due to capacity restrictions
352 dl 1.3 * @throws NullPointerException if the specified element is null and this
353 dl 1.1 * deque does not permit null elements
354     */
355     boolean add(E e);
356    
357     /**
358     * Retrieves and removes the head of the queue represented by
359     * this deque, or <tt>null</tt> if this deque is empty. In other words,
360     * retrieves and removes the first element of this deque, or <tt>null</tt>
361     * if this deque is empty.
362     *
363     * <p>This method is equivalent to {@link #pollFirst()}.
364     *
365     * @return the first element of this deque, or <tt>null</tt> if
366     * this deque is empty
367     */
368     E poll();
369    
370     /**
371     * Retrieves and removes the head of the queue represented by this deque.
372     * This method differs from the <tt>poll</tt> method only in that it
373     * throws an exception if this deque is empty.
374     *
375     * <p>This method is equivalent to {@link #removeFirst()}.
376     *
377     * @return the head of the queue represented by this deque
378     * @throws NoSuchElementException if this deque is empty
379     */
380     E remove();
381    
382     /**
383     * Retrieves, but does not remove, the head of the queue represented by
384     * this deque, returning <tt>null</tt> if this deque is empty.
385     *
386 dl 1.3 * <p>This method is equivalent to {@link #peekFirst()}.
387 dl 1.1 *
388     * @return the head of the queue represented by this deque, or
389     * <tt>null</tt> if this deque is empty
390     */
391     E peek();
392    
393     /**
394     * Retrieves, but does not remove, the head of the queue represented by
395     * this deque. This method differs from the <tt>peek</tt> method only in
396     * that it throws an exception if this deque is empty.
397     *
398 dl 1.3 * <p>This method is equivalent to {@link #getFirst()}.
399 dl 1.1 *
400     * @return the head of the queue represented by this deque
401     * @throws NoSuchElementException if this deque is empty
402     */
403     E element();
404    
405    
406     // *** Stack methods ***
407    
408     /**
409     * Pushes an element onto the stack represented by this deque. In other
410 dl 1.3 * words, inserts the element at the front of this deque unless it would
411 dl 1.1 * violate capacity restrictions.
412     *
413     * <p>This method is equivalent to {@link #addFirst}.
414     *
415 dl 1.3 * @param e the element to push
416 dl 1.1 * @throws IllegalStateException if it was not possible to insert
417     * the element due to capacity restrictions
418 dl 1.3 * @throws NullPointerException if the specified element is null and this
419 dl 1.1 * deque does not permit null elements
420     */
421     void push(E e);
422    
423     /**
424     * Pops an element from the stack represented by this deque. In other
425 dl 1.2 * words, removes and returns the first element of this deque.
426 dl 1.1 *
427     * <p>This method is equivalent to {@link #removeFirst()}.
428     *
429     * @return the element at the front of this deque (which is the top
430     * of the stack represented by this deque)
431     * @throws NoSuchElementException if this deque is empty
432     */
433     E pop();
434    
435    
436     // *** Collection Method ***
437    
438     /**
439     * Returns an iterator over the elements in this deque. The elements
440     * will be ordered from first (head) to last (tail).
441 dl 1.3 *
442 dl 1.1 * @return an <tt>Iterator</tt> over the elements in this deque
443     */
444     Iterator<E> iterator();
445     }