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

part 3 is done #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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,63 @@ public class ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSplitera

private final Spliterator<A> inner;
private final B[] array;
private int index;

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

public ZipWithArraySpliterator(Spliterator<A> inner, B[] array, int index) {
super(Math.min(inner.estimateSize(), array.length - index), inner.characteristics());
this.inner = inner;
this.array = array;
this.index = index;
}

@Override
public int characteristics() {
// TODO
throw new UnsupportedOperationException();
return inner.characteristics() & ~Spliterator.SORTED;
}

@Override
public boolean tryAdvance(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
return index < array.length && inner.tryAdvance(a -> {
Pair<A, B> pair = new Pair<>(a, array[index]);
index += 1;
action.accept(pair);
});
}

@Override
public void forEachRemaining(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
if (inner.hasCharacteristics(SIZED) && inner.estimateSize() <= array.length - index) {
inner.forEachRemaining(a -> {
Pair<A, B> pair = new Pair<>(a, array[index]);
index += 1;
action.accept(pair);
});
} else {
super.forEachRemaining(action);
}
}

@Override
public Spliterator<Pair<A, B>> trySplit() {
// TODO
throw new UnsupportedOperationException();
if (inner.hasCharacteristics(SUBSIZED)) {
Spliterator<A> split = inner.trySplit();
if (split == null) {
return null;
}
final ZipWithArraySpliterator<A, B> spliterator = new ZipWithArraySpliterator<>(split, array, index);
index = Math.min((int) (index + split.estimateSize()), array.length);
return spliterator;
} else {
return super.trySplit();
}
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return Math.min(inner.estimateSize(), array.length - index);
}
}