From 500a2f062bbe77dec14623b328eb49140c9623c4 Mon Sep 17 00:00:00 2001 From: Vasilii Bobkov Date: Tue, 18 Jul 2017 17:50:42 +0300 Subject: [PATCH 1/2] Part 3 (not all) --- .../exercise/ZipWithArraySpliterator.java | 41 +++++++---- .../exercise/ZipWithArraySpliteratorTest.java | 68 +++++++++++++++++++ 2 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java diff --git a/src/main/java/spliterators/part3/exercise/ZipWithArraySpliterator.java b/src/main/java/spliterators/part3/exercise/ZipWithArraySpliterator.java index 292137e..3090d4e 100755 --- a/src/main/java/spliterators/part3/exercise/ZipWithArraySpliterator.java +++ b/src/main/java/spliterators/part3/exercise/ZipWithArraySpliterator.java @@ -9,40 +9,55 @@ public class ZipWithArraySpliterator extends Spliterators.AbstractSplitera private final Spliterator inner; private final B[] array; + private long currentIndex; public ZipWithArraySpliterator(Spliterator inner, B[] array) { - super(Long.MAX_VALUE, 0); // FIXME: - // TODO - throw new UnsupportedOperationException(); + this(inner, array, 0); + } + + private ZipWithArraySpliterator(Spliterator inner, B[] array, long startIndex){ + super(Long.MAX_VALUE, 0); + this.array = array; + this.inner = inner; + currentIndex = startIndex; } @Override public int characteristics() { - // TODO - throw new UnsupportedOperationException(); + return inner.characteristics(); } @Override public boolean tryAdvance(Consumer> action) { - // TODO - throw new UnsupportedOperationException(); + if (currentIndex < array.length + && inner.tryAdvance(a -> action.accept(new Pair<>(a, array[(int) currentIndex])))) { + currentIndex += 1; + return true; + } else { + return false; + } } @Override public void forEachRemaining(Consumer> action) { - // TODO - throw new UnsupportedOperationException(); + inner.forEachRemaining(a -> { + if (currentIndex < array.length) { + action.accept(new Pair<>(a, array[(int) currentIndex])); + currentIndex += 1; + } + }); } @Override public Spliterator> trySplit() { - // TODO - throw new UnsupportedOperationException(); + Spliterator aSpliterator = inner.trySplit(); + Spliterator> newSpliterator = new ZipWithArraySpliterator<>(aSpliterator, array, currentIndex); + currentIndex += newSpliterator.estimateSize(); + return newSpliterator; } @Override public long estimateSize() { - // TODO - throw new UnsupportedOperationException(); + return Math.min(inner.estimateSize(), (array.length - currentIndex)); } } diff --git a/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java b/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java new file mode 100644 index 0000000..fdd0043 --- /dev/null +++ b/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java @@ -0,0 +1,68 @@ +package spliterators.part3.exercise; + +import org.hamcrest.core.Is; +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +import static org.junit.Assert.assertThat; + +public class ZipWithArraySpliteratorTest { + + + private String[] generateStringArrayWithSize(int size) { + String[] scope = {"Random 1", "Random 2", "Random 3", "Random 4", "Random 5", "Random 6"}; + Random random = new Random(); + String[] result = new String[size]; + for (int i = 0; i < size; i++) { + result[i] = scope[random.nextInt(scope.length)]; + } + return result; + } + + private List> combaineToPairs(String[] array1, String[] array2) { + List> list = new ArrayList<>(); + int minLength = Math.min(array1.length, array2.length); + for (int i = 0; i < minLength; i++) { + list.add(new Pair<>(array1[i], array2[i])); + } + return list; + } + + @Test + public void comparePaired() { + final String[] englishLevelString = {"Elementary", + "Pre-Intermediate", + "Intermediate", + "Pro-Intermediate", + "Advanced", + "Pro-Advanced", + "Expert", + "Pro-Expert"}; + final String[] englishLevelNum = {"A1", "A2", "B1", "B2", "C1", "C2"/*,"D1", "D2", "E1", "E2"*/}; + Spliterator spliterator = Arrays.spliterator(englishLevelString); + StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator, englishLevelNum), true) + .forEach(s -> System.out.println(s.getA() + " " + s.getB())); + + } + + @Test + public void test1() { + String[] largeArray = generateStringArrayWithSize(10); + String[] smallArray = generateStringArrayWithSize(5); + List> expected = combaineToPairs(largeArray, smallArray); + + Spliterator spliterator1 = Arrays.spliterator(largeArray); + List> result1 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator1, smallArray), true) + .collect(Collectors.toList()); + assertThat(result1, Is.is(expected)); + + + Spliterator spliterator2 = Arrays.spliterator(smallArray); + List> result2 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator2, smallArray), true) + .collect(Collectors.toList()); + assertThat(result2, Is.is(expected)); + } +} \ No newline at end of file From 524e3fff01e2c7bbe0c97b8292648b332c54e9af Mon Sep 17 00:00:00 2001 From: Vasilii Bobkov Date: Mon, 24 Jul 2017 15:23:00 +0300 Subject: [PATCH 2/2] Part 3 test fixed --- .../part3/exercise/ZipWithArraySpliteratorTest.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java b/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java index fdd0043..9df4a64 100644 --- a/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java +++ b/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java @@ -22,7 +22,7 @@ private String[] generateStringArrayWithSize(int size) { return result; } - private List> combaineToPairs(String[] array1, String[] array2) { + private List> combineToPairs(String[] array1, String[] array2) { List> list = new ArrayList<>(); int minLength = Math.min(array1.length, array2.length); for (int i = 0; i < minLength; i++) { @@ -52,17 +52,18 @@ public void comparePaired() { public void test1() { String[] largeArray = generateStringArrayWithSize(10); String[] smallArray = generateStringArrayWithSize(5); - List> expected = combaineToPairs(largeArray, smallArray); + List> expected1 = combineToPairs(largeArray, smallArray); + List> expected2 = combineToPairs(smallArray, largeArray); Spliterator spliterator1 = Arrays.spliterator(largeArray); List> result1 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator1, smallArray), true) .collect(Collectors.toList()); - assertThat(result1, Is.is(expected)); + assertThat(result1, Is.is(expected1)); Spliterator spliterator2 = Arrays.spliterator(smallArray); - List> result2 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator2, smallArray), true) + List> result2 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator2, largeArray), true) .collect(Collectors.toList()); - assertThat(result2, Is.is(expected)); + assertThat(result2, Is.is(expected2)); } } \ No newline at end of file