ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166x/Deque.java
Revision: 1.3
Committed: Sun Dec 5 21:15:31 2004 UTC (19 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.2: +323 -116 lines
Log Message:
Revised Deque interface; new BlockingDeque interface and implementations

File Contents

# Content
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 jsr166x; // XXX This belongs in java.util!!! XXX
8 import java.util.*; // XXX This import goes away XXX
9
10 /**
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 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 Queue<E> {
162 /**
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 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 e);
175
176 /**
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 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 e);
189
190 /**
191 * Inserts the specified element to the front of this deque unless it
192 * 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 * @throws NullPointerException if <tt>e</tt> is null and this
198 * deque does not permit null elements
199 */
200 void addFirst(E e);
201
202 /**
203 * Inserts the specified element to the end of this deque unless it would
204 * 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 * @throws NullPointerException if <tt>e</tt> is null and this
210 * 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 * 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
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 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
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 * 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
277 */
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
287 */
288 E lastElement();
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 * @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(Object e);
302
303 /**
304 * Removes the last occurrence of the specified element in this
305 * deque. If the deque does not contain the element, it is
306 * unchanged. More formally, removes the last element <tt>e</tt>
307 * such that <tt>(o==null ? e==null : o.equals(e))</tt> (if
308 * such an element exists).
309 *
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(Object e);
315
316
317 // *** Queue methods ***
318
319 /**
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 boolean offer(E e);
336
337 /**
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 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 }