Skip to content

Commit cc72d3c

Browse files
feat(gatherer): added built in gatherer examples
1 parent b8485cb commit cc72d3c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package de.claudioaltamura.java23.streamgatherer;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
import java.util.stream.Gatherers;
7+
import java.util.stream.Stream;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
class BuiltInGathererTest {
12+
13+
@Test
14+
void fold() {
15+
var result = Stream.of(1, 2, 4, 5).gather(Gatherers.fold(()->0, (a, b) -> a + b)).toList();
16+
assertThat(result.getFirst()).isEqualTo(12);
17+
}
18+
19+
@Test
20+
void mapConcurrent() {
21+
var result = Stream.of(1, 2, 4, 5).gather(Gatherers.mapConcurrent(4, a -> a * 2)).toList();
22+
assertThat(result).containsExactly(2, 4, 8, 10);
23+
}
24+
25+
@Test
26+
void windowFixed() {
27+
var result = Stream.of(1, 2, 4, 5).gather(Gatherers.windowFixed(2)).toArray();
28+
assertThat(result).containsExactly(List.of(1, 2), List.of(4, 5));
29+
}
30+
31+
@Test
32+
void windowSliding() {
33+
var result = Stream.of(1, 2, 3, 4, 5).gather(Gatherers.windowSliding(2)).toArray();
34+
assertThat(result).containsExactly(List.of(1, 2), List.of(2, 3), List.of(3, 4), List.of(4, 5));
35+
}
36+
}

0 commit comments

Comments
 (0)