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 |
* <p>The twelve methods described above are summarized in the |
28 |
* following table: |
29 |
* |
30 |
* <p><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 |
* added at 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: |
71 |
* |
72 |
* <p><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 |
* <td>{@link java.util.Queue#offer offer(e)}</td> |
79 |
* <td>{@link #offerLast offerLast(e)}</td> |
80 |
* </tr> |
81 |
* <tr> |
82 |
* <td>{@link java.util.Queue#add add(e)}</td> |
83 |
* <td>{@link #addLast addLast(e)}</td> |
84 |
* </tr> |
85 |
* <tr> |
86 |
* <td>{@link java.util.Queue#poll poll()}</td> |
87 |
* <td>{@link #pollFirst pollFirst()}</td> |
88 |
* </tr> |
89 |
* <tr> |
90 |
* <td>{@link java.util.Queue#remove remove()}</td> |
91 |
* <td>{@link #removeFirst removeFirst()}</td> |
92 |
* </tr> |
93 |
* <tr> |
94 |
* <td>{@link java.util.Queue#peek peek()}</td> |
95 |
* <td>{@link #peek peekFirst()}</td> |
96 |
* </tr> |
97 |
* <tr> |
98 |
* <td>{@link java.util.Queue#element element()}</td> |
99 |
* <td>{@link #getFirst getFirst()}</td> |
100 |
* </tr> |
101 |
* </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 |
* When a deque is used as a stack, elements are pushed and popped from the |
106 |
* beginning of the deque. Stack methods are precisely equivalent to |
107 |
* <tt>Deque</tt> methods as indicated in the table below: |
108 |
* |
109 |
* <p><table BORDER CELLPADDING=3 CELLSPACING=1> |
110 |
* <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 |
* </tr> |
118 |
* <tr> |
119 |
* <td>{@link #pop pop()}</td> |
120 |
* <td>{@link #removeFirst removeFirst()}</td> |
121 |
* </tr> |
122 |
* <tr> |
123 |
* <td>{@link #peek peek()}</td> |
124 |
* <td>{@link #peekFirst peekFirst()}</td> |
125 |
* </tr> |
126 |
* </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 |
* <p>This interface provides two methods to remove interior |
133 |
* 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 |
* |
146 |
* <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 |
* Inserts the specified element at the front of 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 the specified element is null and this |
172 |
* deque does not permit null elements |
173 |
*/ |
174 |
boolean offerFirst(E e); |
175 |
|
176 |
/** |
177 |
* Inserts the specified element at 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 the specified element is null and this |
186 |
* deque does not permit null elements |
187 |
*/ |
188 |
boolean offerLast(E e); |
189 |
|
190 |
/** |
191 |
* Inserts the specified element at 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 the specified element is null and this |
198 |
* deque does not permit null elements |
199 |
*/ |
200 |
void addFirst(E e); |
201 |
|
202 |
/** |
203 |
* Inserts the specified element at 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 the specified element 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 |
* Retrieves and removes 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>peekFirst</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 getFirst(); |
279 |
|
280 |
/** |
281 |
* Retrieves, but does not remove, the last element of this |
282 |
* deque. This method differs from the <tt>peekLast</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 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 |
* @param o 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 null and this |
300 |
* deque does not permit null elements |
301 |
*/ |
302 |
boolean removeFirstOccurrence(Object o); |
303 |
|
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 |
* @param o element to be removed from this deque, if present |
312 |
* @return <tt>true</tt> if the deque contained the specified element |
313 |
* @throws NullPointerException if the specified element is null and this |
314 |
* deque does not permit null elements |
315 |
*/ |
316 |
boolean removeLastOccurrence(Object o); |
317 |
|
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 |
* the specified element at the end of this deque. When using a |
325 |
* 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 |
* @throws NullPointerException if the specified element is null and this |
335 |
* 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 |
* inserts the specified element as the last element of this deque. |
343 |
* |
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 |
* @throws NullPointerException if the specified element is null and this |
351 |
* 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 |
* This method differs from the <tt>poll</tt> method only in that it |
371 |
* 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 |
* <p>This method is equivalent to {@link #peekFirst()}. |
385 |
* |
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 |
* this deque. This method differs from the <tt>peek</tt> method only in |
394 |
* that it throws an exception if this deque is empty. |
395 |
* |
396 |
* <p>This method is equivalent to {@link #getFirst()}. |
397 |
* |
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 |
* words, inserts the element at the front of this deque unless it would |
409 |
* violate capacity restrictions. |
410 |
* |
411 |
* <p>This method is equivalent to {@link #addFirst}. |
412 |
* |
413 |
* @param e the element to push |
414 |
* @throws IllegalStateException if it was not possible to insert |
415 |
* the element due to capacity restrictions |
416 |
* @throws NullPointerException if the specified element is null and this |
417 |
* 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 |
* words, removes and returns the first element of this deque. |
424 |
* |
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 |
* |
440 |
* @return an <tt>Iterator</tt> over the elements in this deque |
441 |
*/ |
442 |
Iterator<E> iterator(); |
443 |
} |