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..9df4a64 --- /dev/null +++ b/src/test/java/spliterators/part3/exercise/ZipWithArraySpliteratorTest.java @@ -0,0 +1,69 @@ +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> combineToPairs(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> 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(expected1)); + + + Spliterator spliterator2 = Arrays.spliterator(smallArray); + List> result2 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator2, largeArray), true) + .collect(Collectors.toList()); + assertThat(result2, Is.is(expected2)); + } +} \ No newline at end of file