Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7859 views
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/publicdomain/zero/1.0/
5
*/
6
7
package com.artifex.mupdfdemo;
8
9
import java.util.Collection;
10
import java.util.Iterator;
11
import java.util.List;
12
import java.util.NoSuchElementException;
13
import java.util.Queue;
14
import java.util.Stack;
15
16
// BEGIN android-note
17
// removed link to collections framework docs
18
// END android-note
19
20
/**
21
* A linear collection that supports element insertion and removal at
22
* both ends. The name <i>deque</i> is short for "double ended queue"
23
* and is usually pronounced "deck". Most <tt>Deque</tt>
24
* implementations place no fixed limits on the number of elements
25
* they may contain, but this interface supports capacity-restricted
26
* deques as well as those with no fixed size limit.
27
*
28
* <p>This interface defines methods to access the elements at both
29
* ends of the deque. Methods are provided to insert, remove, and
30
* examine the element. Each of these methods exists in two forms:
31
* one throws an exception if the operation fails, the other returns a
32
* special value (either <tt>null</tt> or <tt>false</tt>, depending on
33
* the operation). The latter form of the insert operation is
34
* designed specifically for use with capacity-restricted
35
* <tt>Deque</tt> implementations; in most implementations, insert
36
* operations cannot fail.
37
*
38
* <p>The twelve methods described above are summarized in the
39
* following table:
40
*
41
* <p>
42
* <table BORDER CELLPADDING=3 CELLSPACING=1>
43
* <tr>
44
* <td></td>
45
* <td ALIGN=CENTER COLSPAN = 2> <b>First Element (Head)</b></td>
46
* <td ALIGN=CENTER COLSPAN = 2> <b>Last Element (Tail)</b></td>
47
* </tr>
48
* <tr>
49
* <td></td>
50
* <td ALIGN=CENTER><em>Throws exception</em></td>
51
* <td ALIGN=CENTER><em>Special value</em></td>
52
* <td ALIGN=CENTER><em>Throws exception</em></td>
53
* <td ALIGN=CENTER><em>Special value</em></td>
54
* </tr>
55
* <tr>
56
* <td><b>Insert</b></td>
57
* <td>{@link #addFirst addFirst(e)}</td>
58
* <td>{@link #offerFirst offerFirst(e)}</td>
59
* <td>{@link #addLast addLast(e)}</td>
60
* <td>{@link #offerLast offerLast(e)}</td>
61
* </tr>
62
* <tr>
63
* <td><b>Remove</b></td>
64
* <td>{@link #removeFirst removeFirst()}</td>
65
* <td>{@link #pollFirst pollFirst()}</td>
66
* <td>{@link #removeLast removeLast()}</td>
67
* <td>{@link #pollLast pollLast()}</td>
68
* </tr>
69
* <tr>
70
* <td><b>Examine</b></td>
71
* <td>{@link #getFirst getFirst()}</td>
72
* <td>{@link #peekFirst peekFirst()}</td>
73
* <td>{@link #getLast getLast()}</td>
74
* <td>{@link #peekLast peekLast()}</td>
75
* </tr>
76
* </table>
77
*
78
* <p>This interface extends the {@link Queue} interface. When a deque is
79
* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
80
* added at the end of the deque and removed from the beginning. The methods
81
* inherited from the <tt>Queue</tt> interface are precisely equivalent to
82
* <tt>Deque</tt> methods as indicated in the following table:
83
*
84
* <p>
85
* <table BORDER CELLPADDING=3 CELLSPACING=1>
86
* <tr>
87
* <td ALIGN=CENTER> <b><tt>Queue</tt> Method</b></td>
88
* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
89
* </tr>
90
* <tr>
91
* <td>{@link java.util.Queue#add add(e)}</td>
92
* <td>{@link #addLast addLast(e)}</td>
93
* </tr>
94
* <tr>
95
* <td>{@link java.util.Queue#offer offer(e)}</td>
96
* <td>{@link #offerLast offerLast(e)}</td>
97
* </tr>
98
* <tr>
99
* <td>{@link java.util.Queue#remove remove()}</td>
100
* <td>{@link #removeFirst removeFirst()}</td>
101
* </tr>
102
* <tr>
103
* <td>{@link java.util.Queue#poll poll()}</td>
104
* <td>{@link #pollFirst pollFirst()}</td>
105
* </tr>
106
* <tr>
107
* <td>{@link java.util.Queue#element element()}</td>
108
* <td>{@link #getFirst getFirst()}</td>
109
* </tr>
110
* <tr>
111
* <td>{@link java.util.Queue#peek peek()}</td>
112
* <td>{@link #peek peekFirst()}</td>
113
* </tr>
114
* </table>
115
*
116
* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This
117
* interface should be used in preference to the legacy {@link Stack} class.
118
* When a deque is used as a stack, elements are pushed and popped from the
119
* beginning of the deque. Stack methods are precisely equivalent to
120
* <tt>Deque</tt> methods as indicated in the table below:
121
*
122
* <p>
123
* <table BORDER CELLPADDING=3 CELLSPACING=1>
124
* <tr>
125
* <td ALIGN=CENTER> <b>Stack Method</b></td>
126
* <td ALIGN=CENTER> <b>Equivalent <tt>Deque</tt> Method</b></td>
127
* </tr>
128
* <tr>
129
* <td>{@link #push push(e)}</td>
130
* <td>{@link #addFirst addFirst(e)}</td>
131
* </tr>
132
* <tr>
133
* <td>{@link #pop pop()}</td>
134
* <td>{@link #removeFirst removeFirst()}</td>
135
* </tr>
136
* <tr>
137
* <td>{@link #peek peek()}</td>
138
* <td>{@link #peekFirst peekFirst()}</td>
139
* </tr>
140
* </table>
141
*
142
* <p>Note that the {@link #peek peek} method works equally well when
143
* a deque is used as a queue or a stack; in either case, elements are
144
* drawn from the beginning of the deque.
145
*
146
* <p>This interface provides two methods to remove interior
147
* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
148
* {@link #removeLastOccurrence removeLastOccurrence}.
149
*
150
* <p>Unlike the {@link List} interface, this interface does not
151
* provide support for indexed access to elements.
152
*
153
* <p>While <tt>Deque</tt> implementations are not strictly required
154
* to prohibit the insertion of null elements, they are strongly
155
* encouraged to do so. Users of any <tt>Deque</tt> implementations
156
* that do allow null elements are strongly encouraged <i>not</i> to
157
* take advantage of the ability to insert nulls. This is so because
158
* <tt>null</tt> is used as a special return value by various methods
159
* to indicated that the deque is empty.
160
*
161
* <p><tt>Deque</tt> implementations generally do not define
162
* element-based versions of the <tt>equals</tt> and <tt>hashCode</tt>
163
* methods, but instead inherit the identity-based versions from class
164
* <tt>Object</tt>.
165
*
166
* @author Doug Lea
167
* @author Josh Bloch
168
* @since 1.6
169
* @param <E> the type of elements held in this collection
170
*/
171
172
public interface Deque<E> extends Queue<E> {
173
/**
174
* Inserts the specified element at the front of this deque if it is
175
* possible to do so immediately without violating capacity restrictions.
176
* When using a capacity-restricted deque, it is generally preferable to
177
* use method {@link #offerFirst}.
178
*
179
* @param e the element to add
180
* @throws IllegalStateException if the element cannot be added at this
181
* time due to capacity restrictions
182
* @throws ClassCastException if the class of the specified element
183
* prevents it from being added to this deque
184
* @throws NullPointerException if the specified element is null and this
185
* deque does not permit null elements
186
* @throws IllegalArgumentException if some property of the specified
187
* element prevents it from being added to this deque
188
*/
189
void addFirst(E e);
190
191
/**
192
* Inserts the specified element at the end of this deque if it is
193
* possible to do so immediately without violating capacity restrictions.
194
* When using a capacity-restricted deque, it is generally preferable to
195
* use method {@link #offerLast}.
196
*
197
* <p>This method is equivalent to {@link #add}.
198
*
199
* @param e the element to add
200
* @throws IllegalStateException if the element cannot be added at this
201
* time due to capacity restrictions
202
* @throws ClassCastException if the class of the specified element
203
* prevents it from being added to this deque
204
* @throws NullPointerException if the specified element is null and this
205
* deque does not permit null elements
206
* @throws IllegalArgumentException if some property of the specified
207
* element prevents it from being added to this deque
208
*/
209
void addLast(E e);
210
211
/**
212
* Inserts the specified element at the front of this deque unless it would
213
* violate capacity restrictions. When using a capacity-restricted deque,
214
* this method is generally preferable to the {@link #addFirst} method,
215
* which can fail to insert an element only by throwing an exception.
216
*
217
* @param e the element to add
218
* @return <tt>true</tt> if the element was added to this deque, else
219
* <tt>false</tt>
220
* @throws ClassCastException if the class of the specified element
221
* prevents it from being added to this deque
222
* @throws NullPointerException if the specified element is null and this
223
* deque does not permit null elements
224
* @throws IllegalArgumentException if some property of the specified
225
* element prevents it from being added to this deque
226
*/
227
boolean offerFirst(E e);
228
229
/**
230
* Inserts the specified element at the end of this deque unless it would
231
* violate capacity restrictions. When using a capacity-restricted deque,
232
* this method is generally preferable to the {@link #addLast} method,
233
* which can fail to insert an element only by throwing an exception.
234
*
235
* @param e the element to add
236
* @return <tt>true</tt> if the element was added to this deque, else
237
* <tt>false</tt>
238
* @throws ClassCastException if the class of the specified element
239
* prevents it from being added to this deque
240
* @throws NullPointerException if the specified element is null and this
241
* deque does not permit null elements
242
* @throws IllegalArgumentException if some property of the specified
243
* element prevents it from being added to this deque
244
*/
245
boolean offerLast(E e);
246
247
/**
248
* Retrieves and removes the first element of this deque. This method
249
* differs from {@link #pollFirst pollFirst} only in that it throws an
250
* exception if this deque is empty.
251
*
252
* @return the head of this deque
253
* @throws NoSuchElementException if this deque is empty
254
*/
255
E removeFirst();
256
257
/**
258
* Retrieves and removes the last element of this deque. This method
259
* differs from {@link #pollLast pollLast} only in that it throws an
260
* exception if this deque is empty.
261
*
262
* @return the tail of this deque
263
* @throws NoSuchElementException if this deque is empty
264
*/
265
E removeLast();
266
267
/**
268
* Retrieves and removes the first element of this deque,
269
* or returns <tt>null</tt> if this deque is empty.
270
*
271
* @return the head of this deque, or <tt>null</tt> if this deque is empty
272
*/
273
E pollFirst();
274
275
/**
276
* Retrieves and removes the last element of this deque,
277
* or returns <tt>null</tt> if this deque is empty.
278
*
279
* @return the tail of this deque, or <tt>null</tt> if this deque is empty
280
*/
281
E pollLast();
282
283
/**
284
* Retrieves, but does not remove, the first element of this deque.
285
*
286
* This method differs from {@link #peekFirst peekFirst} only in that it
287
* throws an exception if this deque is empty.
288
*
289
* @return the head of this deque
290
* @throws NoSuchElementException if this deque is empty
291
*/
292
E getFirst();
293
294
/**
295
* Retrieves, but does not remove, the last element of this deque.
296
* This method differs from {@link #peekLast peekLast} only in that it
297
* throws an exception if this deque is empty.
298
*
299
* @return the tail of this deque
300
* @throws NoSuchElementException if this deque is empty
301
*/
302
E getLast();
303
304
/**
305
* Retrieves, but does not remove, the first element of this deque,
306
* or returns <tt>null</tt> if this deque is empty.
307
*
308
* @return the head of this deque, or <tt>null</tt> if this deque is empty
309
*/
310
E peekFirst();
311
312
/**
313
* Retrieves, but does not remove, the last element of this deque,
314
* or returns <tt>null</tt> if this deque is empty.
315
*
316
* @return the tail of this deque, or <tt>null</tt> if this deque is empty
317
*/
318
E peekLast();
319
320
/**
321
* Removes the first occurrence of the specified element from this deque.
322
* If the deque does not contain the element, it is unchanged.
323
* More formally, removes the first element <tt>e</tt> such that
324
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
325
* (if such an element exists).
326
* Returns <tt>true</tt> if this deque contained the specified element
327
* (or equivalently, if this deque changed as a result of the call).
328
*
329
* @param o element to be removed from this deque, if present
330
* @return <tt>true</tt> if an element was removed as a result of this call
331
* @throws ClassCastException if the class of the specified element
332
* is incompatible with this deque (optional)
333
* @throws NullPointerException if the specified element is null and this
334
* deque does not permit null elements (optional)
335
*/
336
boolean removeFirstOccurrence(Object o);
337
338
/**
339
* Removes the last occurrence of the specified element from this deque.
340
* If the deque does not contain the element, it is unchanged.
341
* More formally, removes the last element <tt>e</tt> such that
342
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
343
* (if such an element exists).
344
* Returns <tt>true</tt> if this deque contained the specified element
345
* (or equivalently, if this deque changed as a result of the call).
346
*
347
* @param o element to be removed from this deque, if present
348
* @return <tt>true</tt> if an element was removed as a result of this call
349
* @throws ClassCastException if the class of the specified element
350
* is incompatible with this deque (optional)
351
* @throws NullPointerException if the specified element is null and this
352
* deque does not permit null elements (optional)
353
*/
354
boolean removeLastOccurrence(Object o);
355
356
// *** Queue methods ***
357
358
/**
359
* Inserts the specified element into the queue represented by this deque
360
* (in other words, at the tail of this deque) if it is possible to do so
361
* immediately without violating capacity restrictions, returning
362
* <tt>true</tt> upon success and throwing an
363
* <tt>IllegalStateException</tt> if no space is currently available.
364
* When using a capacity-restricted deque, it is generally preferable to
365
* use {@link #offer(Object) offer}.
366
*
367
* <p>This method is equivalent to {@link #addLast}.
368
*
369
* @param e the element to add
370
* @return <tt>true</tt> (as specified by {@link Collection#add})
371
* @throws IllegalStateException if the element cannot be added at this
372
* time due to capacity restrictions
373
* @throws ClassCastException if the class of the specified element
374
* prevents it from being added to this deque
375
* @throws NullPointerException if the specified element is null and this
376
* deque does not permit null elements
377
* @throws IllegalArgumentException if some property of the specified
378
* element prevents it from being added to this deque
379
*/
380
boolean add(E e);
381
382
/**
383
* Inserts the specified element into the queue represented by this deque
384
* (in other words, at the tail of this deque) if it is possible to do so
385
* immediately without violating capacity restrictions, returning
386
* <tt>true</tt> upon success and <tt>false</tt> if no space is currently
387
* available. When using a capacity-restricted deque, this method is
388
* generally preferable to the {@link #add} method, which can fail to
389
* insert an element only by throwing an exception.
390
*
391
* <p>This method is equivalent to {@link #offerLast}.
392
*
393
* @param e the element to add
394
* @return <tt>true</tt> if the element was added to this deque, else
395
* <tt>false</tt>
396
* @throws ClassCastException if the class of the specified element
397
* prevents it from being added to this deque
398
* @throws NullPointerException if the specified element is null and this
399
* deque does not permit null elements
400
* @throws IllegalArgumentException if some property of the specified
401
* element prevents it from being added to this deque
402
*/
403
boolean offer(E e);
404
405
/**
406
* Retrieves and removes the head of the queue represented by this deque
407
* (in other words, the first element of this deque).
408
* This method differs from {@link #poll poll} only in that it throws an
409
* exception if this deque is empty.
410
*
411
* <p>This method is equivalent to {@link #removeFirst()}.
412
*
413
* @return the head of the queue represented by this deque
414
* @throws NoSuchElementException if this deque is empty
415
*/
416
E remove();
417
418
/**
419
* Retrieves and removes the head of the queue represented by this deque
420
* (in other words, the first element of this deque), or returns
421
* <tt>null</tt> if this deque is empty.
422
*
423
* <p>This method is equivalent to {@link #pollFirst()}.
424
*
425
* @return the first element of this deque, or <tt>null</tt> if
426
* this deque is empty
427
*/
428
E poll();
429
430
/**
431
* Retrieves, but does not remove, the head of the queue represented by
432
* this deque (in other words, the first element of this deque).
433
* This method differs from {@link #peek peek} only in that it throws an
434
* exception if this deque is empty.
435
*
436
* <p>This method is equivalent to {@link #getFirst()}.
437
*
438
* @return the head of the queue represented by this deque
439
* @throws NoSuchElementException if this deque is empty
440
*/
441
E element();
442
443
/**
444
* Retrieves, but does not remove, the head of the queue represented by
445
* this deque (in other words, the first element of this deque), or
446
* returns <tt>null</tt> if this deque is empty.
447
*
448
* <p>This method is equivalent to {@link #peekFirst()}.
449
*
450
* @return the head of the queue represented by this deque, or
451
* <tt>null</tt> if this deque is empty
452
*/
453
E peek();
454
455
456
// *** Stack methods ***
457
458
/**
459
* Pushes an element onto the stack represented by this deque (in other
460
* words, at the head of this deque) if it is possible to do so
461
* immediately without violating capacity restrictions, returning
462
* <tt>true</tt> upon success and throwing an
463
* <tt>IllegalStateException</tt> if no space is currently available.
464
*
465
* <p>This method is equivalent to {@link #addFirst}.
466
*
467
* @param e the element to push
468
* @throws IllegalStateException if the element cannot be added at this
469
* time due to capacity restrictions
470
* @throws ClassCastException if the class of the specified element
471
* prevents it from being added to this deque
472
* @throws NullPointerException if the specified element is null and this
473
* deque does not permit null elements
474
* @throws IllegalArgumentException if some property of the specified
475
* element prevents it from being added to this deque
476
*/
477
void push(E e);
478
479
/**
480
* Pops an element from the stack represented by this deque. In other
481
* words, removes and returns the first element of this deque.
482
*
483
* <p>This method is equivalent to {@link #removeFirst()}.
484
*
485
* @return the element at the front of this deque (which is the top
486
* of the stack represented by this deque)
487
* @throws NoSuchElementException if this deque is empty
488
*/
489
E pop();
490
491
492
// *** Collection methods ***
493
494
/**
495
* Removes the first occurrence of the specified element from this deque.
496
* If the deque does not contain the element, it is unchanged.
497
* More formally, removes the first element <tt>e</tt> such that
498
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>
499
* (if such an element exists).
500
* Returns <tt>true</tt> if this deque contained the specified element
501
* (or equivalently, if this deque changed as a result of the call).
502
*
503
* <p>This method is equivalent to {@link #removeFirstOccurrence}.
504
*
505
* @param o element to be removed from this deque, if present
506
* @return <tt>true</tt> if an element was removed as a result of this call
507
* @throws ClassCastException if the class of the specified element
508
* is incompatible with this deque (optional)
509
* @throws NullPointerException if the specified element is null and this
510
* deque does not permit null elements (optional)
511
*/
512
boolean remove(Object o);
513
514
/**
515
* Returns <tt>true</tt> if this deque contains the specified element.
516
* More formally, returns <tt>true</tt> if and only if this deque contains
517
* at least one element <tt>e</tt> such that
518
* <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
519
*
520
* @param o element whose presence in this deque is to be tested
521
* @return <tt>true</tt> if this deque contains the specified element
522
* @throws ClassCastException if the type of the specified element
523
* is incompatible with this deque (optional)
524
* @throws NullPointerException if the specified element is null and this
525
* deque does not permit null elements (optional)
526
*/
527
boolean contains(Object o);
528
529
/**
530
* Returns the number of elements in this deque.
531
*
532
* @return the number of elements in this deque
533
*/
534
public int size();
535
536
/**
537
* Returns an iterator over the elements in this deque in proper sequence.
538
* The elements will be returned in order from first (head) to last (tail).
539
*
540
* @return an iterator over the elements in this deque in proper sequence
541
*/
542
Iterator<E> iterator();
543
544
/**
545
* Returns an iterator over the elements in this deque in reverse
546
* sequential order. The elements will be returned in order from
547
* last (tail) to first (head).
548
*
549
* @return an iterator over the elements in this deque in reverse
550
* sequence
551
*/
552
Iterator<E> descendingIterator();
553
554
}
555
556