ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166x/Deque.java
Revision: 1.13
Committed: Sun Jan 18 20:17:33 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +1 -0 lines
Log Message:
exactly one blank line before and after package statements

File Contents

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