From 77b21b0a9e9acd6025ecbe2de969c9a523d856dc Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 23 Sep 2018 06:53:46 +0200 Subject: [PATCH] refactor givenMultipleStream_whenInterleaved_thenInterleaved --- .../protonpack/ProtonpackUnitTest.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java b/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java index 1b64c8924a..6ec2e1b837 100644 --- a/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java +++ b/java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java @@ -4,10 +4,9 @@ import com.codepoetics.protonpack.Indexed; import com.codepoetics.protonpack.StreamUtils; import com.codepoetics.protonpack.collectors.CollectorUtils; import com.codepoetics.protonpack.collectors.NonUniqueValueException; -import com.codepoetics.protonpack.selectors.Selector; +import com.codepoetics.protonpack.selectors.Selectors; import org.junit.Test; -import java.util.Arrays; import java.util.List; import java.util.Optional; import java.util.Set; @@ -92,20 +91,13 @@ public class ProtonpackUnitTest { Stream streamOfPlayers = Stream.of("Ronaldo", "Messi"); Stream streamOfLeagues = Stream.of("Serie A", "La Liga"); - AtomicInteger counter = new AtomicInteger(0); - Selector roundRobinSelector = (o) -> { - Object[] vals = (Object[]) o; - while (counter.get() >= vals.length || vals[counter.get()] == null) { - if (counter.incrementAndGet() >= vals.length) - counter.set(0); - } - return counter.getAndIncrement(); - }; - Stream interleavedStream = StreamUtils.interleave(roundRobinSelector, streamOfClubs, streamOfPlayers, - streamOfLeagues); - List interleavedList = interleavedStream.collect(Collectors.toList()); - assertThat(interleavedList).containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", - "Liverpool"); + List interleavedList = StreamUtils + .interleave(Selectors.roundRobin(), streamOfClubs, streamOfPlayers, streamOfLeagues) + .collect(Collectors.toList()); + + assertThat(interleavedList) + .hasSize(7) + .containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", "Liverpool"); } @Test