Reimplement unfold

This commit is contained in:
Grzegorz Piwowarek 2018-09-23 07:26:49 +02:00
parent 77b21b0a9e
commit 2a2d4222bb

View File

@ -40,7 +40,8 @@ public class ProtonpackUnitTest {
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)
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");
@ -68,7 +69,8 @@ public class ProtonpackUnitTest {
Set<String> merged = StreamUtils.merge(() -> "", (valOne, valTwo) -> valOne + " " + valTwo, streamOfClubs,
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");
}
@ -103,7 +105,8 @@ 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());
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)
@ -118,37 +121,38 @@ public class ProtonpackUnitTest {
.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};
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)
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));
@ -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());
});
}
}