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

Third_homework (Aleksei Litkovetc) #47

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,44 +5,65 @@
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 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package spliterators.part3.exercise;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class ZipWithArraySpliteratorTest {
@Test
public void tryAdvanceTest() {
Stream<Integer> stream = Stream.of(1, 2, 3);
String[] array = generateArray(5);
ZipWithArraySpliterator<Integer, String> spliterator = new ZipWithArraySpliterator<>(stream.spliterator(), array);
spliterator.tryAdvance(p -> assertEquals(new Pair<>(1, array[0]), p));

assertEquals(2, spliterator.estimateSize());
}

@Test
public void trySplitTest() {
Stream<Integer> stream = Stream.of(1, 2, 3);
String[] array = {"d", "b", "z"};
List<Pair<Integer, String>> expected = Arrays.asList(new Pair<>(1, "d"), new Pair<>(2, "b"), new Pair<>(3, "z"));
ZipWithArraySpliterator<Integer, String> spliterator = new ZipWithArraySpliterator<>(stream.spliterator(), array);
long originSize = spliterator.estimateSize();
Spliterator<Pair<Integer, String>> spliterator1 = spliterator.trySplit();
long size = spliterator.estimateSize();
long size1 = spliterator1.estimateSize();
List<Pair<Integer, String>> list1 = StreamSupport.stream(spliterator1, false).collect(Collectors.toList());
List<Pair<Integer, String>> list2 = StreamSupport.stream(spliterator, false).collect(Collectors.toList());
list1.addAll(list2);

assertEquals(originSize, size + size1);
assertEquals(expected, list1);
}

@Test
public void emptyStreamTest(){
String[] array = {"dbz"};
Stream<Integer> empty = Stream.empty();
ZipWithArraySpliterator<Integer, String> spliterator = new ZipWithArraySpliterator<>(empty.spliterator(), array);

assertEquals(0, spliterator.estimateSize());
assertNull(spliterator.trySplit());
}

@Test
public void forEachRemainingTest() {
Stream<Integer> stream = Stream.of(1, 2);
String[] array = {"John", "Ace", "Danny", "Felix"};
ZipWithArraySpliterator<Integer, String> spliterator = new ZipWithArraySpliterator<>(stream.spliterator(), array);
List<Pair<Integer, String>> expected = Arrays.asList(new Pair<>(1, "John"), new Pair<>(2, "Ace"));
List<Pair<Integer, String>> result = new ArrayList<>();
spliterator.forEachRemaining(result::add);

assertEquals(expected, result);
assertFalse(spliterator.tryAdvance(System.out::print));
}

@Test
public void spliteratorStreamTest() {
final String[] array = {"1", "2", "3"};
Stream<Integer> stream = Stream.of(1, 2, 3);
Stream<Pair<Integer, String>> pairStream = StreamSupport.stream(
new ZipWithArraySpliterator<>(stream.spliterator(), array),
true);
int sum = pairStream
.mapToInt(value -> value.getA() + Integer.parseInt(value.getB()))
.sum();

assertEquals(12, sum);
}

private String[] generateArray(int size) {
Random rand = new Random();
final String[] result = new String[size];
for (int i = 0; i < size; i++) {
result[i] = String.valueOf(rand.nextInt());
}
return result;
}
}