44import com .codepoetics .protonpack .StreamUtils ;
55import com .codepoetics .protonpack .collectors .CollectorUtils ;
66import com .codepoetics .protonpack .collectors .NonUniqueValueException ;
7- import com .codepoetics .protonpack .selectors .Selector ;
7+ import com .codepoetics .protonpack .selectors .Selectors ;
88import org .junit .Test ;
99
10- import java .util .Arrays ;
1110import java .util .List ;
1211import java .util .Optional ;
1312import java .util .Set ;
2120import static org .assertj .core .api .Assertions .assertThat ;
2221import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2322
24- @ SuppressWarnings ("unchecked" )
2523public class ProtonpackUnitTest {
2624 @ Test
2725 public void whenTakeWhile_thenTakenWhile () {
@@ -39,25 +37,26 @@ public void whenTakeUntil_thenTakenUntil() {
3937
4038 @ Test
4139 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 ());
40+ String [] clubs = {"Juventus" , "Barcelona" , "Liverpool" , "PSG" };
41+ String [] players = {"Ronaldo" , "Messi" , "Salah" };
42+ Set <String > zippedFrom2Sources = StreamUtils
43+ .zip (stream (clubs ), stream (players ), (club , player ) -> club + " " + player )
44+ .collect (Collectors .toSet ());
4645 assertThat (zippedFrom2Sources ).contains ("Juventus Ronaldo" , "Barcelona Messi" , "Liverpool Salah" );
4746
48- String [] leagues = { "Serie A" , "La Liga" , "Premier League" };
47+ String [] leagues = {"Serie A" , "La Liga" , "Premier League" };
4948 Set <String > zippedFrom3Sources = StreamUtils .zip (stream (clubs ), stream (players ), stream (leagues ),
50- (club , player , league ) -> club + " " + player + " " + league ).collect (Collectors .toSet ());
49+ (club , player , league ) -> club + " " + player + " " + league ).collect (Collectors .toSet ());
5150 assertThat (zippedFrom3Sources ).contains ("Juventus Ronaldo Serie A" , "Barcelona Messi La Liga" ,
52- "Liverpool Salah Premier League" );
51+ "Liverpool Salah Premier League" );
5352 }
5453
5554 @ Test
5655 public void whenZippedWithIndex_thenZippedWithIndex () {
5756 Stream <String > streamOfClubs = Stream .of ("Juventus" , "Barcelona" , "Liverpool" );
5857 Set <Indexed <String >> zipsWithIndex = StreamUtils .zipWithIndex (streamOfClubs ).collect (Collectors .toSet ());
5958 assertThat (zipsWithIndex ).contains (Indexed .index (0 , "Juventus" ), Indexed .index (1 , "Barcelona" ),
60- Indexed .index (2 , "Liverpool" ));
59+ Indexed .index (2 , "Liverpool" ));
6160 }
6261
6362 @ Test
@@ -67,9 +66,10 @@ public void givenMultipleStream_whenMerged_thenMerged() {
6766 Stream <String > streamOfLeagues = Stream .of ("Serie A" , "La Liga" , "Premier League" );
6867
6968 Set <String > merged = StreamUtils .merge (() -> "" , (valOne , valTwo ) -> valOne + " " + valTwo , streamOfClubs ,
70- streamOfPlayers , streamOfLeagues ).collect (Collectors .toSet ());
69+ streamOfPlayers , streamOfLeagues ).collect (Collectors .toSet ());
7170
72- assertThat (merged ).contains (" Juventus Ronaldo Serie A" , " Barcelona Messi La Liga" , " Liverpool Salah Premier League" ,
71+ assertThat (merged )
72+ .contains (" Juventus Ronaldo Serie A" , " Barcelona Messi La Liga" , " Liverpool Salah Premier League" ,
7373 " PSG" );
7474 }
7575
@@ -79,7 +79,7 @@ public void givenMultipleStream_whenMergedToList_thenMergedToList() {
7979 Stream <String > streamOfPlayers = Stream .of ("Ronaldo" , "Messi" );
8080
8181 List <List <String >> mergedListOfList = StreamUtils .mergeToList (streamOfClubs , streamOfPlayers )
82- .collect (Collectors .toList ());
82+ .collect (Collectors .toList ());
8383 assertThat (mergedListOfList .get (0 )).isInstanceOf (List .class );
8484 assertThat (mergedListOfList .get (0 )).containsExactly ("Juventus" , "Ronaldo" );
8585 assertThat (mergedListOfList .get (1 )).containsExactly ("Barcelona" , "Messi" );
@@ -92,72 +92,67 @@ public void givenMultipleStream_whenInterleaved_thenInterleaved() {
9292 Stream <String > streamOfPlayers = Stream .of ("Ronaldo" , "Messi" );
9393 Stream <String > streamOfLeagues = Stream .of ("Serie A" , "La Liga" );
9494
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" );
95+ List <String > interleavedList = StreamUtils
96+ .interleave (Selectors .roundRobin (), streamOfClubs , streamOfPlayers , streamOfLeagues )
97+ .collect (Collectors .toList ());
98+
99+ assertThat (interleavedList )
100+ .hasSize (7 )
101+ .containsExactly ("Juventus" , "Ronaldo" , "Serie A" , "Barcelona" , "Messi" , "La Liga" , "Liverpool" );
109102 }
110103
111104 @ Test
112105 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 ());
106+ Integer [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
107+ List <Integer > skippedUntilGreaterThan5 = StreamUtils .skipUntil (stream (numbers ), i -> i > 5 )
108+ .collect (Collectors .toList ());
115109 assertThat (skippedUntilGreaterThan5 ).containsExactly (6 , 7 , 8 , 9 , 10 );
116110
117111 List <Integer > skippedUntilLessThanEquals5 = StreamUtils .skipUntil (stream (numbers ), i -> i <= 5 )
118- .collect (Collectors .toList ());
112+ .collect (Collectors .toList ());
119113 assertThat (skippedUntilLessThanEquals5 ).containsExactly (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 );
120114 }
121115
122116 @ Test
123117 public void whenSkippedWhile_thenSkippedWhile () {
124- Integer [] numbers = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
118+ Integer [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
125119 List <Integer > skippedWhileLessThanEquals5 = StreamUtils .skipWhile (stream (numbers ), i -> i <= 5 )
126- .collect (Collectors .toList ());
120+ .collect (Collectors .toList ());
127121 assertThat (skippedWhileLessThanEquals5 ).containsExactly (6 , 7 , 8 , 9 , 10 );
128122
129- List <Integer > skippedWhileGreaterThan5 = StreamUtils .skipWhile (stream (numbers ), i -> i > 5 ).collect (Collectors .toList ());
123+ List <Integer > skippedWhileGreaterThan5 = StreamUtils .skipWhile (stream (numbers ), i -> i > 5 )
124+ .collect (Collectors .toList ());
130125 assertThat (skippedWhileGreaterThan5 ).containsExactly (1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 );
131126 }
132127
133128 @ Test
134129 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 ();
130+ Stream <Integer > unfolded = StreamUtils .unfold (2 , i -> (i < 100 ) ? Optional .of (i * i ) : Optional .empty ());
139131
140- List <Integer > fib = StreamUtils .unfold (1 , fibonacciGenerator ).collect (Collectors .toList ());
141- assertThat (fib ).containsExactly (1 , 1 , 2 , 3 , 5 , 8 , 13 );
132+ assertThat (unfolded .collect (Collectors .toList ())).containsExactly (2 , 4 , 16 , 256 );
142133 }
143134
144135 @ Test
145136 public void whenWindowed_thenWindowed () {
146- Integer [] numbers = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
137+ Integer [] numbers = {1 , 2 , 3 , 4 , 5 , 6 , 7 };
147138
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 ),
139+ List <List <Integer >> windowedWithSkip1 = StreamUtils .windowed (stream (numbers ), 3 , 1 )
140+ .collect (Collectors .toList ());
141+ assertThat (windowedWithSkip1 )
142+ .containsExactly (asList (1 , 2 , 3 ), asList (2 , 3 , 4 ), asList (3 , 4 , 5 ), asList (4 , 5 , 6 ),
150143 asList (5 , 6 , 7 ));
151144
152- List <List <Integer >> windowedWithSkip2 = StreamUtils .windowed (stream (numbers ), 3 , 2 ).collect (Collectors .toList ());
145+ List <List <Integer >> windowedWithSkip2 = StreamUtils .windowed (stream (numbers ), 3 , 2 )
146+ .collect (Collectors .toList ());
153147 assertThat (windowedWithSkip2 ).containsExactly (asList (1 , 2 , 3 ), asList (3 , 4 , 5 ), asList (5 , 6 , 7 ));
154148 }
155149
156150 @ Test
157151 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 ());
152+ Integer [] numbers = {1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 };
153+ List <List <Integer >> aggregated = StreamUtils
154+ .aggregate (stream (numbers ), (int1 , int2 ) -> int1 .compareTo (int2 ) == 0 )
155+ .collect (Collectors .toList ());
161156 assertThat (aggregated ).containsExactly (asList (1 ), asList (2 , 2 ), asList (3 ), asList (4 , 4 , 4 ), asList (5 ));
162157
163158 List <List <Integer >> aggregatedFixSize = StreamUtils .aggregate (stream (numbers ), 5 ).collect (Collectors .toList ());
@@ -166,36 +161,35 @@ public void whenAggregated_thenAggregated() {
166161
167162 @ Test
168163 public void whenGroupedRun_thenGroupedRun () {
169- Integer [] numbers = { 1 , 1 , 2 , 3 , 4 , 4 , 5 };
164+ Integer [] numbers = {1 , 1 , 2 , 3 , 4 , 4 , 5 };
170165 List <List <Integer >> grouped = StreamUtils .groupRuns (stream (numbers )).collect (Collectors .toList ());
171166 assertThat (grouped ).containsExactly (asList (1 , 1 ), asList (2 ), asList (3 ), asList (4 , 4 ), asList (5 ));
172167
173- Integer [] numbers2 = { 1 , 2 , 3 , 1 };
168+ Integer [] numbers2 = {1 , 2 , 3 , 1 };
174169 List <List <Integer >> grouped2 = StreamUtils .groupRuns (stream (numbers2 )).collect (Collectors .toList ());
175170 assertThat (grouped2 ).containsExactly (asList (1 ), asList (2 ), asList (3 ), asList (1 ));
176171 }
177172
178173 @ Test
179174 public void whenAggregatedOnListCondition_thenAggregatedOnListCondition () {
180- Integer [] numbers = { 1 , 1 , 2 , 3 , 4 , 4 , 5 };
175+ Integer [] numbers = {1 , 1 , 2 , 3 , 4 , 4 , 5 };
181176 Stream <List <Integer >> aggregated = StreamUtils .aggregateOnListCondition (stream (numbers ),
182- (currentList , nextInt ) -> currentList .stream ().mapToInt (Integer ::intValue ).sum () + nextInt <= 5 );
177+ (currentList , nextInt ) -> currentList .stream ().mapToInt (Integer ::intValue ).sum () + nextInt <= 5 );
183178 assertThat (aggregated ).containsExactly (asList (1 , 1 , 2 ), asList (3 ), asList (4 ), asList (4 ), asList (5 ));
184179 }
185180
186181 @ Test
187182 public void givenProjectionFunction_whenMaxedBy_thenMaxedBy () {
188183 Stream <String > clubs = Stream .of ("Juventus" , "Barcelona" , "PSG" );
189184 Optional <String > longestName = clubs .collect (CollectorUtils .maxBy (String ::length ));
190- assertThat (longestName . get ()). isEqualTo ("Barcelona" );
185+ assertThat (longestName ). contains ("Barcelona" );
191186 }
192187
193188 @ Test
194189 public void givenStreamOfMultipleElem_whenUniqueCollector_thenValueReturned () {
195190 Stream <Integer > singleElement = Stream .of (1 );
196191 Optional <Integer > unique = singleElement .collect (CollectorUtils .unique ());
197- assertThat (unique .get ()).isEqualTo (1 );
198-
192+ assertThat (unique ).contains (1 );
199193 }
200194
201195 @ Test
@@ -205,5 +199,4 @@ public void givenStreamOfMultipleElem_whenUniqueCollector_thenExceptionThrown()
205199 multipleElement .collect (CollectorUtils .unique ());
206200 });
207201 }
208-
209202}
0 commit comments