|
| 1 | +package com.baeldung.protonpack; |
| 2 | + |
| 3 | +import com.codepoetics.protonpack.Indexed; |
| 4 | +import com.codepoetics.protonpack.StreamUtils; |
| 5 | +import com.codepoetics.protonpack.collectors.CollectorUtils; |
| 6 | +import com.codepoetics.protonpack.collectors.NonUniqueValueException; |
| 7 | +import com.codepoetics.protonpack.selectors.Selector; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Optional; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.concurrent.atomic.AtomicInteger; |
| 15 | +import java.util.function.Function; |
| 16 | +import java.util.stream.Collectors; |
| 17 | +import java.util.stream.Stream; |
| 18 | + |
| 19 | +import static java.util.Arrays.asList; |
| 20 | +import static java.util.Arrays.stream; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
| 23 | + |
| 24 | +@SuppressWarnings("unchecked") |
| 25 | +public class ProtonpackUnitTest { |
| 26 | + @Test |
| 27 | + public void whenTakeWhile_thenTakenWhile() { |
| 28 | + Stream<Integer> streamOfInt = Stream.iterate(1, i -> i + 1); |
| 29 | + List<Integer> result = StreamUtils.takeWhile(streamOfInt, i -> i < 5).collect(Collectors.toList()); |
| 30 | + assertThat(result).contains(1, 2, 3, 4); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenTakeUntil_thenTakenUntil() { |
| 35 | + Stream<Integer> streamOfInt = Stream.iterate(1, i -> i + 1); |
| 36 | + List<Integer> result = StreamUtils.takeUntil(streamOfInt, i -> i > 50).collect(Collectors.toList()); |
| 37 | + assertThat(result).contains(10, 20, 30, 40); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void givenMultipleStream_whenZipped_thenZipped() { |
| 42 | + String[] clubs = { "Juventus", "Barcelona", "Liverpool", "PSG" }; |
| 43 | + String[] players = { "Ronaldo", "Messi", "Salah" }; |
| 44 | + Set<String> zippedFrom2Sources = StreamUtils.zip(stream(clubs), stream(players), (club, player) -> club + " " + player) |
| 45 | + .collect(Collectors.toSet()); |
| 46 | + assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah"); |
| 47 | + |
| 48 | + String[] leagues = { "Serie A", "La Liga", "Premier League" }; |
| 49 | + Set<String> zippedFrom3Sources = StreamUtils.zip(stream(clubs), stream(players), stream(leagues), |
| 50 | + (club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet()); |
| 51 | + assertThat(zippedFrom3Sources).contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga", |
| 52 | + "Liverpool Salah Premier League"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void whenZippedWithIndex_thenZippedWithIndex() { |
| 57 | + Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool"); |
| 58 | + Set<Indexed<String>> zipsWithIndex = StreamUtils.zipWithIndex(streamOfClubs).collect(Collectors.toSet()); |
| 59 | + assertThat(zipsWithIndex).contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"), |
| 60 | + Indexed.index(2, "Liverpool")); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void givenMultipleStream_whenMerged_thenMerged() { |
| 65 | + Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool", "PSG"); |
| 66 | + Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi", "Salah"); |
| 67 | + Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League"); |
| 68 | + |
| 69 | + Set<String> merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs, |
| 70 | + streamOfPlayers, streamOfLeagues).collect(Collectors.toSet()); |
| 71 | + |
| 72 | + assertThat(merged).contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League", |
| 73 | + " PSG"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void givenMultipleStream_whenMergedToList_thenMergedToList() { |
| 78 | + Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "PSG"); |
| 79 | + Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi"); |
| 80 | + |
| 81 | + List<List<String>> mergedListOfList = StreamUtils.mergeToList(streamOfClubs, streamOfPlayers) |
| 82 | + .collect(Collectors.toList()); |
| 83 | + assertThat(mergedListOfList.get(0)).isInstanceOf(List.class); |
| 84 | + assertThat(mergedListOfList.get(0)).containsExactly("Juventus", "Ronaldo"); |
| 85 | + assertThat(mergedListOfList.get(1)).containsExactly("Barcelona", "Messi"); |
| 86 | + assertThat(mergedListOfList.get(2)).containsExactly("PSG"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void givenMultipleStream_whenInterleaved_thenInterleaved() { |
| 91 | + Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool"); |
| 92 | + Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi"); |
| 93 | + Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga"); |
| 94 | + |
| 95 | + AtomicInteger counter = new AtomicInteger(0); |
| 96 | + Selector roundRobinSelector = (o) -> { |
| 97 | + Object[] vals = (Object[]) o; |
| 98 | + while (counter.get() >= vals.length || vals[counter.get()] == null) { |
| 99 | + if (counter.incrementAndGet() >= vals.length) |
| 100 | + counter.set(0); |
| 101 | + } |
| 102 | + return counter.getAndIncrement(); |
| 103 | + }; |
| 104 | + Stream<String> interleavedStream = StreamUtils.interleave(roundRobinSelector, streamOfClubs, streamOfPlayers, |
| 105 | + streamOfLeagues); |
| 106 | + List<String> interleavedList = interleavedStream.collect(Collectors.toList()); |
| 107 | + assertThat(interleavedList).containsExactly("Juventus", "Ronaldo", "Serie A", "Barcelona", "Messi", "La Liga", |
| 108 | + "Liverpool"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void whenSkippedUntil_thenSkippedUntil() { |
| 113 | + Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 114 | + List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5).collect(Collectors.toList()); |
| 115 | + assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10); |
| 116 | + |
| 117 | + List<Integer> skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5) |
| 118 | + .collect(Collectors.toList()); |
| 119 | + assertThat(skippedUntilLessThanEquals5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void whenSkippedWhile_thenSkippedWhile() { |
| 124 | + Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 125 | + List<Integer> skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5) |
| 126 | + .collect(Collectors.toList()); |
| 127 | + assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10); |
| 128 | + |
| 129 | + List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList()); |
| 130 | + assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() { |
| 135 | + AtomicInteger lastValue = new AtomicInteger(0); |
| 136 | + Function<Integer, Optional<Integer>> fibonacciGenerator = (i) -> (i < 10) ? |
| 137 | + Optional.of(i + lastValue.getAndSet(i)) : |
| 138 | + Optional.empty(); |
| 139 | + |
| 140 | + List<Integer> fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList()); |
| 141 | + assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void whenWindowed_thenWindowed() { |
| 146 | + Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7 }; |
| 147 | + |
| 148 | + List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1).collect(Collectors.toList()); |
| 149 | + assertThat(windowedWithSkip1).containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6), |
| 150 | + asList(5, 6, 7)); |
| 151 | + |
| 152 | + List<List<Integer>> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList()); |
| 153 | + assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7)); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + public void whenAggregated_thenAggregated() { |
| 158 | + Integer[] numbers = { 1, 2, 2, 3, 4, 4, 4, 5 }; |
| 159 | + List<List<Integer>> aggregated = StreamUtils.aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0) |
| 160 | + .collect(Collectors.toList()); |
| 161 | + assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5)); |
| 162 | + |
| 163 | + List<List<Integer>> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList()); |
| 164 | + assertThat(aggregatedFixSize).containsExactly(asList(1, 2, 2, 3, 4), asList(4, 4, 5)); |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void whenGroupedRun_thenGroupedRun() { |
| 169 | + Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 }; |
| 170 | + List<List<Integer>> grouped = StreamUtils.groupRuns(stream(numbers)).collect(Collectors.toList()); |
| 171 | + assertThat(grouped).containsExactly(asList(1, 1), asList(2), asList(3), asList(4, 4), asList(5)); |
| 172 | + |
| 173 | + Integer[] numbers2 = { 1, 2, 3, 1 }; |
| 174 | + List<List<Integer>> grouped2 = StreamUtils.groupRuns(stream(numbers2)).collect(Collectors.toList()); |
| 175 | + assertThat(grouped2).containsExactly(asList(1), asList(2), asList(3), asList(1)); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void whenAggregatedOnListCondition_thenAggregatedOnListCondition() { |
| 180 | + Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 }; |
| 181 | + Stream<List<Integer>> aggregated = StreamUtils.aggregateOnListCondition(stream(numbers), |
| 182 | + (currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5); |
| 183 | + assertThat(aggregated).containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5)); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void givenProjectionFunction_whenMaxedBy_thenMaxedBy() { |
| 188 | + Stream<String> clubs = Stream.of("Juventus", "Barcelona", "PSG"); |
| 189 | + Optional<String> longestName = clubs.collect(CollectorUtils.maxBy(String::length)); |
| 190 | + assertThat(longestName.get()).isEqualTo("Barcelona"); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() { |
| 195 | + Stream<Integer> singleElement = Stream.of(1); |
| 196 | + Optional<Integer> unique = singleElement.collect(CollectorUtils.unique()); |
| 197 | + assertThat(unique.get()).isEqualTo(1); |
| 198 | + |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + public void givenStreamOfMultipleElem_whenUniqueCollector_thenExceptionThrown() { |
| 203 | + Stream<Integer> multipleElement = Stream.of(1, 2, 3); |
| 204 | + assertThatExceptionOfType(NonUniqueValueException.class).isThrownBy(() -> { |
| 205 | + multipleElement.collect(CollectorUtils.unique()); |
| 206 | + }); |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments