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..371ac3a9bb 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; @@ -21,7 +20,6 @@ import static java.util.Arrays.stream; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; -@SuppressWarnings("unchecked") public class ProtonpackUnitTest { @Test public void whenTakeWhile_thenTakenWhile() { @@ -39,17 +37,18 @@ public class ProtonpackUnitTest { @Test public void givenMultipleStream_whenZipped_thenZipped() { - String[] clubs = { "Juventus", "Barcelona", "Liverpool", "PSG" }; - String[] players = { "Ronaldo", "Messi", "Salah" }; - Set zippedFrom2Sources = StreamUtils.zip(stream(clubs), stream(players), (club, player) -> club + " " + player) - .collect(Collectors.toSet()); + String[] clubs = {"Juventus", "Barcelona", "Liverpool", "PSG"}; + String[] players = {"Ronaldo", "Messi", "Salah"}; + Set zippedFrom2Sources = StreamUtils + .zip(stream(clubs), stream(players), (club, player) -> club + " " + player) + .collect(Collectors.toSet()); assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah"); - String[] leagues = { "Serie A", "La Liga", "Premier League" }; + String[] leagues = {"Serie A", "La Liga", "Premier League"}; Set zippedFrom3Sources = StreamUtils.zip(stream(clubs), stream(players), stream(leagues), - (club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet()); + (club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet()); assertThat(zippedFrom3Sources).contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga", - "Liverpool Salah Premier League"); + "Liverpool Salah Premier League"); } @Test @@ -57,7 +56,7 @@ public class ProtonpackUnitTest { Stream streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool"); Set> zipsWithIndex = StreamUtils.zipWithIndex(streamOfClubs).collect(Collectors.toSet()); assertThat(zipsWithIndex).contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"), - Indexed.index(2, "Liverpool")); + Indexed.index(2, "Liverpool")); } @Test @@ -67,9 +66,10 @@ public class ProtonpackUnitTest { Stream streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League"); Set merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs, - streamOfPlayers, streamOfLeagues).collect(Collectors.toSet()); + streamOfPlayers, streamOfLeagues).collect(Collectors.toSet()); - assertThat(merged).contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League", + assertThat(merged) + .contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League", " PSG"); } @@ -79,7 +79,7 @@ public class ProtonpackUnitTest { Stream streamOfPlayers = Stream.of("Ronaldo", "Messi"); List> mergedListOfList = StreamUtils.mergeToList(streamOfClubs, streamOfPlayers) - .collect(Collectors.toList()); + .collect(Collectors.toList()); assertThat(mergedListOfList.get(0)).isInstanceOf(List.class); assertThat(mergedListOfList.get(0)).containsExactly("Juventus", "Ronaldo"); assertThat(mergedListOfList.get(1)).containsExactly("Barcelona", "Messi"); @@ -92,72 +92,67 @@ 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 public void whenSkippedUntil_thenSkippedUntil() { - Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; - List skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5).collect(Collectors.toList()); + Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + List skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5) + .collect(Collectors.toList()); assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10); List skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5) - .collect(Collectors.toList()); + .collect(Collectors.toList()); assertThat(skippedUntilLessThanEquals5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } @Test public void whenSkippedWhile_thenSkippedWhile() { - Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; List skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5) - .collect(Collectors.toList()); + .collect(Collectors.toList()); assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10); - List skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList()); + List skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5) + .collect(Collectors.toList()); assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); } @Test public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() { - AtomicInteger lastValue = new AtomicInteger(0); - Function> fibonacciGenerator = (i) -> (i < 10) ? - Optional.of(i + lastValue.getAndSet(i)) : - Optional.empty(); + Stream unfolded = StreamUtils.unfold(2, i -> (i < 100) ? Optional.of(i * i) : Optional.empty()); - List fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList()); - assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13); + assertThat(unfolded.collect(Collectors.toList())).containsExactly(2, 4, 16, 256); } @Test public void whenWindowed_thenWindowed() { - Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7 }; + Integer[] numbers = {1, 2, 3, 4, 5, 6, 7}; - List> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1).collect(Collectors.toList()); - assertThat(windowedWithSkip1).containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6), + List> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1) + .collect(Collectors.toList()); + assertThat(windowedWithSkip1) + .containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6), asList(5, 6, 7)); - List> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList()); + List> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2) + .collect(Collectors.toList()); assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7)); } @Test public void whenAggregated_thenAggregated() { - Integer[] numbers = { 1, 2, 2, 3, 4, 4, 4, 5 }; - List> aggregated = StreamUtils.aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0) - .collect(Collectors.toList()); + Integer[] numbers = {1, 2, 2, 3, 4, 4, 4, 5}; + List> aggregated = StreamUtils + .aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0) + .collect(Collectors.toList()); assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5)); List> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList()); @@ -166,20 +161,20 @@ public class ProtonpackUnitTest { @Test public void whenGroupedRun_thenGroupedRun() { - Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 }; + Integer[] numbers = {1, 1, 2, 3, 4, 4, 5}; List> grouped = StreamUtils.groupRuns(stream(numbers)).collect(Collectors.toList()); assertThat(grouped).containsExactly(asList(1, 1), asList(2), asList(3), asList(4, 4), asList(5)); - Integer[] numbers2 = { 1, 2, 3, 1 }; + Integer[] numbers2 = {1, 2, 3, 1}; List> grouped2 = StreamUtils.groupRuns(stream(numbers2)).collect(Collectors.toList()); assertThat(grouped2).containsExactly(asList(1), asList(2), asList(3), asList(1)); } @Test public void whenAggregatedOnListCondition_thenAggregatedOnListCondition() { - Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 }; + Integer[] numbers = {1, 1, 2, 3, 4, 4, 5}; Stream> aggregated = StreamUtils.aggregateOnListCondition(stream(numbers), - (currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5); + (currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5); assertThat(aggregated).containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5)); } @@ -187,15 +182,14 @@ public class ProtonpackUnitTest { public void givenProjectionFunction_whenMaxedBy_thenMaxedBy() { Stream clubs = Stream.of("Juventus", "Barcelona", "PSG"); Optional longestName = clubs.collect(CollectorUtils.maxBy(String::length)); - assertThat(longestName.get()).isEqualTo("Barcelona"); + assertThat(longestName).contains("Barcelona"); } @Test public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() { Stream singleElement = Stream.of(1); Optional unique = singleElement.collect(CollectorUtils.unique()); - assertThat(unique.get()).isEqualTo(1); - + assertThat(unique).contains(1); } @Test @@ -205,5 +199,4 @@ public class ProtonpackUnitTest { multipleElement.collect(CollectorUtils.unique()); }); } - }