diff --git a/src/main/java/spliterators/part2/example/IndexedPair.java b/src/main/java/spliterators/part2/example/IndexedPair.java index ba75771..7324e64 100755 --- a/src/main/java/spliterators/part2/example/IndexedPair.java +++ b/src/main/java/spliterators/part2/example/IndexedPair.java @@ -5,15 +5,15 @@ import org.apache.commons.lang3.builder.ToStringBuilder; public class IndexedPair<T> { - private final int index; + private final long index; private final T value; - public IndexedPair(int index, T value) { + public IndexedPair(long index, T value) { this.index = index; this.value = value; } - public int getIndex() { + public long getIndex() { return index; } diff --git a/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java b/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java index d3c0162..424948b 100755 --- a/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java +++ b/src/main/java/spliterators/part2/exercise/IndexedDoublePair.java @@ -5,15 +5,15 @@ import org.apache.commons.lang3.builder.ToStringBuilder; public class IndexedDoublePair { - private final int index; + private final long index; private final double value; - public IndexedDoublePair(int index, double value) { + public IndexedDoublePair(long index, double value) { this.index = index; this.value = value; } - public int getIndex() { + public long getIndex() { return index; } diff --git a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java index deb4867..bac66a3 100755 --- a/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java +++ b/src/main/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliterator.java @@ -3,18 +3,19 @@ import java.util.Spliterator; import java.util.Spliterators; import java.util.function.Consumer; +import java.util.Comparator; public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> { private final OfDouble inner; - private int currentIndex; + private long 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()); currentIndex = firstIndex; this.inner = inner; @@ -22,35 +23,48 @@ private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) { @Override public int characteristics() { - // TODO - throw new UnsupportedOperationException(); + return inner.characteristics() | NONNULL; + + } + + @Override + public Comparator<? super IndexedDoublePair> getComparator() { + if (inner.hasCharacteristics(SORTED)) { + return Comparator.comparing(IndexedDoublePair::getValue); + } else { + throw new IllegalStateException(); + } } @Override public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) { - // TODO - throw new UnsupportedOperationException(); + boolean res = inner.tryAdvance((Double e) -> action.accept(new IndexedDoublePair(currentIndex, e))); + currentIndex += 1; + return res; } @Override public void forEachRemaining(Consumer<? super IndexedDoublePair> action) { - // TODO - throw new UnsupportedOperationException(); + inner.forEachRemaining((Double e) -> { + action.accept(new IndexedDoublePair(currentIndex, e)); + currentIndex += 1; + }); } @Override public Spliterator<IndexedDoublePair> trySplit() { - // TODO - // if (inner.hasCharacteristics(???)) { - // use inner.trySplit - // } else - - return super.trySplit(); + if (inner.hasCharacteristics(SUBSIZED)) { + OfDouble splitInner = this.inner.trySplit(); + ZipWithIndexDoubleSpliterator spliterator = new ZipWithIndexDoubleSpliterator(currentIndex, splitInner); + currentIndex += splitInner.estimateSize(); + return spliterator; + } else { + return super.trySplit(); + } } @Override public long estimateSize() { - // TODO - throw new UnsupportedOperationException(); + return inner.estimateSize(); } } diff --git a/src/test/java/spliterators/part2/example/IndexedArraySpliteratorTest.java b/src/test/java/spliterators/part2/example/IndexedArraySpliteratorTest.java index 4e9614e..6e2721b 100755 --- a/src/test/java/spliterators/part2/example/IndexedArraySpliteratorTest.java +++ b/src/test/java/spliterators/part2/example/IndexedArraySpliteratorTest.java @@ -30,7 +30,7 @@ public void comparePaired() { final List<IndexedPair<String>> result1 = Stream.iterate( new IndexedPair<>(0, randomArray[0]), - p -> new IndexedPair<>(p.getIndex() + 1, randomArray[p.getIndex() + 1])) + p -> new IndexedPair<>(p.getIndex() + 1, randomArray[(int)p.getIndex() + 1])) .limit(randomArray.length) .collect(toList()); 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..e3dce0a --- /dev/null +++ b/src/test/java/spliterators/part2/exercise/ZipWithIndexDoubleSpliteratorTest.java @@ -0,0 +1,87 @@ +package spliterators.part2.exercise; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Spliterator; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static org.junit.Assert.*; + + +public class ZipWithIndexDoubleSpliteratorTest { + + final double[] array = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; + + @Test + public void testCount() { + Spliterator.OfDouble spliterator = Arrays.spliterator(array); + final long expected = 6; + + long actual = StreamSupport + .stream(new ZipWithIndexDoubleSpliterator(spliterator), true) + .count(); + + assertEquals(expected, actual); + } + + @Test + public void testToList() { + Spliterator.OfDouble spliterator = Arrays.spliterator(array); + final List<Double> expected = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0); + + List<Double> actual = StreamSupport + .stream(new ZipWithIndexDoubleSpliterator(spliterator), true) + .map(IndexedDoublePair::getValue) + .collect(Collectors.toList()); + + assertEquals(expected, actual); + } + + @Test + public void testPaired() { + Spliterator.OfDouble spliterator = Arrays.spliterator(array); + final List<IndexedDoublePair> expected = Arrays.asList( + new IndexedDoublePair(0, 1.0), + new IndexedDoublePair(1, 2.0), + new IndexedDoublePair(2, 3.0), + new IndexedDoublePair(3, 4.0), + new IndexedDoublePair(4, 5.0), + new IndexedDoublePair(5, 6.0) + ); + + List<IndexedDoublePair> actual = StreamSupport + .stream(new ZipWithIndexDoubleSpliterator(spliterator), true) + .collect(Collectors.toList()); + + assertEquals(expected, actual); + } + + @Test + public void testSkip() { + Spliterator.OfDouble spliterator = Arrays.spliterator(array); + + double actual = StreamSupport + .stream(new ZipWithIndexDoubleSpliterator(spliterator), true) + .skip(3) + .mapToDouble(IndexedDoublePair::getValue) + .sum(); + + assertTrue(15.0 == actual); + } + + @Test + public void testLimit() { + Spliterator.OfDouble spliterator = Arrays.spliterator(array); + + double actual = StreamSupport + .stream(new ZipWithIndexDoubleSpliterator(spliterator), true) + .limit(3) + .mapToDouble(IndexedDoublePair::getValue) + .sum(); + + assertTrue(6.0 == actual); + } +} \ No newline at end of file