1 |
|
/* |
2 |
< |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
< |
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 |
> |
* Written by Josh Bloch of Google Inc. and released to the public domain, |
3 |
> |
* as explained at http://creativecommons.org/publicdomain/zero/1.0/. |
4 |
|
*/ |
5 |
|
|
6 |
|
package java.util; |
7 |
< |
import java.util.Spliterator; |
7 |
> |
import java.io.Serializable; |
8 |
> |
import java.util.function.Consumer; |
9 |
|
import java.util.stream.Stream; |
10 |
|
import java.util.stream.Streams; |
11 |
– |
import java.util.function.Consumer; |
11 |
|
|
12 |
|
/** |
13 |
|
* Resizable-array implementation of the {@link Deque} interface. Array |
55 |
|
* @param <E> the type of elements held in this collection |
56 |
|
*/ |
57 |
|
public class ArrayDeque<E> extends AbstractCollection<E> |
58 |
< |
implements Deque<E>, Cloneable, java.io.Serializable |
58 |
> |
implements Deque<E>, Cloneable, Serializable |
59 |
|
{ |
60 |
|
/** |
61 |
|
* The array in which the elements of the deque are stored. |
846 |
|
elements[i] = s.readObject(); |
847 |
|
} |
848 |
|
|
849 |
+ |
Spliterator<E> spliterator() { |
850 |
+ |
return new DeqSpliterator<E>(this, -1, -1); |
851 |
+ |
} |
852 |
+ |
|
853 |
|
public Stream<E> stream() { |
854 |
< |
int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED; |
852 |
< |
return Streams.stream |
853 |
< |
(() -> new DeqSpliterator<E>(this, head, tail), flags); |
854 |
> |
return Streams.stream(spliterator()); |
855 |
|
} |
856 |
+ |
|
857 |
|
public Stream<E> parallelStream() { |
858 |
< |
int flags = Streams.STREAM_IS_ORDERED | Streams.STREAM_IS_SIZED; |
857 |
< |
return Streams.parallelStream |
858 |
< |
(() -> new DeqSpliterator<E>(this, head, tail), flags); |
858 |
> |
return Streams.parallelStream(spliterator()); |
859 |
|
} |
860 |
|
|
861 |
– |
|
861 |
|
static final class DeqSpliterator<E> implements Spliterator<E> { |
862 |
|
private final ArrayDeque<E> deq; |
863 |
< |
private final int fence; // initially tail |
864 |
< |
private int index; // current index, modified on traverse/split |
865 |
< |
|
863 |
> |
private int fence; // -1 until first use |
864 |
> |
private int index; // current index, modified on traverse/split |
865 |
> |
|
866 |
|
/** Create new spliterator covering the given array and range */ |
867 |
|
DeqSpliterator(ArrayDeque<E> deq, int origin, int fence) { |
868 |
< |
this.deq = deq; this.index = origin; this.fence = fence; |
868 |
> |
this.deq = deq; |
869 |
> |
this.index = origin; |
870 |
> |
this.fence = fence; |
871 |
> |
} |
872 |
> |
|
873 |
> |
private int getFence() { // force initialization |
874 |
> |
int t; |
875 |
> |
if ((t = fence) < 0) { |
876 |
> |
t = fence = deq.tail; |
877 |
> |
index = deq.head; |
878 |
> |
} |
879 |
> |
return t; |
880 |
|
} |
881 |
|
|
882 |
|
public DeqSpliterator<E> trySplit() { |
883 |
< |
int n = deq.elements.length; |
874 |
< |
int h = index, t = fence; |
883 |
> |
int t = getFence(), h = index, n = deq.elements.length; |
884 |
|
if (h != t && ((h + 1) & (n - 1)) != t) { |
885 |
|
if (h > t) |
886 |
|
t += n; |
887 |
|
int m = ((h + t) >>> 1) & (n - 1); |
888 |
< |
return new DeqSpliterator<E>(deq, h, index = m); |
888 |
> |
return new DeqSpliterator<>(deq, h, index = m); |
889 |
|
} |
890 |
|
return null; |
891 |
|
} |
892 |
|
|
893 |
< |
public void forEach(Consumer<? super E> block) { |
894 |
< |
if (block == null) |
893 |
> |
public void forEach(Consumer<? super E> consumer) { |
894 |
> |
if (consumer == null) |
895 |
|
throw new NullPointerException(); |
896 |
|
Object[] a = deq.elements; |
897 |
< |
int m = a.length - 1, f = fence, i = index; |
897 |
> |
int m = a.length - 1, f = getFence(), i = index; |
898 |
|
index = f; |
899 |
|
while (i != f) { |
900 |
|
@SuppressWarnings("unchecked") E e = (E)a[i]; |
901 |
|
i = (i + 1) & m; |
902 |
|
if (e == null) |
903 |
|
throw new ConcurrentModificationException(); |
904 |
< |
block.accept(e); |
904 |
> |
consumer.accept(e); |
905 |
|
} |
906 |
|
} |
907 |
|
|
908 |
< |
public boolean tryAdvance(Consumer<? super E> block) { |
909 |
< |
if (block == null) |
908 |
> |
public boolean tryAdvance(Consumer<? super E> consumer) { |
909 |
> |
if (consumer == null) |
910 |
|
throw new NullPointerException(); |
911 |
|
Object[] a = deq.elements; |
912 |
< |
int m = a.length - 1, i = index; |
912 |
> |
int m = a.length - 1, f = getFence(), i = index; |
913 |
|
if (i != fence) { |
914 |
|
@SuppressWarnings("unchecked") E e = (E)a[i]; |
915 |
|
index = (i + 1) & m; |
916 |
|
if (e == null) |
917 |
|
throw new ConcurrentModificationException(); |
918 |
< |
block.accept(e); |
918 |
> |
consumer.accept(e); |
919 |
|
return true; |
920 |
|
} |
921 |
|
return false; |
922 |
|
} |
923 |
|
|
915 |
– |
// Other spliterator methods |
924 |
|
public long estimateSize() { |
925 |
< |
int n = fence - index; |
925 |
> |
int n = getFence() - index; |
926 |
|
if (n < 0) |
927 |
|
n += deq.elements.length; |
928 |
< |
return (long)n; |
928 |
> |
return (long) n; |
929 |
> |
} |
930 |
> |
|
931 |
> |
@Override |
932 |
> |
public int characteristics() { |
933 |
> |
return Spliterator.ORDERED | Spliterator.SIZED | |
934 |
> |
Spliterator.NONNULL | Spliterator.SUBSIZED; |
935 |
|
} |
922 |
– |
public boolean hasExactSize() { return true; } |
923 |
– |
public boolean hasExactSplits() { return true; } |
936 |
|
} |
937 |
|
|
938 |
|
} |