8 |
|
|
9 |
|
import java.util.concurrent.locks.Condition; |
10 |
|
import java.util.concurrent.locks.ReentrantLock; |
11 |
< |
import java.util.*; |
11 |
> |
import java.util.AbstractQueue; |
12 |
> |
import java.util.Arrays; |
13 |
> |
import java.util.Collection; |
14 |
> |
import java.util.Comparator; |
15 |
> |
import java.util.Iterator; |
16 |
> |
import java.util.NoSuchElementException; |
17 |
> |
import java.util.PriorityQueue; |
18 |
> |
import java.util.Queue; |
19 |
> |
import java.util.SortedSet; |
20 |
> |
import java.util.Spliterator; |
21 |
> |
import java.util.stream.Stream; |
22 |
> |
import java.util.stream.Streams; |
23 |
> |
import java.util.function.Block; |
24 |
|
|
25 |
|
/** |
26 |
|
* An unbounded {@linkplain BlockingQueue blocking queue} that uses |
919 |
|
} |
920 |
|
} |
921 |
|
|
922 |
+ |
// wrapping constructor in method avoids transient javac problems |
923 |
+ |
final PBQSpliterator<E> spliterator() { |
924 |
+ |
Object[] a = toArray(); |
925 |
+ |
return new PBQSpliterator(a, 0, a.length); |
926 |
+ |
} |
927 |
+ |
|
928 |
+ |
public Stream<E> stream() { |
929 |
+ |
int flags = Streams.STREAM_IS_SIZED; |
930 |
+ |
return Streams.stream |
931 |
+ |
(() -> spliterator(), flags); |
932 |
+ |
} |
933 |
+ |
public Stream<E> parallelStream() { |
934 |
+ |
int flags = Streams.STREAM_IS_SIZED; |
935 |
+ |
return Streams.parallelStream |
936 |
+ |
(() -> spliterator(), flags); |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
/** Index-based split-by-two Spliterator */ |
940 |
+ |
static final class PBQSpliterator<E> implements Spliterator<E>, Iterator<E> { |
941 |
+ |
private final Object[] array; |
942 |
+ |
private int index; // current index, modified on advance/split |
943 |
+ |
private final int fence; // one past last index |
944 |
+ |
|
945 |
+ |
/** Create new spliterator covering the given array and range */ |
946 |
+ |
PBQSpliterator(Object[] array, int origin, int fence) { |
947 |
+ |
this.array = array; this.index = origin; this.fence = fence; |
948 |
+ |
} |
949 |
+ |
|
950 |
+ |
public PBQSpliterator<E> trySplit() { |
951 |
+ |
int lo = index, mid = (lo + fence) >>> 1; |
952 |
+ |
return (lo >= mid)? null : |
953 |
+ |
new PBQSpliterator<E>(array, lo, index = mid); |
954 |
+ |
} |
955 |
+ |
|
956 |
+ |
public void forEach(Block<? super E> block) { |
957 |
+ |
Object[] a; int i, hi; // hoist accesses and checks from loop |
958 |
+ |
if (block == null) |
959 |
+ |
throw new NullPointerException(); |
960 |
+ |
if ((a = array).length >= (hi = fence) && |
961 |
+ |
(i = index) >= 0 && i < hi) { |
962 |
+ |
index = hi; |
963 |
+ |
do { |
964 |
+ |
@SuppressWarnings("unchecked") E e = (E) a[i]; |
965 |
+ |
block.accept(e); |
966 |
+ |
} while (++i < hi); |
967 |
+ |
} |
968 |
+ |
} |
969 |
+ |
|
970 |
+ |
public boolean tryAdvance(Block<? super E> block) { |
971 |
+ |
if (index >= 0 && index < fence) { |
972 |
+ |
@SuppressWarnings("unchecked") E e = (E) array[index++]; |
973 |
+ |
block.accept(e); |
974 |
+ |
return true; |
975 |
+ |
} |
976 |
+ |
return false; |
977 |
+ |
} |
978 |
+ |
|
979 |
+ |
public long estimateSize() { return (long)(fence - index); } |
980 |
+ |
public boolean hasExactSize() { return true; } |
981 |
+ |
public boolean hasExactSplits() { return true; } |
982 |
+ |
|
983 |
+ |
// Iterator support |
984 |
+ |
public Iterator<E> iterator() { return this; } |
985 |
+ |
public void remove() { throw new UnsupportedOperationException(); } |
986 |
+ |
public boolean hasNext() { return index >= 0 && index < fence; } |
987 |
+ |
|
988 |
+ |
public E next() { |
989 |
+ |
if (index < 0 || index >= fence) |
990 |
+ |
throw new NoSuchElementException(); |
991 |
+ |
@SuppressWarnings("unchecked") E e = (E) array[index++]; |
992 |
+ |
return e; |
993 |
+ |
} |
994 |
+ |
} |
995 |
+ |
|
996 |
|
// Unsafe mechanics |
997 |
|
private static final sun.misc.Unsafe UNSAFE; |
998 |
|
private static final long allocationSpinLockOffset; |