refactor givenMultipleStream_whenInterleaved_thenInterleaved

This commit is contained in:
Grzegorz Piwowarek 2018-09-23 06:53:46 +02:00
parent 991d6937f1
commit 77b21b0a9e

View File

@ -4,10 +4,9 @@ import com.codepoetics.protonpack.Indexed;
import com.codepoetics.protonpack.StreamUtils; import com.codepoetics.protonpack.StreamUtils;
import com.codepoetics.protonpack.collectors.CollectorUtils; import com.codepoetics.protonpack.collectors.CollectorUtils;
import com.codepoetics.protonpack.collectors.NonUniqueValueException; import com.codepoetics.protonpack.collectors.NonUniqueValueException;
import com.codepoetics.protonpack.selectors.Selector; import com.codepoetics.protonpack.selectors.Selectors;
import org.junit.Test; import org.junit.Test;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -92,20 +91,13 @@ public class ProtonpackUnitTest {
Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi"); Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");
Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga"); Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga");
AtomicInteger counter = new AtomicInteger(0); List<String> interleavedList = StreamUtils
Selector roundRobinSelector = (o) -> { .interleave(Selectors.roundRobin(), streamOfClubs, streamOfPlayers, streamOfLeagues)
Object[] vals = (Object[]) o; .collect(Collectors.toList());
while (counter.get() >= vals.length || vals[counter.get()] == null) {
if (counter.incrementAndGet() >= vals.length) assertThat(interleavedList)
counter.set(0); .hasSize(7)
} .containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", "Liverpool");
return counter.getAndIncrement();
};
Stream<String> interleavedStream = StreamUtils.interleave(roundRobinSelector, streamOfClubs, streamOfPlayers,
streamOfLeagues);
List<String> interleavedList = interleavedStream.collect(Collectors.toList());
assertThat(interleavedList).containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga",
"Liverpool");
} }
@Test @Test