Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/util/Deque.java
41152 views
1
/*
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3
*
4
* This code is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 2 only, as
6
* published by the Free Software Foundation. Oracle designates this
7
* particular file as subject to the "Classpath" exception as provided
8
* by Oracle in the LICENSE file that accompanied this code.
9
*
10
* This code is distributed in the hope that it will be useful, but WITHOUT
11
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13
* version 2 for more details (a copy is included in the LICENSE file that
14
* accompanied this code).
15
*
16
* You should have received a copy of the GNU General Public License version
17
* 2 along with this work; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
*
20
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21
* or visit www.oracle.com if you need additional information or have any
22
* questions.
23
*/
24
25
/*
26
* This file is available under and governed by the GNU General Public
27
* License version 2 only, as published by the Free Software Foundation.
28
* However, the following notice accompanied the original version of this
29
* file:
30
*
31
* Written by Doug Lea and Josh Bloch with assistance from members of
32
* JCP JSR-166 Expert Group and released to the public domain, as explained
33
* at http://creativecommons.org/publicdomain/zero/1.0/
34
*/
35
36
package java.util;
37
38
/**
39
* A linear collection that supports element insertion and removal at
40
* both ends. The name <i>deque</i> is short for "double ended queue"
41
* and is usually pronounced "deck". Most {@code Deque}
42
* implementations place no fixed limits on the number of elements
43
* they may contain, but this interface supports capacity-restricted
44
* deques as well as those with no fixed size limit.
45
*
46
* <p>This interface defines methods to access the elements at both
47
* ends of the deque. Methods are provided to insert, remove, and
48
* examine the element. Each of these methods exists in two forms:
49
* one throws an exception if the operation fails, the other returns a
50
* special value (either {@code null} or {@code false}, depending on
51
* the operation). The latter form of the insert operation is
52
* designed specifically for use with capacity-restricted
53
* {@code Deque} implementations; in most implementations, insert
54
* operations cannot fail.
55
*
56
* <p>The twelve methods described above are summarized in the
57
* following table:
58
*
59
* <table class="striped">
60
* <caption>Summary of Deque methods</caption>
61
* <thead>
62
* <tr>
63
* <td rowspan="2"></td>
64
* <th scope="col" colspan="2"> First Element (Head)</th>
65
* <th scope="col" colspan="2"> Last Element (Tail)</th>
66
* </tr>
67
* <tr>
68
* <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th>
69
* <th scope="col" style="font-weight:normal; font-style:italic">Special value</th>
70
* <th scope="col" style="font-weight:normal; font-style:italic">Throws exception</th>
71
* <th scope="col" style="font-weight:normal; font-style:italic">Special value</th>
72
* </tr>
73
* </thead>
74
* <tbody>
75
* <tr>
76
* <th scope="row">Insert</th>
77
* <td>{@link #addFirst(Object) addFirst(e)}</td>
78
* <td>{@link #offerFirst(Object) offerFirst(e)}</td>
79
* <td>{@link #addLast(Object) addLast(e)}</td>
80
* <td>{@link #offerLast(Object) offerLast(e)}</td>
81
* </tr>
82
* <tr>
83
* <th scope="row">Remove</th>
84
* <td>{@link #removeFirst() removeFirst()}</td>
85
* <td>{@link #pollFirst() pollFirst()}</td>
86
* <td>{@link #removeLast() removeLast()}</td>
87
* <td>{@link #pollLast() pollLast()}</td>
88
* </tr>
89
* <tr>
90
* <th scope="row">Examine</th>
91
* <td>{@link #getFirst() getFirst()}</td>
92
* <td>{@link #peekFirst() peekFirst()}</td>
93
* <td>{@link #getLast() getLast()}</td>
94
* <td>{@link #peekLast() peekLast()}</td>
95
* </tr>
96
* </tbody>
97
* </table>
98
*
99
* <p>This interface extends the {@link Queue} interface. When a deque is
100
* used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
101
* added at the end of the deque and removed from the beginning. The methods
102
* inherited from the {@code Queue} interface are precisely equivalent to
103
* {@code Deque} methods as indicated in the following table:
104
*
105
* <table class="striped">
106
* <caption>Comparison of Queue and Deque methods</caption>
107
* <thead>
108
* <tr>
109
* <th scope="col"> {@code Queue} Method</th>
110
* <th scope="col"> Equivalent {@code Deque} Method</th>
111
* </tr>
112
* </thead>
113
* <tbody>
114
* <tr>
115
* <th scope="row">{@link #add(Object) add(e)}</th>
116
* <td>{@link #addLast(Object) addLast(e)}</td>
117
* </tr>
118
* <tr>
119
* <th scope="row">{@link #offer(Object) offer(e)}</th>
120
* <td>{@link #offerLast(Object) offerLast(e)}</td>
121
* </tr>
122
* <tr>
123
* <th scope="row">{@link #remove() remove()}</th>
124
* <td>{@link #removeFirst() removeFirst()}</td>
125
* </tr>
126
* <tr>
127
* <th scope="row">{@link #poll() poll()}</th>
128
* <td>{@link #pollFirst() pollFirst()}</td>
129
* </tr>
130
* <tr>
131
* <th scope="row">{@link #element() element()}</th>
132
* <td>{@link #getFirst() getFirst()}</td>
133
* </tr>
134
* <tr>
135
* <th scope="row">{@link #peek() peek()}</th>
136
* <td>{@link #peekFirst() peekFirst()}</td>
137
* </tr>
138
* </tbody>
139
* </table>
140
*
141
* <p>Deques can also be used as LIFO (Last-In-First-Out) stacks. This
142
* interface should be used in preference to the legacy {@link Stack} class.
143
* When a deque is used as a stack, elements are pushed and popped from the
144
* beginning of the deque. Stack methods are equivalent to {@code Deque}
145
* methods as indicated in the table below:
146
*
147
* <table class="striped">
148
* <caption>Comparison of Stack and Deque methods</caption>
149
* <thead>
150
* <tr>
151
* <th scope="col"> Stack Method</th>
152
* <th scope="col"> Equivalent {@code Deque} Method</th>
153
* </tr>
154
* </thead>
155
* <tbody>
156
* <tr>
157
* <th scope="row">{@link #push(Object) push(e)}</th>
158
* <td>{@link #addFirst(Object) addFirst(e)}</td>
159
* </tr>
160
* <tr>
161
* <th scope="row">{@link #pop() pop()}</th>
162
* <td>{@link #removeFirst() removeFirst()}</td>
163
* </tr>
164
* <tr>
165
* <th scope="row">{@link #peek() peek()}</th>
166
* <td>{@link #getFirst() getFirst()}</td>
167
* </tr>
168
* </tbody>
169
* </table>
170
*
171
* <p>Note that the {@link #peek peek} method works equally well when
172
* a deque is used as a queue or a stack; in either case, elements are
173
* drawn from the beginning of the deque.
174
*
175
* <p>This interface provides two methods to remove interior
176
* elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
177
* {@link #removeLastOccurrence removeLastOccurrence}.
178
*
179
* <p>Unlike the {@link List} interface, this interface does not
180
* provide support for indexed access to elements.
181
*
182
* <p>While {@code Deque} implementations are not strictly required
183
* to prohibit the insertion of null elements, they are strongly
184
* encouraged to do so. Users of any {@code Deque} implementations
185
* that do allow null elements are strongly encouraged <i>not</i> to
186
* take advantage of the ability to insert nulls. This is so because
187
* {@code null} is used as a special return value by various methods
188
* to indicate that the deque is empty.
189
*
190
* <p>{@code Deque} implementations generally do not define
191
* element-based versions of the {@code equals} and {@code hashCode}
192
* methods, but instead inherit the identity-based versions from class
193
* {@code Object}.
194
*
195
* <p>This interface is a member of the
196
* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
197
* Java Collections Framework</a>.
198
*
199
* @author Doug Lea
200
* @author Josh Bloch
201
* @since 1.6
202
* @param <E> the type of elements held in this deque
203
*/
204
public interface Deque<E> extends Queue<E> {
205
/**
206
* Inserts the specified element at the front of this deque if it is
207
* possible to do so immediately without violating capacity restrictions,
208
* throwing an {@code IllegalStateException} if no space is currently
209
* available. When using a capacity-restricted deque, it is generally
210
* preferable to use method {@link #offerFirst}.
211
*
212
* @param e the element to add
213
* @throws IllegalStateException if the element cannot be added at this
214
* time due to capacity restrictions
215
* @throws ClassCastException if the class of the specified element
216
* prevents it from being added to this deque
217
* @throws NullPointerException if the specified element is null and this
218
* deque does not permit null elements
219
* @throws IllegalArgumentException if some property of the specified
220
* element prevents it from being added to this deque
221
*/
222
void addFirst(E e);
223
224
/**
225
* Inserts the specified element at the end of this deque if it is
226
* possible to do so immediately without violating capacity restrictions,
227
* throwing an {@code IllegalStateException} if no space is currently
228
* available. When using a capacity-restricted deque, it is generally
229
* preferable to use method {@link #offerLast}.
230
*
231
* <p>This method is equivalent to {@link #add}.
232
*
233
* @param e the element to add
234
* @throws IllegalStateException if the element cannot be added at this
235
* time due to capacity restrictions
236
* @throws ClassCastException if the class of the specified element
237
* prevents it from being added to this deque
238
* @throws NullPointerException if the specified element is null and this
239
* deque does not permit null elements
240
* @throws IllegalArgumentException if some property of the specified
241
* element prevents it from being added to this deque
242
*/
243
void addLast(E e);
244
245
/**
246
* Inserts the specified element at the front of this deque unless it would
247
* violate capacity restrictions. When using a capacity-restricted deque,
248
* this method is generally preferable to the {@link #addFirst} method,
249
* which can fail to insert an element only by throwing an exception.
250
*
251
* @param e the element to add
252
* @return {@code true} if the element was added to this deque, else
253
* {@code false}
254
* @throws ClassCastException if the class of the specified element
255
* prevents it from being added to this deque
256
* @throws NullPointerException if the specified element is null and this
257
* deque does not permit null elements
258
* @throws IllegalArgumentException if some property of the specified
259
* element prevents it from being added to this deque
260
*/
261
boolean offerFirst(E e);
262
263
/**
264
* Inserts the specified element at the end of this deque unless it would
265
* violate capacity restrictions. When using a capacity-restricted deque,
266
* this method is generally preferable to the {@link #addLast} method,
267
* which can fail to insert an element only by throwing an exception.
268
*
269
* @param e the element to add
270
* @return {@code true} if the element was added to this deque, else
271
* {@code false}
272
* @throws ClassCastException if the class of the specified element
273
* prevents it from being added to this deque
274
* @throws NullPointerException if the specified element is null and this
275
* deque does not permit null elements
276
* @throws IllegalArgumentException if some property of the specified
277
* element prevents it from being added to this deque
278
*/
279
boolean offerLast(E e);
280
281
/**
282
* Retrieves and removes the first element of this deque. This method
283
* differs from {@link #pollFirst pollFirst} only in that it throws an
284
* exception if this deque is empty.
285
*
286
* @return the head of this deque
287
* @throws NoSuchElementException if this deque is empty
288
*/
289
E removeFirst();
290
291
/**
292
* Retrieves and removes the last element of this deque. This method
293
* differs from {@link #pollLast pollLast} only in that it throws an
294
* exception if this deque is empty.
295
*
296
* @return the tail of this deque
297
* @throws NoSuchElementException if this deque is empty
298
*/
299
E removeLast();
300
301
/**
302
* Retrieves and removes the first element of this deque,
303
* or returns {@code null} if this deque is empty.
304
*
305
* @return the head of this deque, or {@code null} if this deque is empty
306
*/
307
E pollFirst();
308
309
/**
310
* Retrieves and removes the last element of this deque,
311
* or returns {@code null} if this deque is empty.
312
*
313
* @return the tail of this deque, or {@code null} if this deque is empty
314
*/
315
E pollLast();
316
317
/**
318
* Retrieves, but does not remove, the first element of this deque.
319
*
320
* This method differs from {@link #peekFirst peekFirst} only in that it
321
* throws an exception if this deque is empty.
322
*
323
* @return the head of this deque
324
* @throws NoSuchElementException if this deque is empty
325
*/
326
E getFirst();
327
328
/**
329
* Retrieves, but does not remove, the last element of this deque.
330
* This method differs from {@link #peekLast peekLast} only in that it
331
* throws an exception if this deque is empty.
332
*
333
* @return the tail of this deque
334
* @throws NoSuchElementException if this deque is empty
335
*/
336
E getLast();
337
338
/**
339
* Retrieves, but does not remove, the first element of this deque,
340
* or returns {@code null} if this deque is empty.
341
*
342
* @return the head of this deque, or {@code null} if this deque is empty
343
*/
344
E peekFirst();
345
346
/**
347
* Retrieves, but does not remove, the last element of this deque,
348
* or returns {@code null} if this deque is empty.
349
*
350
* @return the tail of this deque, or {@code null} if this deque is empty
351
*/
352
E peekLast();
353
354
/**
355
* Removes the first occurrence of the specified element from this deque.
356
* If the deque does not contain the element, it is unchanged.
357
* More formally, removes the first element {@code e} such that
358
* {@code Objects.equals(o, e)} (if such an element exists).
359
* Returns {@code true} if this deque contained the specified element
360
* (or equivalently, if this deque changed as a result of the call).
361
*
362
* @param o element to be removed from this deque, if present
363
* @return {@code true} if an element was removed as a result of this call
364
* @throws ClassCastException if the class of the specified element
365
* is incompatible with this deque
366
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
367
* @throws NullPointerException if the specified element is null and this
368
* deque does not permit null elements
369
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
370
*/
371
boolean removeFirstOccurrence(Object o);
372
373
/**
374
* Removes the last occurrence of the specified element from this deque.
375
* If the deque does not contain the element, it is unchanged.
376
* More formally, removes the last element {@code e} such that
377
* {@code Objects.equals(o, e)} (if such an element exists).
378
* Returns {@code true} if this deque contained the specified element
379
* (or equivalently, if this deque changed as a result of the call).
380
*
381
* @param o element to be removed from this deque, if present
382
* @return {@code true} if an element was removed as a result of this call
383
* @throws ClassCastException if the class of the specified element
384
* is incompatible with this deque
385
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
386
* @throws NullPointerException if the specified element is null and this
387
* deque does not permit null elements
388
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
389
*/
390
boolean removeLastOccurrence(Object o);
391
392
// *** Queue methods ***
393
394
/**
395
* Inserts the specified element into the queue represented by this deque
396
* (in other words, at the tail of this deque) if it is possible to do so
397
* immediately without violating capacity restrictions, returning
398
* {@code true} upon success and throwing an
399
* {@code IllegalStateException} if no space is currently available.
400
* When using a capacity-restricted deque, it is generally preferable to
401
* use {@link #offer(Object) offer}.
402
*
403
* <p>This method is equivalent to {@link #addLast}.
404
*
405
* @param e the element to add
406
* @return {@code true} (as specified by {@link Collection#add})
407
* @throws IllegalStateException if the element cannot be added at this
408
* time due to capacity restrictions
409
* @throws ClassCastException if the class of the specified element
410
* prevents it from being added to this deque
411
* @throws NullPointerException if the specified element is null and this
412
* deque does not permit null elements
413
* @throws IllegalArgumentException if some property of the specified
414
* element prevents it from being added to this deque
415
*/
416
boolean add(E e);
417
418
/**
419
* Inserts the specified element into the queue represented by this deque
420
* (in other words, at the tail of this deque) if it is possible to do so
421
* immediately without violating capacity restrictions, returning
422
* {@code true} upon success and {@code false} if no space is currently
423
* available. When using a capacity-restricted deque, this method is
424
* generally preferable to the {@link #add} method, which can fail to
425
* insert an element only by throwing an exception.
426
*
427
* <p>This method is equivalent to {@link #offerLast}.
428
*
429
* @param e the element to add
430
* @return {@code true} if the element was added to this deque, else
431
* {@code false}
432
* @throws ClassCastException if the class of the specified element
433
* prevents it from being added to this deque
434
* @throws NullPointerException if the specified element is null and this
435
* deque does not permit null elements
436
* @throws IllegalArgumentException if some property of the specified
437
* element prevents it from being added to this deque
438
*/
439
boolean offer(E e);
440
441
/**
442
* Retrieves and removes the head of the queue represented by this deque
443
* (in other words, the first element of this deque).
444
* This method differs from {@link #poll() poll()} only in that it
445
* throws an exception if this deque is empty.
446
*
447
* <p>This method is equivalent to {@link #removeFirst()}.
448
*
449
* @return the head of the queue represented by this deque
450
* @throws NoSuchElementException if this deque is empty
451
*/
452
E remove();
453
454
/**
455
* Retrieves and removes the head of the queue represented by this deque
456
* (in other words, the first element of this deque), or returns
457
* {@code null} if this deque is empty.
458
*
459
* <p>This method is equivalent to {@link #pollFirst()}.
460
*
461
* @return the first element of this deque, or {@code null} if
462
* this deque is empty
463
*/
464
E poll();
465
466
/**
467
* Retrieves, but does not remove, the head of the queue represented by
468
* this deque (in other words, the first element of this deque).
469
* This method differs from {@link #peek peek} only in that it throws an
470
* exception if this deque is empty.
471
*
472
* <p>This method is equivalent to {@link #getFirst()}.
473
*
474
* @return the head of the queue represented by this deque
475
* @throws NoSuchElementException if this deque is empty
476
*/
477
E element();
478
479
/**
480
* Retrieves, but does not remove, the head of the queue represented by
481
* this deque (in other words, the first element of this deque), or
482
* returns {@code null} if this deque is empty.
483
*
484
* <p>This method is equivalent to {@link #peekFirst()}.
485
*
486
* @return the head of the queue represented by this deque, or
487
* {@code null} if this deque is empty
488
*/
489
E peek();
490
491
/**
492
* Adds all of the elements in the specified collection at the end
493
* of this deque, as if by calling {@link #addLast} on each one,
494
* in the order that they are returned by the collection's iterator.
495
*
496
* <p>When using a capacity-restricted deque, it is generally preferable
497
* to call {@link #offer(Object) offer} separately on each element.
498
*
499
* <p>An exception encountered while trying to add an element may result
500
* in only some of the elements having been successfully added when
501
* the associated exception is thrown.
502
*
503
* @param c the elements to be inserted into this deque
504
* @return {@code true} if this deque changed as a result of the call
505
* @throws IllegalStateException if not all the elements can be added at
506
* this time due to insertion restrictions
507
* @throws ClassCastException if the class of an element of the specified
508
* collection prevents it from being added to this deque
509
* @throws NullPointerException if the specified collection contains a
510
* null element and this deque does not permit null elements,
511
* or if the specified collection is null
512
* @throws IllegalArgumentException if some property of an element of the
513
* specified collection prevents it from being added to this deque
514
*/
515
boolean addAll(Collection<? extends E> c);
516
517
// *** Stack methods ***
518
519
/**
520
* Pushes an element onto the stack represented by this deque (in other
521
* words, at the head of this deque) if it is possible to do so
522
* immediately without violating capacity restrictions, throwing an
523
* {@code IllegalStateException} if no space is currently available.
524
*
525
* <p>This method is equivalent to {@link #addFirst}.
526
*
527
* @param e the element to push
528
* @throws IllegalStateException if the element cannot be added at this
529
* time due to capacity restrictions
530
* @throws ClassCastException if the class of the specified element
531
* prevents it from being added to this deque
532
* @throws NullPointerException if the specified element is null and this
533
* deque does not permit null elements
534
* @throws IllegalArgumentException if some property of the specified
535
* element prevents it from being added to this deque
536
*/
537
void push(E e);
538
539
/**
540
* Pops an element from the stack represented by this deque. In other
541
* words, removes and returns the first element of this deque.
542
*
543
* <p>This method is equivalent to {@link #removeFirst()}.
544
*
545
* @return the element at the front of this deque (which is the top
546
* of the stack represented by this deque)
547
* @throws NoSuchElementException if this deque is empty
548
*/
549
E pop();
550
551
552
// *** Collection methods ***
553
554
/**
555
* Removes the first occurrence of the specified element from this deque.
556
* If the deque does not contain the element, it is unchanged.
557
* More formally, removes the first element {@code e} such that
558
* {@code Objects.equals(o, e)} (if such an element exists).
559
* Returns {@code true} if this deque contained the specified element
560
* (or equivalently, if this deque changed as a result of the call).
561
*
562
* <p>This method is equivalent to {@link #removeFirstOccurrence(Object)}.
563
*
564
* @param o element to be removed from this deque, if present
565
* @return {@code true} if an element was removed as a result of this call
566
* @throws ClassCastException if the class of the specified element
567
* is incompatible with this deque
568
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
569
* @throws NullPointerException if the specified element is null and this
570
* deque does not permit null elements
571
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
572
*/
573
boolean remove(Object o);
574
575
/**
576
* Returns {@code true} if this deque contains the specified element.
577
* More formally, returns {@code true} if and only if this deque contains
578
* at least one element {@code e} such that {@code Objects.equals(o, e)}.
579
*
580
* @param o element whose presence in this deque is to be tested
581
* @return {@code true} if this deque contains the specified element
582
* @throws ClassCastException if the class of the specified element
583
* is incompatible with this deque
584
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
585
* @throws NullPointerException if the specified element is null and this
586
* deque does not permit null elements
587
* (<a href="{@docRoot}/java.base/java/util/Collection.html#optional-restrictions">optional</a>)
588
*/
589
boolean contains(Object o);
590
591
/**
592
* Returns the number of elements in this deque.
593
*
594
* @return the number of elements in this deque
595
*/
596
int size();
597
598
/**
599
* Returns an iterator over the elements in this deque in proper sequence.
600
* The elements will be returned in order from first (head) to last (tail).
601
*
602
* @return an iterator over the elements in this deque in proper sequence
603
*/
604
Iterator<E> iterator();
605
606
/**
607
* Returns an iterator over the elements in this deque in reverse
608
* sequential order. The elements will be returned in order from
609
* last (tail) to first (head).
610
*
611
* @return an iterator over the elements in this deque in reverse
612
* sequence
613
*/
614
Iterator<E> descendingIterator();
615
616
}
617
618