7 |
|
import java.io.Serializable; |
8 |
|
import java.util.function.Consumer; |
9 |
|
import java.util.stream.Stream; |
10 |
– |
import java.util.stream.Streams; |
10 |
|
|
11 |
|
/** |
12 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
18 |
|
* when used as a queue. |
19 |
|
* |
20 |
|
* <p>Most {@code ArrayDeque} operations run in amortized constant time. |
21 |
< |
* Exceptions include {@link #remove(Object) remove}, {@link |
22 |
< |
* #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence |
23 |
< |
* removeLastOccurrence}, {@link #contains contains}, {@link #iterator |
24 |
< |
* iterator.remove()}, and the bulk operations, all of which run in linear |
25 |
< |
* time. |
21 |
> |
* Exceptions include |
22 |
> |
* {@link #remove(Object) remove}, |
23 |
> |
* {@link #removeFirstOccurrence removeFirstOccurrence}, |
24 |
> |
* {@link #removeLastOccurrence removeLastOccurrence}, |
25 |
> |
* {@link #contains contains}, |
26 |
> |
* {@link #iterator iterator.remove()}, |
27 |
> |
* and the bulk operations, all of which run in linear time. |
28 |
|
* |
29 |
< |
* <p>The iterators returned by this class's {@code iterator} method are |
30 |
< |
* <i>fail-fast</i>: If the deque is modified at any time after the iterator |
31 |
< |
* is created, in any way except through the iterator's own {@code remove} |
32 |
< |
* method, the iterator will generally throw a {@link |
29 |
> |
* <p>The iterators returned by this class's {@link #iterator() iterator} |
30 |
> |
* method are <em>fail-fast</em>: If the deque is modified at any time after |
31 |
> |
* the iterator is created, in any way except through the iterator's own |
32 |
> |
* {@code remove} method, the iterator will generally throw a {@link |
33 |
|
* ConcurrentModificationException}. Thus, in the face of concurrent |
34 |
|
* modification, the iterator fails quickly and cleanly, rather than risking |
35 |
|
* arbitrary, non-deterministic behavior at an undetermined time in the |
315 |
|
* @return {@code true} if the deque contained the specified element |
316 |
|
*/ |
317 |
|
public boolean removeFirstOccurrence(Object o) { |
318 |
< |
if (o == null) |
319 |
< |
return false; |
320 |
< |
int mask = elements.length - 1; |
321 |
< |
int i = head; |
322 |
< |
Object x; |
323 |
< |
while ( (x = elements[i]) != null) { |
324 |
< |
if (o.equals(x)) { |
325 |
< |
delete(i); |
326 |
< |
return true; |
318 |
> |
if (o != null) { |
319 |
> |
int mask = elements.length - 1; |
320 |
> |
int i = head; |
321 |
> |
Object x; |
322 |
> |
while ( (x = elements[i]) != null) { |
323 |
> |
if (o.equals(x)) { |
324 |
> |
delete(i); |
325 |
> |
return true; |
326 |
> |
} |
327 |
> |
i = (i + 1) & mask; |
328 |
|
} |
327 |
– |
i = (i + 1) & mask; |
329 |
|
} |
330 |
|
return false; |
331 |
|
} |
612 |
|
} |
613 |
|
} |
614 |
|
|
615 |
+ |
/** |
616 |
+ |
* This class is nearly a mirror-image of DeqIterator, using tail |
617 |
+ |
* instead of head for initial cursor, and head instead of tail |
618 |
+ |
* for fence. |
619 |
+ |
*/ |
620 |
|
private class DescendingIterator implements Iterator<E> { |
615 |
– |
/* |
616 |
– |
* This class is nearly a mirror-image of DeqIterator, using |
617 |
– |
* tail instead of head for initial cursor, and head instead of |
618 |
– |
* tail for fence. |
619 |
– |
*/ |
621 |
|
private int cursor = tail; |
622 |
|
private int fence = head; |
623 |
|
private int lastRet = -1; |
658 |
|
* @return {@code true} if this deque contains the specified element |
659 |
|
*/ |
660 |
|
public boolean contains(Object o) { |
661 |
< |
if (o == null) |
662 |
< |
return false; |
663 |
< |
int mask = elements.length - 1; |
664 |
< |
int i = head; |
665 |
< |
Object x; |
666 |
< |
while ( (x = elements[i]) != null) { |
667 |
< |
if (o.equals(x)) |
668 |
< |
return true; |
669 |
< |
i = (i + 1) & mask; |
661 |
> |
if (o != null) { |
662 |
> |
int mask = elements.length - 1; |
663 |
> |
int i = head; |
664 |
> |
Object x; |
665 |
> |
while ( (x = elements[i]) != null) { |
666 |
> |
if (o.equals(x)) |
667 |
> |
return true; |
668 |
> |
i = (i + 1) & mask; |
669 |
> |
} |
670 |
|
} |
671 |
|
return false; |
672 |
|
} |
810 |
|
/** |
811 |
|
* Saves this deque to a stream (that is, serializes it). |
812 |
|
* |
813 |
+ |
* @param s the stream |
814 |
+ |
* @throws java.io.IOException if an I/O error occurs |
815 |
|
* @serialData The current size ({@code int}) of the deque, |
816 |
|
* followed by all of its elements (each an object reference) in |
817 |
|
* first-to-last order. |
831 |
|
|
832 |
|
/** |
833 |
|
* Reconstitutes this deque from a stream (that is, deserializes it). |
834 |
+ |
* @param s the stream |
835 |
+ |
* @throws ClassNotFoundException if the class of a serialized object |
836 |
+ |
* could not be found |
837 |
+ |
* @throws java.io.IOException if an I/O error occurs |
838 |
|
*/ |
839 |
|
private void readObject(java.io.ObjectInputStream s) |
840 |
|
throws java.io.IOException, ClassNotFoundException { |
851 |
|
elements[i] = s.readObject(); |
852 |
|
} |
853 |
|
|
854 |
< |
Spliterator<E> spliterator() { |
854 |
> |
public Spliterator<E> spliterator() { |
855 |
|
return new DeqSpliterator<E>(this, -1, -1); |
856 |
|
} |
857 |
|
|
851 |
– |
public Stream<E> stream() { |
852 |
– |
return Streams.stream(spliterator()); |
853 |
– |
} |
854 |
– |
|
855 |
– |
public Stream<E> parallelStream() { |
856 |
– |
return Streams.parallelStream(spliterator()); |
857 |
– |
} |
858 |
– |
|
858 |
|
static final class DeqSpliterator<E> implements Spliterator<E> { |
859 |
|
private final ArrayDeque<E> deq; |
860 |
|
private int fence; // -1 until first use |
876 |
|
return t; |
877 |
|
} |
878 |
|
|
879 |
< |
public DeqSpliterator<E> trySplit() { |
879 |
> |
public Spliterator<E> trySplit() { |
880 |
|
int t = getFence(), h = index, n = deq.elements.length; |
881 |
|
if (h != t && ((h + 1) & (n - 1)) != t) { |
882 |
|
if (h > t) |
887 |
|
return null; |
888 |
|
} |
889 |
|
|
890 |
< |
public void forEach(Consumer<? super E> consumer) { |
890 |
> |
public void forEachRemaining(Consumer<? super E> consumer) { |
891 |
|
if (consumer == null) |
892 |
|
throw new NullPointerException(); |
893 |
|
Object[] a = deq.elements; |