File tree Expand file tree Collapse file tree 2 files changed +27
-11
lines changed
main/java/org/jayield/advs Expand file tree Collapse file tree 2 files changed +27
-11
lines changed Original file line number Diff line number Diff line change 20
20
import org .jayield .Traverser ;
21
21
import org .jayield .Yield ;
22
22
23
- import java .util .Iterator ;
24
23
import java .util .List ;
24
+ import java .util .Spliterator ;
25
25
26
26
public class AdvancerList <U > implements Advancer <U >, Traverser <U > {
27
- private final List <U > data ;
28
- private final Iterator <U > current ;
27
+ private final Spliterator <U > current ;
29
28
30
29
public AdvancerList (List <U > data ) {
31
- this .data = data ;
32
- this .current = data .iterator ();
30
+ this .current = data .spliterator ();
33
31
}
34
32
35
33
@ Override
36
34
public void traverse (Yield <? super U > yield ) {
37
- if (!current .hasNext ())
38
- throw new IllegalStateException ("Traverser has already been operated on or closed!" );
39
- data .forEach (yield ::ret );
35
+ current .forEachRemaining (yield ::ret );
40
36
}
41
37
42
38
@ Override
43
39
public boolean tryAdvance (Yield <? super U > yield ) {
44
- if (!current .hasNext ()) return false ;
45
- yield .ret (current .next ());
46
- return true ;
40
+ return current .tryAdvance (yield ::ret );
47
41
}
48
42
}
Original file line number Diff line number Diff line change @@ -289,6 +289,28 @@ public void testReduce() {
289
289
assertEquals (actual , expected );
290
290
}
291
291
292
+ @ Test
293
+ public void testFlatMapAndReduce () {
294
+ List <Query <String >> input = new ArrayList <>();
295
+ input .add (Query .of ("a" ));
296
+ input .add (Query .of ("b" ));
297
+ input .add (Query .of ("c" ));
298
+ String expected = "abc" ;
299
+ String actual = fromList (input ).flatMap (s -> s ).reduce ((p , c ) -> p + c ).orElseThrow ();
300
+ assertEquals (actual , expected );
301
+ }
302
+
303
+ @ Test
304
+ public void testFromListFlatMapAndReduce () {
305
+ List <Query <String >> input = new ArrayList <>();
306
+ input .add (fromList (List .of ("a" )));
307
+ input .add (fromList (List .of ("b" )));
308
+ input .add (fromList (List .of ("c" )));
309
+ String expected = "abc" ;
310
+ String actual = fromList (input ).flatMap (s -> s ).reduce ((p , c ) -> p + c ).orElseThrow ();
311
+ assertEquals (actual , expected );
312
+ }
313
+
292
314
@ Test
293
315
public void testReduceOnEmpty () {
294
316
String [] input = {};
You can’t perform that action at this time.
0 commit comments