Skip to content

Part2 (Kozyrev Egor) #41

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
Expand Up @@ -5,15 +5,15 @@
import org.apache.commons.lang3.builder.ToStringBuilder;

public class IndexedDoublePair {
private final int index;
private final long index;
private final double value;

public IndexedDoublePair(int index, double value) {
public IndexedDoublePair(long index, double value) {
this.index = index;
this.value = value;
}

public int getIndex() {
public long getIndex() {
return index;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package spliterators.part2.exercise;

import java.util.Comparator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
Expand All @@ -8,49 +9,62 @@ public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSplitera


private final OfDouble inner;
private int currentIndex;
private long currentIndex;

public ZipWithIndexDoubleSpliterator(OfDouble inner) {
this(0, inner);
}

private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) {
private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
currentIndex = firstIndex;
this.inner = inner;
}

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

@Override
public Comparator<? super IndexedDoublePair> getComparator() {
if (inner.hasCharacteristics(SORTED)) {
return Comparator.comparing(IndexedDoublePair::getValue);
} else {
throw new IllegalStateException();
}
}

@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
boolean res = inner.tryAdvance((Double e) -> action.accept(new IndexedDoublePair(currentIndex, e)));
currentIndex += 1;
return res;
}

@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
// TODO
throw new UnsupportedOperationException();
inner.forEachRemaining((Double e) -> {
action.accept(new IndexedDoublePair(currentIndex, e));
currentIndex += 1;
});
}

@Override
public Spliterator<IndexedDoublePair> trySplit() {
// TODO
// if (inner.hasCharacteristics(???)) {
// use inner.trySplit
// } else

return super.trySplit();
if (inner.hasCharacteristics(SUBSIZED)) {
OfDouble splitInner = this.inner.trySplit();
ZipWithIndexDoubleSpliterator spliterator = new ZipWithIndexDoubleSpliterator(currentIndex, splitInner);
currentIndex += splitInner.estimateSize();
return spliterator;
} else {
return super.trySplit();
}
}

@Override
public long estimateSize() {
// TODO
throw new UnsupportedOperationException();
return inner.estimateSize();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package spliterators.part2.exercise;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.junit.Assert.*;


public class ZipWithIndexDoubleSpliteratorTest {

final double[] array = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};

@Test
public void testCount() {
Spliterator.OfDouble spliterator = Arrays.spliterator(array);
final long expected = 6;

long actual = StreamSupport
.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.count();

assertEquals(expected, actual);
}

@Test
public void testToList() {
Spliterator.OfDouble spliterator = Arrays.spliterator(array);
final List<Double> expected = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0);

List<Double> actual = StreamSupport
.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.map(IndexedDoublePair::getValue)
.collect(Collectors.toList());

assertEquals(expected, actual);
}

@Test
public void testPaired() {
Spliterator.OfDouble spliterator = Arrays.spliterator(array);
final List<IndexedDoublePair> expected = Arrays.asList(
new IndexedDoublePair(0, 1.0),
new IndexedDoublePair(1, 2.0),
new IndexedDoublePair(2, 3.0),
new IndexedDoublePair(3, 4.0),
new IndexedDoublePair(4, 5.0),
new IndexedDoublePair(5, 6.0)
);

List<IndexedDoublePair> actual = StreamSupport
.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.collect(Collectors.toList());

assertEquals(expected, actual);
}

@Test
public void testSkip() {
Spliterator.OfDouble spliterator = Arrays.spliterator(array);

double actual = StreamSupport
.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.skip(3)
.mapToDouble(IndexedDoublePair::getValue)
.sum();

assertTrue(15.0 == actual);
}

@Test
public void testLimit() {
Spliterator.OfDouble spliterator = Arrays.spliterator(array);

double actual = StreamSupport
.stream(new ZipWithIndexDoubleSpliterator(spliterator), true)
.limit(3)
.mapToDouble(IndexedDoublePair::getValue)
.sum();

assertTrue(6.0 == actual);
}
}