@Test(dataProvider = "SpinedBuffer", groups = { "serialization-hostile" }) public void testLastSplit(int[] array, SpinedBuffer<Integer> sb) { Spliterator<Integer> spliterator = sb.spliterator(); Spliterator<Integer> split = spliterator.trySplit(); long splitSizes = (split == null) ? 0 : split.getExactSizeIfKnown(); long lastSplitSize = spliterator.getExactSizeIfKnown(); splitSizes += lastSplitSize; assertEquals(splitSizes, array.length); List<Integer> contentOfLastSplit = new ArrayList<>(); spliterator.forEachRemaining(contentOfLastSplit::add); assertEquals(contentOfLastSplit.size(), lastSplitSize); List<Integer> end = Arrays.stream(array) .boxed() .skip(array.length - lastSplitSize) .collect(Collectors.toList()); assertEquals(contentOfLastSplit, end); }
public Spliterator<E> spliterator() { class Splitr implements Spliterator<E> { int splSpineIndex; final int lastSpineIndex; int splElementIndex; final int lastSpineElementFence; E[] splChunk; Splitr(int firstSpineIndex, int lastSpineIndex, int firstSpineElementIndex, int lastSpineElementFence) { this.splSpineIndex = firstSpineIndex; this.lastSpineIndex = lastSpineIndex; this.splElementIndex = firstSpineElementIndex; this.lastSpineElementFence = lastSpineElementFence; assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; } @Override public long estimateSize() { return (splSpineIndex == lastSpineIndex) ? (long) lastSpineElementFence - splElementIndex : priorElementCount[lastSpineIndex] + lastSpineElementFence - priorElementCount[splSpineIndex] - splElementIndex; } @Override public int characteristics() { return SPLITERATOR_CHARACTERISTICS; } @Override public boolean tryAdvance(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { consumer.accept(splChunk[splElementIndex++]); if (splElementIndex == splChunk.length) { splElementIndex = 0; ++splSpineIndex; if (spine != null && splSpineIndex <= lastSpineIndex) splChunk = spine[splSpineIndex]; } return true; } return false; } @Override public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { int i = splElementIndex; for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { E[] chunk = spine[sp]; for (; i < chunk.length; i++) { consumer.accept(chunk[i]); } i = 0; } E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; int hElementIndex = lastSpineElementFence; for (; i < hElementIndex; i++) { consumer.accept(chunk[i]); } splSpineIndex = lastSpineIndex; splElementIndex = lastSpineElementFence; } } @Override public Spliterator<E> trySplit() { if (splSpineIndex < lastSpineIndex) { Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1, splElementIndex, spine[lastSpineIndex-1].length); splSpineIndex = lastSpineIndex; splElementIndex = 0; splChunk = spine[splSpineIndex]; return ret; } else if (splSpineIndex == lastSpineIndex) { int t = (lastSpineElementFence - splElementIndex) / 2; if (t == 0) return null; else { Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t); splElementIndex += t; return ret; } } else { return null; } } } return new Splitr(0, spineIndex, 0, elementIndex); }
SpinedBuffer extends AbstractSpinedBuffer implements Consumer<E>, Iterable<E> { public Spliterator<E> spliterator() { class Splitr implements Spliterator<E> { int splSpineIndex; final int lastSpineIndex; int splElementIndex; final int lastSpineElementFence; E[] splChunk; Splitr(int firstSpineIndex, int lastSpineIndex, int firstSpineElementIndex, int lastSpineElementFence) { this.splSpineIndex = firstSpineIndex; this.lastSpineIndex = lastSpineIndex; this.splElementIndex = firstSpineElementIndex; this.lastSpineElementFence = lastSpineElementFence; assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; } @Override public long estimateSize() { return (splSpineIndex == lastSpineIndex) ? (long) lastSpineElementFence - splElementIndex : priorElementCount[lastSpineIndex] + lastSpineElementFence - priorElementCount[splSpineIndex] - splElementIndex; } @Override public int characteristics() { return SPLITERATOR_CHARACTERISTICS; } @Override public boolean tryAdvance(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { consumer.accept(splChunk[splElementIndex++]); if (splElementIndex == splChunk.length) { splElementIndex = 0; ++splSpineIndex; if (spine != null && splSpineIndex <= lastSpineIndex) splChunk = spine[splSpineIndex]; } return true; } return false; } @Override public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { int i = splElementIndex; for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { E[] chunk = spine[sp]; for (; i < chunk.length; i++) { consumer.accept(chunk[i]); } i = 0; } E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; int hElementIndex = lastSpineElementFence; for (; i < hElementIndex; i++) { consumer.accept(chunk[i]); } splSpineIndex = lastSpineIndex; splElementIndex = lastSpineElementFence; } } @Override public Spliterator<E> trySplit() { if (splSpineIndex < lastSpineIndex) { Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1, splElementIndex, spine[lastSpineIndex-1].length); splSpineIndex = lastSpineIndex; splElementIndex = 0; splChunk = spine[splSpineIndex]; return ret; } else if (splSpineIndex == lastSpineIndex) { int t = (lastSpineElementFence - splElementIndex) / 2; if (t == 0) return null; else { Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t); splElementIndex += t; return ret; } } else { return null; } } } return new Splitr(0, spineIndex, 0, elementIndex); } }
SpinedBuffer extends AbstractSpinedBuffer implements Consumer<E>, Iterable<E> { public Spliterator<E> spliterator() { class Splitr implements Spliterator<E> { int splSpineIndex; final int lastSpineIndex; int splElementIndex; final int lastSpineElementFence; E[] splChunk; Splitr(int firstSpineIndex, int lastSpineIndex, int firstSpineElementIndex, int lastSpineElementFence) { this.splSpineIndex = firstSpineIndex; this.lastSpineIndex = lastSpineIndex; this.splElementIndex = firstSpineElementIndex; this.lastSpineElementFence = lastSpineElementFence; assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; } @Override public long estimateSize() { return (splSpineIndex == lastSpineIndex) ? (long) lastSpineElementFence - splElementIndex : priorElementCount[lastSpineIndex] + lastSpineElementFence - priorElementCount[splSpineIndex] - splElementIndex; } @Override public int characteristics() { return SPLITERATOR_CHARACTERISTICS; } @Override public boolean tryAdvance(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { consumer.accept(splChunk[splElementIndex++]); if (splElementIndex == splChunk.length) { splElementIndex = 0; ++splSpineIndex; if (spine != null && splSpineIndex <= lastSpineIndex) splChunk = spine[splSpineIndex]; } return true; } return false; } @Override public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { int i = splElementIndex; for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { E[] chunk = spine[sp]; for (; i < chunk.length; i++) { consumer.accept(chunk[i]); } i = 0; } E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; int hElementIndex = lastSpineElementFence; for (; i < hElementIndex; i++) { consumer.accept(chunk[i]); } splSpineIndex = lastSpineIndex; splElementIndex = lastSpineElementFence; } } @Override public Spliterator<E> trySplit() { if (splSpineIndex < lastSpineIndex) { Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1, splElementIndex, spine[lastSpineIndex-1].length); splSpineIndex = lastSpineIndex; splElementIndex = 0; splChunk = spine[splSpineIndex]; return ret; } else if (splSpineIndex == lastSpineIndex) { int t = (lastSpineElementFence - splElementIndex) / 2; if (t == 0) return null; else { Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t); splElementIndex += t; return ret; } } else { return null; } } } return new Splitr(0, spineIndex, 0, elementIndex); } @SuppressWarnings("unchecked")  SpinedBuffer(int initialCapacity); @SuppressWarnings("unchecked")  SpinedBuffer(); }
SpinedBuffer extends AbstractSpinedBuffer implements Consumer<E>, Iterable<E> { public Spliterator<E> spliterator() { class Splitr implements Spliterator<E> { int splSpineIndex; final int lastSpineIndex; int splElementIndex; final int lastSpineElementFence; E[] splChunk; Splitr(int firstSpineIndex, int lastSpineIndex, int firstSpineElementIndex, int lastSpineElementFence) { this.splSpineIndex = firstSpineIndex; this.lastSpineIndex = lastSpineIndex; this.splElementIndex = firstSpineElementIndex; this.lastSpineElementFence = lastSpineElementFence; assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; } @Override public long estimateSize() { return (splSpineIndex == lastSpineIndex) ? (long) lastSpineElementFence - splElementIndex : priorElementCount[lastSpineIndex] + lastSpineElementFence - priorElementCount[splSpineIndex] - splElementIndex; } @Override public int characteristics() { return SPLITERATOR_CHARACTERISTICS; } @Override public boolean tryAdvance(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { consumer.accept(splChunk[splElementIndex++]); if (splElementIndex == splChunk.length) { splElementIndex = 0; ++splSpineIndex; if (spine != null && splSpineIndex <= lastSpineIndex) splChunk = spine[splSpineIndex]; } return true; } return false; } @Override public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { int i = splElementIndex; for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { E[] chunk = spine[sp]; for (; i < chunk.length; i++) { consumer.accept(chunk[i]); } i = 0; } E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; int hElementIndex = lastSpineElementFence; for (; i < hElementIndex; i++) { consumer.accept(chunk[i]); } splSpineIndex = lastSpineIndex; splElementIndex = lastSpineElementFence; } } @Override public Spliterator<E> trySplit() { if (splSpineIndex < lastSpineIndex) { Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1, splElementIndex, spine[lastSpineIndex-1].length); splSpineIndex = lastSpineIndex; splElementIndex = 0; splChunk = spine[splSpineIndex]; return ret; } else if (splSpineIndex == lastSpineIndex) { int t = (lastSpineElementFence - splElementIndex) / 2; if (t == 0) return null; else { Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t); splElementIndex += t; return ret; } } else { return null; } } } return new Splitr(0, spineIndex, 0, elementIndex); } @SuppressWarnings("unchecked")  SpinedBuffer(int initialCapacity); @SuppressWarnings("unchecked")  SpinedBuffer(); E get(long index); void copyInto(E[] array, int offset); E[] asArray(IntFunction<E[]> arrayFactory); @Override void clear(); @Override Iterator<E> iterator(); @Override void forEach(Consumer<? super E> consumer); @Override void accept(E e); @Override String toString(); Spliterator<E> spliterator(); }
SpinedBuffer extends AbstractSpinedBuffer implements Consumer<E>, Iterable<E> { public Spliterator<E> spliterator() { class Splitr implements Spliterator<E> { int splSpineIndex; final int lastSpineIndex; int splElementIndex; final int lastSpineElementFence; E[] splChunk; Splitr(int firstSpineIndex, int lastSpineIndex, int firstSpineElementIndex, int lastSpineElementFence) { this.splSpineIndex = firstSpineIndex; this.lastSpineIndex = lastSpineIndex; this.splElementIndex = firstSpineElementIndex; this.lastSpineElementFence = lastSpineElementFence; assert spine != null || firstSpineIndex == 0 && lastSpineIndex == 0; splChunk = (spine == null) ? curChunk : spine[firstSpineIndex]; } @Override public long estimateSize() { return (splSpineIndex == lastSpineIndex) ? (long) lastSpineElementFence - splElementIndex : priorElementCount[lastSpineIndex] + lastSpineElementFence - priorElementCount[splSpineIndex] - splElementIndex; } @Override public int characteristics() { return SPLITERATOR_CHARACTERISTICS; } @Override public boolean tryAdvance(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { consumer.accept(splChunk[splElementIndex++]); if (splElementIndex == splChunk.length) { splElementIndex = 0; ++splSpineIndex; if (spine != null && splSpineIndex <= lastSpineIndex) splChunk = spine[splSpineIndex]; } return true; } return false; } @Override public void forEachRemaining(Consumer<? super E> consumer) { Objects.requireNonNull(consumer); if (splSpineIndex < lastSpineIndex || (splSpineIndex == lastSpineIndex && splElementIndex < lastSpineElementFence)) { int i = splElementIndex; for (int sp = splSpineIndex; sp < lastSpineIndex; sp++) { E[] chunk = spine[sp]; for (; i < chunk.length; i++) { consumer.accept(chunk[i]); } i = 0; } E[] chunk = (splSpineIndex == lastSpineIndex) ? splChunk : spine[lastSpineIndex]; int hElementIndex = lastSpineElementFence; for (; i < hElementIndex; i++) { consumer.accept(chunk[i]); } splSpineIndex = lastSpineIndex; splElementIndex = lastSpineElementFence; } } @Override public Spliterator<E> trySplit() { if (splSpineIndex < lastSpineIndex) { Spliterator<E> ret = new Splitr(splSpineIndex, lastSpineIndex - 1, splElementIndex, spine[lastSpineIndex-1].length); splSpineIndex = lastSpineIndex; splElementIndex = 0; splChunk = spine[splSpineIndex]; return ret; } else if (splSpineIndex == lastSpineIndex) { int t = (lastSpineElementFence - splElementIndex) / 2; if (t == 0) return null; else { Spliterator<E> ret = Arrays.spliterator(splChunk, splElementIndex, splElementIndex + t); splElementIndex += t; return ret; } } else { return null; } } } return new Splitr(0, spineIndex, 0, elementIndex); } @SuppressWarnings("unchecked")  SpinedBuffer(int initialCapacity); @SuppressWarnings("unchecked")  SpinedBuffer(); E get(long index); void copyInto(E[] array, int offset); E[] asArray(IntFunction<E[]> arrayFactory); @Override void clear(); @Override Iterator<E> iterator(); @Override void forEach(Consumer<? super E> consumer); @Override void accept(E e); @Override String toString(); Spliterator<E> spliterator();  }