Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part3 (by Vasilii Bobkov) #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,55 @@ public class ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSplitera

private final Spliterator<A> inner;
private final B[] array;
private long currentIndex;

public ZipWithArraySpliterator(Spliterator<A> inner, B[] array) {
super(Long.MAX_VALUE, 0); // FIXME:
// TODO
throw new UnsupportedOperationException();
this(inner, array, 0);
}

private ZipWithArraySpliterator(Spliterator<A> 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<? super Pair<A, B>> 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<? super Pair<A, B>> 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<Pair<A, B>> trySplit() {
// TODO
throw new UnsupportedOperationException();
Spliterator<A> aSpliterator = inner.trySplit();
Spliterator<Pair<A, B>> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<Pair<String, String>> combineToPairs(String[] array1, String[] array2) {
List<Pair<String, String>> 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<String> 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<Pair<String, String>> expected1 = combineToPairs(largeArray, smallArray);
List<Pair<String, String>> expected2 = combineToPairs(smallArray, largeArray);

Spliterator<String> spliterator1 = Arrays.spliterator(largeArray);
List<Pair<String, String>> result1 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator1, smallArray), true)
.collect(Collectors.toList());
assertThat(result1, Is.is(expected1));


Spliterator<String> spliterator2 = Arrays.spliterator(smallArray);
List<Pair<String, String>> result2 = StreamSupport.stream(new ZipWithArraySpliterator<>(spliterator2, largeArray), true)
.collect(Collectors.toList());
assertThat(result2, Is.is(expected2));
}
}