906 |
|
* Immutable snapshot spliterator that binds to elements "late". |
907 |
|
*/ |
908 |
|
final class PBQSpliterator implements Spliterator<E> { |
909 |
< |
Object[] array; |
909 |
> |
Object[] array; // null until late-bound-initialized |
910 |
|
int index; |
911 |
|
int fence; |
912 |
|
|
913 |
+ |
PBQSpliterator() {} |
914 |
+ |
|
915 |
|
PBQSpliterator(Object[] array, int index, int fence) { |
916 |
|
this.array = array; |
917 |
|
this.index = index; |
918 |
|
this.fence = fence; |
919 |
|
} |
920 |
|
|
921 |
< |
final int getFence() { |
922 |
< |
int hi; |
923 |
< |
if ((hi = fence) < 0) |
924 |
< |
hi = fence = (array = toArray()).length; |
923 |
< |
return hi; |
921 |
> |
private int getFence() { |
922 |
> |
if (array == null) |
923 |
> |
fence = (array = toArray()).length; |
924 |
> |
return fence; |
925 |
|
} |
926 |
|
|
927 |
|
public PBQSpliterator trySplit() { |
930 |
|
new PBQSpliterator(array, lo, index = mid); |
931 |
|
} |
932 |
|
|
932 |
– |
@SuppressWarnings("unchecked") |
933 |
|
public void forEachRemaining(Consumer<? super E> action) { |
934 |
|
Objects.requireNonNull(action); |
935 |
< |
Object[] a; int i, hi; // hoist accesses and checks from loop |
936 |
< |
if ((a = array) == null) |
937 |
< |
fence = (a = toArray()).length; |
938 |
< |
if ((hi = fence) <= a.length && |
939 |
< |
(i = index) >= 0 && i < (index = hi)) { |
940 |
< |
do { action.accept((E)a[i]); } while (++i < hi); |
941 |
< |
} |
935 |
> |
final int hi = getFence(), lo = index; |
936 |
> |
final Object[] a = array; |
937 |
> |
index = hi; // ensure exhaustion |
938 |
> |
for (int i = lo; i < hi; i++) |
939 |
> |
action.accept((E) a[i]); |
940 |
|
} |
941 |
|
|
942 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
943 |
|
Objects.requireNonNull(action); |
944 |
|
if (getFence() > index && index >= 0) { |
945 |
< |
@SuppressWarnings("unchecked") E e = (E) array[index++]; |
948 |
< |
action.accept(e); |
945 |
> |
action.accept((E) array[index++]); |
946 |
|
return true; |
947 |
|
} |
948 |
|
return false; |
975 |
|
* @since 1.8 |
976 |
|
*/ |
977 |
|
public Spliterator<E> spliterator() { |
978 |
< |
return new PBQSpliterator(null, 0, -1); |
978 |
> |
return new PBQSpliterator(); |
979 |
|
} |
980 |
|
|
981 |
|
// VarHandle mechanics |