public class ForComprehensions
extends java.lang.Object
Constructor and Description |
---|
ForComprehensions() |
Modifier and Type | Method and Description |
---|---|
static <X,V> ComprehensionsModule.MyComprehension<X,V> |
custom(java.lang.Class<X> c)
Step builder for Creating a for comprehension using a custom interface
|
static <X,R> R |
foreachX(java.lang.Class<X> c,
java.util.function.Function<X,R> fn)
Create a for comprehension using a custom interface
|
static <R> R |
foreachX(java.util.function.Function<ComprehensionsModule.ComprehensionData<?,R,? extends ComprehensionsModule.Initialisable>,R> fn)
Create a custom for comprehension virtually unlimited in nesting depths
|
public static <R> R foreachX(java.util.function.Function<ComprehensionsModule.ComprehensionData<?,R,? extends ComprehensionsModule.Initialisable>,R> fn)
List<Integer> list= Arrays.asList(1,2,3);
Stream<Integer> stream = foreachX(c -> c.$("hello",list)
.filter(()->c.<Integer>$("hello")<10)
.yield(()-> c.<Integer>$("hello")+2));
assertThat(Arrays.asList(3,4,5),equalTo(stream.collect(Collectors.toList())));
fn
- For comprehensionpublic static <X,R> R foreachX(java.lang.Class<X> c, java.util.function.Function<X,R> fn)
List<Integer> list= Arrays.asList(1,2,3);
Stream<Integer> stream = foreachX(Custom.class,
c-> c.myVar(list)
.yield(()->c.myVar()+3)
);
assertThat(Arrays.asList(4,5,6),equalTo(stream.collect(Collectors.toList())));
static interface Custom extends CustomForComprehension<Stream<Integer>,Custom>{
Integer myVar();
Custom myVar(List<Integer> value);
}
c
- Interface that defines for comprehension - should extend CustomForComprehensionfn
- for comprehensionpublic static <X,V> ComprehensionsModule.MyComprehension<X,V> custom(java.lang.Class<X> c)
MyComprehension<Custom2,Custom2> comp2 = ForComprehensions.custom(Custom2.class);
comp.foreach(c -> c.i(Arrays.asList(20,30))
.j(Arrays.asList(1,2,3))
.yield(() -> c.i() +c.j()));
c
- Interface that defines for comprehension - should extend CustomForComprehension