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

Second_homework (Aleksei Litkovetc) #46

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
6 changes: 3 additions & 3 deletions src/main/java/spliterators/part2/example/IndexedPair.java
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 IndexedPair<T> {
private final int index;
private final long index;
private final T value;

public IndexedPair(int index, T value) {
public IndexedPair(long index, T 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
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
Expand Up @@ -3,54 +3,68 @@
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
import java.util.Comparator;

public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> {


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;

Choose a reason for hiding this comment

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

Only in case res == true

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
Expand Up @@ -30,7 +30,7 @@ public void comparePaired() {
final List<IndexedPair<String>> result1 =
Stream.iterate(
new IndexedPair<>(0, randomArray[0]),
p -> new IndexedPair<>(p.getIndex() + 1, randomArray[p.getIndex() + 1]))
p -> new IndexedPair<>(p.getIndex() + 1, randomArray[(int)p.getIndex() + 1]))
.limit(randomArray.length)
.collect(toList());

Expand Down
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);
}
}