Reimplement unfold

This commit is contained in:
Grzegorz Piwowarek 2018-09-23 07:26:49 +02:00
parent 77b21b0a9e
commit 2a2d4222bb
1 changed files with 38 additions and 36 deletions

View File

@ -38,17 +38,18 @@ public class ProtonpackUnitTest {
@Test
public void givenMultipleStream_whenZipped_thenZipped() {
String[] clubs = { "Juventus", "Barcelona", "Liverpool", "PSG" };
String[] players = { "Ronaldo", "Messi", "Salah" };
Set<String> 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<String> 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<String> 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
@ -56,7 +57,7 @@ public class ProtonpackUnitTest {
Stream<String> streamOfClubs = Stream.of("Juventus", "Barcelona", "Liverpool");
Set<Indexed<String>> 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
@ -66,9 +67,10 @@ public class ProtonpackUnitTest {
Stream<String> streamOfLeagues = Stream.of("Serie A", "La Liga", "Premier League");
Set<String> 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");
}
@ -78,7 +80,7 @@ public class ProtonpackUnitTest {
Stream<String> streamOfPlayers = Stream.of("Ronaldo", "Messi");
List<List<String>> 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");
@ -102,54 +104,56 @@ public class ProtonpackUnitTest {
@Test
public void whenSkippedUntil_thenSkippedUntil() {
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5).collect(Collectors.toList());
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<Integer> skippedUntilGreaterThan5 = StreamUtils.skipUntil(stream(numbers), i -> i > 5)
.collect(Collectors.toList());
assertThat(skippedUntilGreaterThan5).containsExactly(6, 7, 8, 9, 10);
List<Integer> 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<Integer> skippedWhileLessThanEquals5 = StreamUtils.skipWhile(stream(numbers), i -> i <= 5)
.collect(Collectors.toList());
.collect(Collectors.toList());
assertThat(skippedWhileLessThanEquals5).containsExactly(6, 7, 8, 9, 10);
List<Integer> skippedWhileGreaterThan5 = StreamUtils.skipWhile(stream(numbers), i -> i > 5).collect(Collectors.toList());
List<Integer> 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<Integer, Optional<Integer>> fibonacciGenerator = (i) -> (i < 10) ?
Optional.of(i + lastValue.getAndSet(i)) :
Optional.empty();
Stream<Integer> unfolded = StreamUtils.unfold(2, i -> (i < 100) ? Optional.of(i * i) : Optional.empty());
List<Integer> 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<List<Integer>> 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<List<Integer>> 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<List<Integer>> windowedWithSkip2 = StreamUtils.windowed(stream(numbers), 3, 2).collect(Collectors.toList());
List<List<Integer>> 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<List<Integer>> 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<List<Integer>> 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<List<Integer>> aggregatedFixSize = StreamUtils.aggregate(stream(numbers), 5).collect(Collectors.toList());
@ -158,20 +162,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<List<Integer>> 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<List<Integer>> 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<List<Integer>> 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,7 +191,6 @@ public class ProtonpackUnitTest {
Stream<Integer> singleElement = Stream.of(1);
Optional<Integer> unique = singleElement.collect(CollectorUtils.unique());
assertThat(unique.get()).isEqualTo(1);
}
@Test
@ -197,5 +200,4 @@ public class ProtonpackUnitTest {
multipleElement.collect(CollectorUtils.unique());
});
}
}