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 2020import org .jayield .Traverser ;
2121import org .jayield .Yield ;
2222
23- import java .util .Iterator ;
2423import java .util .List ;
24+ import java .util .Spliterator ;
2525
2626public 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 ;
2928
3029 public AdvancerList (List <U > data ) {
31- this .data = data ;
32- this .current = data .iterator ();
30+ this .current = data .spliterator ();
3331 }
3432
3533 @ Override
3634 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 );
4036 }
4137
4238 @ Override
4339 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 );
4741 }
4842}
Original file line number Diff line number Diff line change @@ -289,6 +289,28 @@ public void testReduce() {
289289 assertEquals (actual , expected );
290290 }
291291
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+
292314 @ Test
293315 public void testReduceOnEmpty () {
294316 String [] input = {};
You can’t perform that action at this time.
0 commit comments