From 0ca207966b5b7a2775dbfc9f56d11c8694e90d12 Mon Sep 17 00:00:00 2001 From: andrei Date: Mon, 17 Jul 2017 22:46:22 +0300 Subject: [PATCH] part2 done --- .../ZipWithIndexDoubleSpliterator.java | 46 +++++++++++------- .../ZipWithIndexDoubleSpliteratorTest.java | 47 +++++++++++++++++++ 2 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 src/test/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliteratorTest.java diff --git a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java index deb4867..b9e8ebd 100755 --- a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java +++ b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java @@ -1,12 +1,13 @@ package spliterators.part2.exercise; +import java.util.Comparator; +import java.util.Optional; import java.util.Spliterator; import java.util.Spliterators; import java.util.function.Consumer; public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator { - private final OfDouble inner; private int currentIndex; @@ -15,42 +16,55 @@ public ZipWithIndexDoubleSpliterator(OfDouble inner) { } private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) { - super(inner.estimateSize(), inner.characteristics()); + super(inner.estimateSize(), IMMUTABLE | ORDERED | SORTED | SIZED | SUBSIZED | NONNULL); currentIndex = firstIndex; this.inner = inner; } @Override public int characteristics() { - // TODO - throw new UnsupportedOperationException(); + return IMMUTABLE | ORDERED | SORTED | SIZED | SUBSIZED | NONNULL; } @Override public boolean tryAdvance(Consumer action) { - // TODO - throw new UnsupportedOperationException(); + if (inner.tryAdvance((double d) -> + action.accept(new IndexedDoublePair(currentIndex, d)))) { + currentIndex++; + return true; + } + return false; + } + + @Override + public Comparator getComparator() { + return (i1, i2) -> inner.getComparator().compare(i1.getValue(), i2.getValue()); } @Override public void forEachRemaining(Consumer action) { - // TODO - throw new UnsupportedOperationException(); + while (inner.tryAdvance((double d) -> + action.accept(new IndexedDoublePair(currentIndex, d)))) { + currentIndex++; + } } @Override public Spliterator trySplit() { - // TODO - // if (inner.hasCharacteristics(???)) { - // use inner.trySplit - // } else - - return super.trySplit(); + if (inner.hasCharacteristics(SUBSIZED)) { + return Optional.ofNullable(inner.trySplit()) + .map(ofD -> { + int newCurrentIndex = currentIndex; + currentIndex += (int) ofD.estimateSize(); + return new ZipWithIndexDoubleSpliterator(newCurrentIndex, ofD);}) + .orElse(null); + } else { + return null; + } } @Override public long estimateSize() { - // TODO - throw new UnsupportedOperationException(); + return currentIndex + inner.estimateSize(); } } diff --git a/src/test/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliteratorTest.java b/src/test/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliteratorTest.java new file mode 100644 index 0000000..d970596 --- /dev/null +++ b/src/test/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliteratorTest.java @@ -0,0 +1,47 @@ +package spliterators.part2.exercise; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import static java.util.stream.Collectors.toList; +import static org.junit.Assert.assertEquals; + +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().nextDouble(); + } + + return result; + } + + @Test + public void comparePaired() { + final double[] randomArray = getRandomArray(1000); + + final List result1 = + Stream.iterate( + new IndexedDoublePair(0, randomArray[0]), + p -> new IndexedDoublePair(p.getIndex() + 1, randomArray[p.getIndex() + 1])) + .limit(randomArray.length) + .collect(toList()); + + final List result2 = + StreamSupport.stream(new ZipWithIndexDoubleSpliterator(Arrays.stream(randomArray).spliterator()), true) + .map(p -> new IndexedDoublePair(p.getIndex() + 1, p.getValue())) + .map(p -> new IndexedDoublePair(p.getIndex() - 1, p.getValue())) + .collect(toList()); + + assertEquals(result1, result2); + + } + +} \ No newline at end of file