Skip to content

Commit 2a2d422

Browse files
committed
Reimplement unfold
1 parent 77b21b0 commit 2a2d422

1 file changed

Lines changed: 38 additions & 36 deletions

File tree

java-streams/src/test/java/com/baeldung/protonpack/ProtonpackUnitTest.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@ public void whenTakeUntil_thenTakenUntil() {
3838

3939
@Test
4040
public void givenMultipleStream_whenZipped_thenZipped() {
41-
String[] clubs = { "Juventus", "Barcelona", "Liverpool", "PSG" };
42-
String[] players = { "Ronaldo", "Messi", "Salah" };
43-
Set<String> zippedFrom2Sources = StreamUtils.zip(stream(clubs), stream(players), (club, player) -> club + " " + player)
44-
.collect(Collectors.toSet());
41+
String[] clubs = {"Juventus", "Barcelona", "Liverpool", "PSG"};
42+
String[] players = {"Ronaldo", "Messi", "Salah"};
43+
Set<String> zippedFrom2Sources = StreamUtils
44+
.zip(stream(clubs), stream(players), (club, player) -> club + " " + player)
45+
.collect(Collectors.toSet());
4546
assertThat(zippedFrom2Sources).contains("Juventus Ronaldo", "Barcelona Messi", "Liverpool Salah");
4647

47-
String[] leagues = { "Serie A", "La Liga", "Premier League" };
48+
String[] leagues = {"Serie A", "La Liga", "Premier League"};
4849
Set<String> zippedFrom3Sources = StreamUtils.zip(stream(clubs), stream(players), stream(leagues),
49-
(club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet());
50+
(club, player, league) -> club + " " + player + " " + league).collect(Collectors.toSet());
5051
assertThat(zippedFrom3Sources).contains("Juventus Ronaldo Serie A", "Barcelona Messi La Liga",
51-
"Liverpool Salah Premier League");
52+
"Liverpool Salah Premier League");
5253
}
5354

5455
@Test
5556
public void whenZippedWithIndex_thenZippedWithIndex() {
5657
Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool");
5758
Set<Indexed<String>> zipsWithIndex = StreamUtils.zipWithIndex(streamOfClubs).collect(Collectors.toSet());
5859
assertThat(zipsWithIndex).contains(Indexed.index(0, "Juventus"), Indexed.index(1, "Barcelona"),
59-
Indexed.index(2, "Liverpool"));
60+
Indexed.index(2, "Liverpool"));
6061
}
6162

6263
@Test
@@ -66,9 +67,10 @@ public void givenMultipleStream_whenMerged_thenMerged() {
6667
Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League");
6768

6869
Set<String> merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs,
69-
streamOfPlayers, streamOfLeagues).collect(Collectors.toSet());
70+
streamOfPlayers, streamOfLeagues).collect(Collectors.toSet());
7071

71-
assertThat(merged).contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League",
72+
assertThat(merged)
73+
.contains(" Juventus Ronaldo Serie A", " Barcelona Messi La Liga", " Liverpool Salah Premier League",
7274
" PSG");
7375
}
7476

@@ -78,7 +80,7 @@ public void givenMultipleStream_whenMergedToList_thenMergedToList() {
7880
Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");
7981

8082
List<List<String>> mergedListOfList = StreamUtils.mergeToList(streamOfClubs, streamOfPlayers)
81-
.collect(Collectors.toList());
83+
.collect(Collectors.toList());
8284
assertThat(mergedListOfList.get(0)).isInstanceOf(List.class);
8385
assertThat(mergedListOfList.get(0)).containsExactly("Juventus", "Ronaldo");
8486
assertThat(mergedListOfList.get(1)).containsExactly("Barcelona", "Messi");
@@ -102,54 +104,56 @@ public void givenMultipleStream_whenInterleaved_thenInterleaved() {
102104

103105
@Test
104106
public void whenSkippedUntil_thenSkippedUntil() {
105-
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
106-
List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5).collect(Collectors.toList());
107+
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
108+
List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5)
109+
.collect(Collectors.toList());
107110
assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10);
108111

109112
List<Integer> skippedUntilLessThanEquals5 = StreamUtils.skipUntil(stream(numbers), i -> i <= 5)
110-
.collect(Collectors.toList());
113+
.collect(Collectors.toList());
111114
assertThat(skippedUntilLessThanEquals5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
112115
}
113116

114117
@Test
115118
public void whenSkippedWhile_thenSkippedWhile() {
116-
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
119+
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
117120
List<Integer> skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5)
118-
.collect(Collectors.toList());
121+
.collect(Collectors.toList());
119122
assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10);
120123

121-
List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList());
124+
List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5)
125+
.collect(Collectors.toList());
122126
assertThat(skippedWhileGreaterThan5).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
123127
}
124128

125129
@Test
126130
public void givenFibonacciGenerator_whenUnfolded_thenUnfolded() {
127-
AtomicInteger lastValue = new AtomicInteger(0);
128-
Function<Integer, Optional<Integer>> fibonacciGenerator = (i) -> (i < 10) ?
129-
Optional.of(i + lastValue.getAndSet(i)) :
130-
Optional.empty();
131+
Stream<Integer> unfolded = StreamUtils.unfold(2, i -> (i < 100) ? Optional.of(i * i) : Optional.empty());
131132

132-
List<Integer> fib = StreamUtils.unfold(1, fibonacciGenerator).collect(Collectors.toList());
133-
assertThat(fib).containsExactly(1, 1, 2, 3, 5, 8, 13);
133+
assertThat(unfolded.collect(Collectors.toList())).containsExactly(2, 4, 16, 256);
134134
}
135135

136136
@Test
137137
public void whenWindowed_thenWindowed() {
138-
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
138+
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7};
139139

140-
List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1).collect(Collectors.toList());
141-
assertThat(windowedWithSkip1).containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6),
140+
List<List<Integer>> windowedWithSkip1 = StreamUtils.windowed(stream(numbers), 3, 1)
141+
.collect(Collectors.toList());
142+
assertThat(windowedWithSkip1)
143+
.containsExactly(asList(1, 2, 3), asList(2, 3, 4), asList(3, 4, 5), asList(4, 5, 6),
142144
asList(5, 6, 7));
143145

144-
List<List<Integer>> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList());
146+
List<List<Integer>> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2)
147+
.collect(Collectors.toList());
145148
assertThat(windowedWithSkip2).containsExactly(asList(1, 2, 3), asList(3, 4, 5), asList(5, 6, 7));
146149
}
147150

148151
@Test
149152
public void whenAggregated_thenAggregated() {
150-
Integer[] numbers = { 1, 2, 2, 3, 4, 4, 4, 5 };
151-
List<List<Integer>> aggregated = StreamUtils.aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0)
152-
.collect(Collectors.toList());
153+
Integer[] numbers = {1, 2, 2, 3, 4, 4, 4, 5};
154+
List<List<Integer>> aggregated = StreamUtils
155+
.aggregate(stream(numbers), (int1, int2) -> int1.compareTo(int2) == 0)
156+
.collect(Collectors.toList());
153157
assertThat(aggregated).containsExactly(asList(1), asList(2, 2), asList(3), asList(4, 4, 4), asList(5));
154158

155159
List<List<Integer>> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList());
@@ -158,20 +162,20 @@ public void whenAggregated_thenAggregated() {
158162

159163
@Test
160164
public void whenGroupedRun_thenGroupedRun() {
161-
Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 };
165+
Integer[] numbers = {1, 1, 2, 3, 4, 4, 5};
162166
List<List<Integer>> grouped = StreamUtils.groupRuns(stream(numbers)).collect(Collectors.toList());
163167
assertThat(grouped).containsExactly(asList(1, 1), asList(2), asList(3), asList(4, 4), asList(5));
164168

165-
Integer[] numbers2 = { 1, 2, 3, 1 };
169+
Integer[] numbers2 = {1, 2, 3, 1};
166170
List<List<Integer>> grouped2 = StreamUtils.groupRuns(stream(numbers2)).collect(Collectors.toList());
167171
assertThat(grouped2).containsExactly(asList(1), asList(2), asList(3), asList(1));
168172
}
169173

170174
@Test
171175
public void whenAggregatedOnListCondition_thenAggregatedOnListCondition() {
172-
Integer[] numbers = { 1, 1, 2, 3, 4, 4, 5 };
176+
Integer[] numbers = {1, 1, 2, 3, 4, 4, 5};
173177
Stream<List<Integer>> aggregated = StreamUtils.aggregateOnListCondition(stream(numbers),
174-
(currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5);
178+
(currentList, nextInt) -> currentList.stream().mapToInt(Integer::intValue).sum() + nextInt <= 5);
175179
assertThat(aggregated).containsExactly(asList(1, 1, 2), asList(3), asList(4), asList(4), asList(5));
176180
}
177181

@@ -187,7 +191,6 @@ public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned() {
187191
Stream<Integer> singleElement = Stream.of(1);
188192
Optional<Integer> unique = singleElement.collect(CollectorUtils.unique());
189193
assertThat(unique.get()).isEqualTo(1);
190-
191194
}
192195

193196
@Test
@@ -197,5 +200,4 @@ public void givenStreamOfMultipleElem_whenUniqueCollector_thenExceptionThrown()
197200
multipleElement.collect(CollectorUtils.unique());
198201
});
199202
}
200-
201203
}

0 commit comments

Comments
 (0)