From ebaf2cf5e31da35af34e21009d76cf00da56adc5 Mon Sep 17 00:00:00 2001 From: Iartsev Kirill Date: Tue, 18 Jul 2017 16:16:30 +0300 Subject: [PATCH 1/2] part2 partially completed --- .../ZipWithIndexDoubleSpliterator.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java index deb4867..be5014c 100755 --- a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java +++ b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java @@ -2,13 +2,14 @@ import java.util.Spliterator; import java.util.Spliterators; +import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator { private final OfDouble inner; - private int currentIndex; + private AtomicInteger currentIndex; public ZipWithIndexDoubleSpliterator(OfDouble inner) { this(0, inner); @@ -16,41 +17,48 @@ public ZipWithIndexDoubleSpliterator(OfDouble inner) { private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) { super(inner.estimateSize(), inner.characteristics()); - currentIndex = firstIndex; + if (! inner.hasCharacteristics(SUBSIZED)) throw new IllegalStateException("Zip got not subsized Spliterator"); + currentIndex = new AtomicInteger(firstIndex); this.inner = inner; } @Override public int characteristics() { - // TODO - throw new UnsupportedOperationException(); + int characteristics = inner.characteristics(); + characteristics &= ~SORTED; + return characteristics; } @Override public boolean tryAdvance(Consumer action) { // TODO - throw new UnsupportedOperationException(); + final boolean res = + inner.tryAdvance((Double v) -> + action.accept(new IndexedDoublePair(currentIndex.get(), v))); + if (res) currentIndex.incrementAndGet(); + return res; } @Override public void forEachRemaining(Consumer action) { // TODO - throw new UnsupportedOperationException(); + inner.forEachRemaining((Double v) -> { + action.accept(new IndexedDoublePair(currentIndex.get(), v)); + currentIndex.incrementAndGet(); + }); } @Override public Spliterator trySplit() { // TODO - // if (inner.hasCharacteristics(???)) { - // use inner.trySplit - // } else - - return super.trySplit(); + if (inner.hasCharacteristics(SUBSIZED)) { + return new ZipWithIndexDoubleSpliterator(inner.trySplit()); + } else return super.trySplit(); } @Override public long estimateSize() { // TODO - throw new UnsupportedOperationException(); + return inner.estimateSize(); } } From f8f5517c5eddf34ff035d7aecc6cd89abd0e2e18 Mon Sep 17 00:00:00 2001 From: Iartsev Kirill Date: Mon, 31 Jul 2017 14:54:40 +0300 Subject: [PATCH 2/2] part2 completed --- .../ZipWithIndexDoubleSpliterator.java | 19 ++++--- .../ZipWithIndexDoubleSpliteratorTest.java | 49 +++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/test/java/spliterators/part2/example/ZipWithIndexDoubleSpliteratorTest.java diff --git a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java index be5014c..3acf7b4 100755 --- a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java +++ b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java @@ -3,22 +3,23 @@ import java.util.Spliterator; import java.util.Spliterators; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator { private final OfDouble inner; - private AtomicInteger currentIndex; + private AtomicLong currentIndex; public ZipWithIndexDoubleSpliterator(OfDouble inner) { this(0, inner); } - private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) { + private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) { super(inner.estimateSize(), inner.characteristics()); if (! inner.hasCharacteristics(SUBSIZED)) throw new IllegalStateException("Zip got not subsized Spliterator"); - currentIndex = new AtomicInteger(firstIndex); + currentIndex = new AtomicLong(firstIndex); this.inner = inner; } @@ -34,7 +35,7 @@ public boolean tryAdvance(Consumer action) { // TODO final boolean res = inner.tryAdvance((Double v) -> - action.accept(new IndexedDoublePair(currentIndex.get(), v))); + action.accept(new IndexedDoublePair((int)currentIndex.get(), v))); if (res) currentIndex.incrementAndGet(); return res; } @@ -43,7 +44,7 @@ public boolean tryAdvance(Consumer action) { public void forEachRemaining(Consumer action) { // TODO inner.forEachRemaining((Double v) -> { - action.accept(new IndexedDoublePair(currentIndex.get(), v)); + action.accept(new IndexedDoublePair((int)currentIndex.get(), v)); currentIndex.incrementAndGet(); }); } @@ -52,7 +53,13 @@ public void forEachRemaining(Consumer action) { public Spliterator trySplit() { // TODO if (inner.hasCharacteristics(SUBSIZED)) { - return new ZipWithIndexDoubleSpliterator(inner.trySplit()); + OfDouble newSplit = inner.trySplit(); + if (newSplit == null) + return null; + Spliterator zipped = + new ZipWithIndexDoubleSpliterator(currentIndex.get(), newSplit); + currentIndex.addAndGet(newSplit.estimateSize()); + return zipped; } else return super.trySplit(); } diff --git a/src/test/java/spliterators/part2/example/ZipWithIndexDoubleSpliteratorTest.java b/src/test/java/spliterators/part2/example/ZipWithIndexDoubleSpliteratorTest.java new file mode 100644 index 0000000..30805c6 --- /dev/null +++ b/src/test/java/spliterators/part2/example/ZipWithIndexDoubleSpliteratorTest.java @@ -0,0 +1,49 @@ +package spliterators.part2.example; + +import org.junit.Test; +import spliterators.part2.exercise.ZipWithIndexDoubleSpliterator; +import spliterators.part3.exercise.Pair; + +import java.util.List; +import java.util.Spliterators; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class ZipWithIndexDoubleSpliteratorTest { + private double[] getRandomArray(int length) { + final double[] result = new double[length]; + + for (int i = 0; i < length; i++) + result[i] = ThreadLocalRandom.current().nextInt(); + + return result; + } + + @Test + public void nonzeroLengthSuccess() { + final double[] randomArray = getRandomArray(10); + + final List collect = StreamSupport.stream(new ZipWithIndexDoubleSpliterator( + Spliterators.spliterator(randomArray, 0)), true) + .map(p -> new Pair<>(p.getIndex() + 1, p.getValue() + 1)) + .map(Pair::toString) + .collect(Collectors.toList()); + assertThat(collect.size(), is(10)); + } + + @Test + public void zeroLengthSuccess() { + final double[] randomArray = getRandomArray(0); + + final List collect = StreamSupport.stream(new ZipWithIndexDoubleSpliterator( + Spliterators.spliterator(randomArray, 0)), true) + .map(p -> new Pair<>(p.getIndex() + 1, p.getValue() + 1)) + .map(Pair::toString) + .collect(Collectors.toList()); + assertThat(collect.size(), is(0)); + } +}