Skip to content

part3 (Kozyrev Egor) #42

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

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,80 @@
package spliterators.part3.exercise;

import java.util.Comparator;
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 final B[] array;
private int currentIndex;
private int endIndex;

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

public ZipWithArraySpliterator(Spliterator<A> inner, B[] array, int startIndex, int endIndex) {
super(array.length, inner.characteristics());
this.inner = inner;
this.array = array;
this.currentIndex = startIndex;
this.endIndex = endIndex;
}

@Override
public int characteristics() {
// TODO
throw new UnsupportedOperationException();
return inner.characteristics()
| NONNULL
| SIZED;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect.
Array may be bigger than the unsized inner.

}

@Override
public boolean tryAdvance(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
return (currentIndex < endIndex

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use if to simplify this expression.

&& inner.tryAdvance(a -> action.accept(new Pair<A, B>(a, array[currentIndex++]))));
}

@Override
public void forEachRemaining(Consumer<? super Pair<A, B>> action) {
// TODO
throw new UnsupportedOperationException();
while (currentIndex < endIndex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inefficient. You can optimize this in case inner.hasCharacteristics(SIZED).

if (!inner.tryAdvance(a -> action.accept(new Pair<A, B>(a, array[currentIndex++])))) {
return;
}
}
}

@Override
public Spliterator<Pair<A, B>> trySplit() {
// TODO
throw new UnsupportedOperationException();
Spliterator<A> splitInner = this.inner.trySplit();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect. Add test with not SIZED inner spliterator and parallel().

if (splitInner == null || array.length - currentIndex <= 1) {
return null;
}

if (inner.hasCharacteristics(SUBSIZED)) {
int newStart = currentIndex;
currentIndex += splitInner.estimateSize();
int newEnd = currentIndex < array.length ? currentIndex : array.length;

return new ZipWithArraySpliterator<>(splitInner, array, newStart, newEnd);
} else {
return super.trySplit();
}
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return endIndex - currentIndex;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect.

}

@Override
public Comparator<Pair<A, B>> getComparator() {
if (inner.hasCharacteristics(SORTED)) {
return (p1, p2) -> inner.getComparator().compare(p1.getA(), p2.getA());
} else {
throw new IllegalStateException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package spliterators.part3.exercise;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.StreamSupport;

import static org.junit.Assert.*;


public class ZipWithArraySpliteratorTest {

private List<Integer> listWithLessSize = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5));
private List<Integer> listWithSameSize = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
private List<Integer> listWithMoreSize = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14));

private Integer[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

@Test
public void testCount() {
final long expectedLess = listWithLessSize.size();
final long actualLess = StreamSupport
.stream(new ZipWithArraySpliterator<>(listWithLessSize.spliterator(), array), true)
.count();
assertEquals(expectedLess, actualLess);

final long expectedSame = listWithSameSize.size();
final long actualSame = StreamSupport
.stream(new ZipWithArraySpliterator<>(listWithSameSize.spliterator(), array), true)
.count();
assertEquals(expectedSame, actualSame);

final long expectedMore = array.length;
final long actualMore = StreamSupport
.stream(new ZipWithArraySpliterator<>(listWithMoreSize.spliterator(), array), true)
.count();
assertEquals(expectedMore, actualMore);
}

@Test
public void testSkip() {
long actual = StreamSupport
.stream(new ZipWithArraySpliterator<>(listWithSameSize.spliterator(), array), true)
.skip(2)
.mapToInt(Pair::getA)
.sum();
assertEquals(44, actual);
}

@Test
public void testLimit() {
long actual = StreamSupport
.stream(new ZipWithArraySpliterator<>(listWithSameSize.spliterator(), array), true)
.limit(4)
.mapToInt(Pair::getA)
.sum();
assertEquals(6, actual);
}
}