989 |
|
} |
990 |
|
|
991 |
|
/** A customized variant of Spliterators.IteratorSpliterator */ |
992 |
< |
static final class LTQSpliterator<E> implements Spliterator<E> { |
992 |
> |
final class LTQSpliterator<E> implements Spliterator<E> { |
993 |
|
static final int MAX_BATCH = 1 << 25; // max batch array size; |
994 |
– |
final LinkedTransferQueue<E> queue; |
994 |
|
Node current; // current node; null until initialized |
995 |
|
int batch; // batch size for splits |
996 |
|
boolean exhausted; // true when no more nodes |
997 |
< |
LTQSpliterator(LinkedTransferQueue<E> queue) { |
999 |
< |
this.queue = queue; |
1000 |
< |
} |
997 |
> |
LTQSpliterator() {} |
998 |
|
|
999 |
|
public Spliterator<E> trySplit() { |
1000 |
|
Node p; |
1004 |
– |
final LinkedTransferQueue<E> q = this.queue; |
1001 |
|
int b = batch; |
1002 |
|
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1; |
1003 |
|
if (!exhausted && |
1004 |
< |
((p = current) != null || (p = q.firstDataNode()) != null) && |
1004 |
> |
((p = current) != null || (p = firstDataNode()) != null) && |
1005 |
|
p.next != null) { |
1006 |
|
Object[] a = new Object[n]; |
1007 |
|
int i = 0; |
1010 |
|
if (e != p && (a[i] = e) != null) |
1011 |
|
++i; |
1012 |
|
if (p == (p = p.next)) |
1013 |
< |
p = q.firstDataNode(); |
1013 |
> |
p = firstDataNode(); |
1014 |
|
} while (p != null && i < n && p.isData); |
1015 |
|
if ((current = p) == null) |
1016 |
|
exhausted = true; |
1029 |
|
public void forEachRemaining(Consumer<? super E> action) { |
1030 |
|
Node p; |
1031 |
|
if (action == null) throw new NullPointerException(); |
1036 |
– |
final LinkedTransferQueue<E> q = this.queue; |
1032 |
|
if (!exhausted && |
1033 |
< |
((p = current) != null || (p = q.firstDataNode()) != null)) { |
1033 |
> |
((p = current) != null || (p = firstDataNode()) != null)) { |
1034 |
|
exhausted = true; |
1035 |
|
do { |
1036 |
|
Object e = p.item; |
1037 |
|
if (e != null && e != p) |
1038 |
|
action.accept((E)e); |
1039 |
|
if (p == (p = p.next)) |
1040 |
< |
p = q.firstDataNode(); |
1040 |
> |
p = firstDataNode(); |
1041 |
|
} while (p != null && p.isData); |
1042 |
|
} |
1043 |
|
} |
1046 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
1047 |
|
Node p; |
1048 |
|
if (action == null) throw new NullPointerException(); |
1054 |
– |
final LinkedTransferQueue<E> q = this.queue; |
1049 |
|
if (!exhausted && |
1050 |
< |
((p = current) != null || (p = q.firstDataNode()) != null)) { |
1050 |
> |
((p = current) != null || (p = firstDataNode()) != null)) { |
1051 |
|
Object e; |
1052 |
|
do { |
1053 |
|
if ((e = p.item) == p) |
1054 |
|
e = null; |
1055 |
|
if (p == (p = p.next)) |
1056 |
< |
p = q.firstDataNode(); |
1056 |
> |
p = firstDataNode(); |
1057 |
|
} while (e == null && p != null && p.isData); |
1058 |
|
if ((current = p) == null) |
1059 |
|
exhausted = true; |
1090 |
|
* @since 1.8 |
1091 |
|
*/ |
1092 |
|
public Spliterator<E> spliterator() { |
1093 |
< |
return new LTQSpliterator<E>(this); |
1093 |
> |
return new LTQSpliterator<E>(); |
1094 |
|
} |
1095 |
|
|
1096 |
|
/* -------------- Removal methods -------------- */ |