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 done #61

Open
wants to merge 1 commit 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
@@ -1,48 +1,86 @@
package spliterators.part3.exercise;

import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;

public class ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSpliterator<Pair<A, B>> {


private final Spliterator<A> inner;
private int currentIndex;

private final B[] array;
private int arrayStartIndex;
private int arrayEndIndex;

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

}

private ZipWithArraySpliterator(int currentIndex, Spliterator<A> inner,
int arrayStartIndex, int arrayEndIndex, B[] array) {
super(inner.estimateSize(), ORDERED | SIZED | SUBSIZED | NONNULL | CONCURRENT);
this.inner = inner;
this.array = array;
this.currentIndex = currentIndex;
this.arrayStartIndex = arrayStartIndex;
this.arrayEndIndex = arrayEndIndex;
}

@Override
public int characteristics() {
// TODO
throw new UnsupportedOperationException();
return ORDERED | SIZED | SUBSIZED | NONNULL | CONCURRENT;
}

@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[currentIndex])))) {
currentIndex++;
return true;
}
return false;
}

@Override
public void forEachRemaining(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
while (currentIndex < array.length &&
inner.tryAdvance(a -> action.accept(new Pair<>(a, array[currentIndex])))) {
currentIndex++;
}
}

@Override
public Spliterator<Pair<A, B>> trySplit() {
// TODO
throw new UnsupportedOperationException();
if (inner.hasCharacteristics(SUBSIZED)) {
return Optional.ofNullable(inner.trySplit())
.map(newInner -> {
int newCurrentIndex = currentIndex;
currentIndex += (int) newInner.estimateSize();

arrayStartIndex = newCurrentIndex < array.length ? newCurrentIndex : array.length - 1;
arrayEndIndex = currentIndex < array.length ? currentIndex : array.length - 1;

Spliterator<Pair<A, B>> spliterator = new ZipWithArraySpliterator(newCurrentIndex, newInner,
arrayStartIndex, arrayEndIndex, array);

arrayStartIndex = currentIndex < array.length ? currentIndex : array.length - 1;
long newSizeOfSpltr = spliterator.estimateSize();
arrayEndIndex = newSizeOfSpltr < array.length ? (int) newSizeOfSpltr : array.length - 1;

return spliterator;
})
.orElse(null);
} else {
return null;
}
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return currentIndex + inner.estimateSize();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package spliterators.part3.exercise;

import org.junit.Test;

import java.util.*;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

public class ZipWithArraySpliteratorTest {

private Integer[] array = {1, 2, 3};
private Spliterator<Integer> arraySpltr = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6}).spliterator();
private ZipWithArraySpliterator<Integer, Integer> zipSpltr = new ZipWithArraySpliterator<>(arraySpltr, array);

@Test
public void testEstimatingSizeForFirstCreating() {
int expected = 6;
int actual = (int) zipSpltr.estimateSize();
assertThat(actual, is(expected));
}

@Test
public void testFirstlySplitting() {
Spliterator<Pair<Integer, Integer>> firstSpl = zipSpltr.trySplit();
Spliterator<Pair<Integer, Integer>> secondSpl = zipSpltr;

assertThat((int) firstSpl.estimateSize(), is(3));
assertThat((int) secondSpl.estimateSize(), is(6));

List<Pair<Integer, Integer>> expectedFirstListOfSpl = Arrays.asList(
new Pair<>(1, 1),
new Pair<>(2, 2),
new Pair<>(3, 3));
List<Pair<Integer, Integer>> actualFirstListOfSpl = new ArrayList<>();
while (firstSpl.tryAdvance(actualFirstListOfSpl::add)) ;
assertEquals(expectedFirstListOfSpl, actualFirstListOfSpl);

List<Pair<Integer, Integer>> expectedSecondListOfSpl = Collections.emptyList();
List<Pair<Integer, Integer>> actualSecondListOfSpl = new ArrayList<>();
while (secondSpl.tryAdvance(actualSecondListOfSpl::add)) ;
assertEquals(expectedSecondListOfSpl, actualSecondListOfSpl);
}
}