ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Deque.java
Revision: 1.6
Committed: Mon May 2 04:19:58 2005 UTC (19 years ago) by jsr166
Branch: MAIN
Changes since 1.5: +6 -6 lines
Log Message:
doc fixes

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 jsr166 1.5 * following table:
29 dl 1.3 *
30 jsr166 1.5 * <p><table BORDER CELLPADDING=3 CELLSPACING=1>
31 dl 1.1 * <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 jsr166 1.5 * <tt>Deque</tt> methods as indicated in the following table:
71 dl 1.1 *
72 jsr166 1.5 * <p><table BORDER CELLPADDING=3 CELLSPACING=1>
73 dl 1.1 * <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     * <td>{@link java.util.Queue#offer offer(e)}</td>
79     * <td>{@link #offerLast offerLast(e)}</td>
80 jsr166 1.5 * </tr>
81     * <tr>
82 dl 1.1 * <td>{@link java.util.Queue#add add(e)}</td>
83     * <td>{@link #addLast addLast(e)}</td>
84 jsr166 1.5 * </tr>
85     * <tr>
86 dl 1.1 * <td>{@link java.util.Queue#poll poll()}</td>
87     * <td>{@link #pollFirst pollFirst()}</td>
88 jsr166 1.5 * </tr>
89     * <tr>
90 dl 1.1 * <td>{@link java.util.Queue#remove remove()}</td>
91     * <td>{@link #removeFirst removeFirst()}</td>
92 jsr166 1.5 * </tr>
93     * <tr>
94 dl 1.1 * <td>{@link java.util.Queue#peek peek()}</td>
95     * <td>{@link #peek peekFirst()}</td>
96 jsr166 1.5 * </tr>
97     * <tr>
98 dl 1.1 * <td>{@link java.util.Queue#element element()}</td>
99     * <td>{@link #getFirst getFirst()}</td>
100 jsr166 1.5 * </tr>
101 dl 1.1 * </table>
102     *
103     * <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This
104     * interface should be used in preference to the legacy {@link Stack} class.
105 dl 1.3 * When a deque is used as a stack, elements are pushed and popped from the
106 dl 1.1 * beginning of the deque. Stack methods are precisely equivalent to
107 jsr166 1.5 * <tt>Deque</tt> methods as indicated in the table below:
108 dl 1.1 *
109 jsr166 1.5 * <p><table BORDER CELLPADDING=3 CELLSPACING=1>
110 dl 1.1 * <tr>
111     * <td ALIGN=CENTER> <b>Stack Method</b></td>
112     * <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
113     * </tr>
114     * <tr>
115     * <td>{@link #push push(e)}</td>
116     * <td>{@link #addFirst addFirst(e)}</td>
117 jsr166 1.5 * </tr>
118     * <tr>
119 dl 1.1 * <td>{@link #pop pop()}</td>
120     * <td>{@link #removeFirst removeFirst()}</td>
121 jsr166 1.5 * </tr>
122     * <tr>
123 dl 1.1 * <td>{@link #peek peek()}</td>
124     * <td>{@link #peekFirst peekFirst()}</td>
125 jsr166 1.5 * </tr>
126 dl 1.1 * </table>
127     *
128     * <p>Note that the {@link #peek peek} method works equally well when
129     * a deque is used as a queue or a stack; in either case, elements are
130     * drawn from the beginning of the deque.
131     *
132 dl 1.2 * <p>This interface provides two methods to remove interior
133 dl 1.1 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
134     * {@link #removeLastOccurrence removeLastOccurrence}. Unlike the
135     * {@link List} interface, this interface does not provide support for
136     * indexed access to elements.
137     *
138     * <p>While <tt>Deque</tt> implementations are not strictly required
139     * to prohibit the insertion of null elements, they are strongly
140     * encouraged to do so. Users of any <tt>Deque</tt> implementations
141     * that do allow null elements are strongly encouraged <i>not</i> to
142     * take advantage of the ability to insert nulls. This is so because
143     * <tt>null</tt> is used as a special return value by various methods
144     * to indicated that the deque is empty.
145 dl 1.3 *
146 dl 1.1 * <p><tt>Deque</tt> implementations generally do not define
147     * element-based versions of the <tt>equals</tt> and <tt>hashCode</tt>
148     * methods, but instead inherit the identity-based versions from class
149     * <tt>Object</tt>.
150     *
151     * <p>This interface is a member of the <a
152     * href="{@docRoot}/../guide/collections/index.html"> Java Collections
153     * Framework</a>.
154     *
155     * @author Doug Lea
156     * @author Josh Bloch
157     * @since 1.6
158     * @param <E> the type of elements held in this collection
159     */
160    
161     public interface Deque<E> extends Queue<E> {
162     /**
163 dl 1.3 * Inserts the specified element at the front of this deque unless it would
164 dl 1.1 * 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 e the element to insert
169     * @return <tt>true</tt> if it was possible to insert the element,
170     * else <tt>false</tt>
171 dl 1.3 * @throws NullPointerException if the specified element is null and this
172 dl 1.1 * deque does not permit null elements
173     */
174     boolean offerFirst(E e);
175    
176     /**
177 dl 1.4 * Inserts the specified element at the end of this deque unless it would
178 dl 1.1 * 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 e the element to insert
183     * @return <tt>true</tt> if it was possible to insert the element,
184     * else <tt>false</tt>
185 dl 1.3 * @throws NullPointerException if the specified element is null and this
186 dl 1.1 * deque does not permit null elements
187     */
188     boolean offerLast(E e);
189    
190     /**
191 dl 1.3 * Inserts the specified element at the front of this deque unless it
192 dl 1.1 * would violate capacity restrictions.
193     *
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 dl 1.3 * @throws NullPointerException if the specified element is null and this
198 dl 1.1 * deque does not permit null elements
199     */
200     void addFirst(E e);
201    
202     /**
203 dl 1.4 * Inserts the specified element at the end of this deque unless it would
204 dl 1.1 * violate capacity restrictions.
205     *
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 dl 1.3 * @throws NullPointerException if the specified element is null and this
210 dl 1.1 * deque does not permit null elements
211     */
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
220     */
221     E pollFirst();
222    
223     /**
224     * Retrieves and removes the last element of this deque, or
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
229     */
230     E pollLast();
231    
232     /**
233 jsr166 1.5 * Retrieves and removes the first element of this deque. This method
234 jsr166 1.6 * differs from the {@link #pollFirst} method only in that it throws an
235 dl 1.1 * exception if this deque is empty.
236     *
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 jsr166 1.6 * differs from the {@link #pollLast} method only in that it throws an
245 dl 1.1 * exception if this deque is empty.
246     *
247     * @return the last element of this deque
248     * @throws NoSuchElementException if this deque is empty
249     */
250     E removeLast();
251    
252     /**
253     * Retrieves, but does not remove, the first element of this deque,
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
258     */
259     E peekFirst();
260    
261     /**
262     * Retrieves, but does not remove, the last element of this deque,
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
267     */
268     E peekLast();
269    
270     /**
271     * Retrieves, but does not remove, the first element of this
272 jsr166 1.6 * deque. This method differs from the {@link #peekFirst} method only
273 dl 1.1 * 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
277     */
278     E getFirst();
279    
280     /**
281     * Retrieves, but does not remove, the last element of this
282 jsr166 1.6 * deque. This method differs from the {@link #peekLast} method only
283 dl 1.1 * 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
287     */
288     E getLast();
289    
290     /**
291     * Removes the first occurrence of the specified element in this
292     * deque. If the deque does not contain the element, it is
293     * unchanged. More formally, removes the first element <tt>e</tt>
294     * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
295     * such an element exists).
296     *
297 dl 1.3 * @param o element to be removed from this deque, if present
298 dl 1.1 * @return <tt>true</tt> if the deque contained the specified element
299 dl 1.4 * @throws NullPointerException if the specified element is null and this
300     * deque does not permit null elements
301 dl 1.1 */
302 dl 1.3 boolean removeFirstOccurrence(Object o);
303 dl 1.1
304     /**
305     * Removes the last occurrence of the specified element in this
306     * deque. If the deque does not contain the element, it is
307     * unchanged. More formally, removes the last element <tt>e</tt>
308     * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
309     * such an element exists).
310     *
311 dl 1.3 * @param o element to be removed from this deque, if present
312 dl 1.1 * @return <tt>true</tt> if the deque contained the specified element
313 dl 1.4 * @throws NullPointerException if the specified element is null and this
314     * deque does not permit null elements
315 dl 1.1 */
316 dl 1.3 boolean removeLastOccurrence(Object o);
317 dl 1.1
318    
319     // *** Queue methods ***
320    
321     /**
322     * Inserts the specified element into the queue represented by this deque
323     * unless it would violate capacity restrictions. In other words, inserts
324 dl 1.4 * the specified element at the end of this deque. When using a
325 dl 1.1 * capacity-restricted deque, this method is generally preferable to the
326     * {@link #add} method, which can fail to insert an element only by
327     * throwing an exception.
328     *
329     * <p>This method is equivalent to {@link #offerLast}.
330     *
331     * @param e the element to insert
332     * @return <tt>true</tt> if it was possible to insert the element,
333     * else <tt>false</tt>
334 dl 1.3 * @throws NullPointerException if the specified element is null and this
335 dl 1.1 * deque does not permit null elements
336     */
337     boolean offer(E e);
338    
339     /**
340     * Inserts the specified element into the queue represented by this
341     * deque unless it would violate capacity restrictions. In other words,
342 dl 1.3 * inserts the specified element as the last element of this deque.
343 dl 1.1 *
344     * <p>This method is equivalent to {@link #addLast}.
345     *
346     * @param e the element to insert
347     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
348     * @throws IllegalStateException if it was not possible to insert
349     * the element due to capacity restrictions
350 dl 1.3 * @throws NullPointerException if the specified element is null and this
351 dl 1.1 * deque does not permit null elements
352     */
353     boolean add(E e);
354    
355     /**
356     * Retrieves and removes the head of the queue represented by
357     * this deque, or <tt>null</tt> if this deque is empty. In other words,
358     * retrieves and removes the first element of this deque, or <tt>null</tt>
359     * if this deque is empty.
360     *
361     * <p>This method is equivalent to {@link #pollFirst()}.
362     *
363     * @return the first element of this deque, or <tt>null</tt> if
364     * this deque is empty
365     */
366     E poll();
367    
368     /**
369     * Retrieves and removes the head of the queue represented by this deque.
370 jsr166 1.6 * This method differs from the {@link #poll} method only in that it
371 dl 1.1 * throws an exception if this deque is empty.
372     *
373     * <p>This method is equivalent to {@link #removeFirst()}.
374     *
375     * @return the head of the queue represented by this deque
376     * @throws NoSuchElementException if this deque is empty
377     */
378     E remove();
379    
380     /**
381     * Retrieves, but does not remove, the head of the queue represented by
382     * this deque, returning <tt>null</tt> if this deque is empty.
383     *
384 dl 1.3 * <p>This method is equivalent to {@link #peekFirst()}.
385 dl 1.1 *
386     * @return the head of the queue represented by this deque, or
387     * <tt>null</tt> if this deque is empty
388     */
389     E peek();
390    
391     /**
392     * Retrieves, but does not remove, the head of the queue represented by
393 jsr166 1.6 * this deque. This method differs from the {@link #peek} method only in
394 dl 1.1 * that it throws an exception if this deque is empty.
395     *
396 dl 1.3 * <p>This method is equivalent to {@link #getFirst()}.
397 dl 1.1 *
398     * @return the head of the queue represented by this deque
399     * @throws NoSuchElementException if this deque is empty
400     */
401     E element();
402    
403    
404     // *** Stack methods ***
405    
406     /**
407     * Pushes an element onto the stack represented by this deque. In other
408 dl 1.3 * words, inserts the element at the front of this deque unless it would
409 dl 1.1 * violate capacity restrictions.
410     *
411     * <p>This method is equivalent to {@link #addFirst}.
412     *
413 dl 1.3 * @param e the element to push
414 dl 1.1 * @throws IllegalStateException if it was not possible to insert
415     * the element due to capacity restrictions
416 dl 1.3 * @throws NullPointerException if the specified element is null and this
417 dl 1.1 * deque does not permit null elements
418     */
419     void push(E e);
420    
421     /**
422     * Pops an element from the stack represented by this deque. In other
423 dl 1.2 * words, removes and returns the first element of this deque.
424 dl 1.1 *
425     * <p>This method is equivalent to {@link #removeFirst()}.
426     *
427     * @return the element at the front of this deque (which is the top
428     * of the stack represented by this deque)
429     * @throws NoSuchElementException if this deque is empty
430     */
431     E pop();
432    
433    
434     // *** Collection Method ***
435    
436     /**
437     * Returns an iterator over the elements in this deque. The elements
438     * will be ordered from first (head) to last (tail).
439 dl 1.3 *
440 dl 1.1 * @return an <tt>Iterator</tt> over the elements in this deque
441     */
442     Iterator<E> iterator();
443     }