772 |
|
} |
773 |
|
|
774 |
|
/** A customized variant of Spliterators.IteratorSpliterator */ |
775 |
< |
static final class CLQSpliterator<E> implements Spliterator<E> { |
775 |
> |
final class CLQSpliterator implements Spliterator<E> { |
776 |
|
static final int MAX_BATCH = 1 << 25; // max batch array size; |
777 |
– |
final ConcurrentLinkedQueue<E> queue; |
777 |
|
Node<E> current; // current node; null until initialized |
778 |
|
int batch; // batch size for splits |
779 |
|
boolean exhausted; // true when no more nodes |
781 |
– |
CLQSpliterator(ConcurrentLinkedQueue<E> queue) { |
782 |
– |
this.queue = queue; |
783 |
– |
} |
780 |
|
|
781 |
|
public Spliterator<E> trySplit() { |
782 |
|
Node<E> p; |
787 |
– |
final ConcurrentLinkedQueue<E> q = this.queue; |
783 |
|
int b = batch; |
784 |
|
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1; |
785 |
|
if (!exhausted && |
786 |
< |
((p = current) != null || (p = q.first()) != null) && |
786 |
> |
((p = current) != null || (p = first()) != null) && |
787 |
|
p.next != null) { |
788 |
|
Object[] a = new Object[n]; |
789 |
|
int i = 0; |
791 |
|
if ((a[i] = p.item) != null) |
792 |
|
++i; |
793 |
|
if (p == (p = p.next)) |
794 |
< |
p = q.first(); |
794 |
> |
p = first(); |
795 |
|
} while (p != null && i < n); |
796 |
|
if ((current = p) == null) |
797 |
|
exhausted = true; |
809 |
|
public void forEachRemaining(Consumer<? super E> action) { |
810 |
|
Node<E> p; |
811 |
|
if (action == null) throw new NullPointerException(); |
817 |
– |
final ConcurrentLinkedQueue<E> q = this.queue; |
812 |
|
if (!exhausted && |
813 |
< |
((p = current) != null || (p = q.first()) != null)) { |
813 |
> |
((p = current) != null || (p = first()) != null)) { |
814 |
|
exhausted = true; |
815 |
|
do { |
816 |
|
E e = p.item; |
817 |
|
if (p == (p = p.next)) |
818 |
< |
p = q.first(); |
818 |
> |
p = first(); |
819 |
|
if (e != null) |
820 |
|
action.accept(e); |
821 |
|
} while (p != null); |
825 |
|
public boolean tryAdvance(Consumer<? super E> action) { |
826 |
|
Node<E> p; |
827 |
|
if (action == null) throw new NullPointerException(); |
834 |
– |
final ConcurrentLinkedQueue<E> q = this.queue; |
828 |
|
if (!exhausted && |
829 |
< |
((p = current) != null || (p = q.first()) != null)) { |
829 |
> |
((p = current) != null || (p = first()) != null)) { |
830 |
|
E e; |
831 |
|
do { |
832 |
|
e = p.item; |
833 |
|
if (p == (p = p.next)) |
834 |
< |
p = q.first(); |
834 |
> |
p = first(); |
835 |
|
} while (e == null && p != null); |
836 |
|
if ((current = p) == null) |
837 |
|
exhausted = true; |
869 |
|
*/ |
870 |
|
@Override |
871 |
|
public Spliterator<E> spliterator() { |
872 |
< |
return new CLQSpliterator<E>(this); |
872 |
> |
return new CLQSpliterator(); |
873 |
|
} |
874 |
|
|
875 |
|
// VarHandle mechanics |