T - Input type for predicate and function (action)R - Return type for function (action) which is executed if the predicate tests positiveX - Type of Function - cyclops pattern matching builders use ActionWithReturn which is serialisable and retains type infopublic interface Case<T,R,X extends java.util.function.Function<T,R>>
| Modifier and Type | Method and Description |
|---|---|
default Case<T,R,java.util.function.Function<T,R>> |
and(java.util.function.Predicate<T> and)
Create a new case which ands the supplied predicate with the current predicate.
|
default Case<T,R,java.util.function.Function<T,R>> |
andOfType(java.lang.Class<T> and)
Add a guard that assures input is of specified type.
|
default Case<T,R,java.util.function.Function<T,R>> |
andOfValue(T and)
Add a guard that assures input is of specified type.
|
default <T1> Case<T,T1,java.util.function.Function<T,T1>> |
andThen(Case<R,T1,? extends java.util.function.Function<R,T1>> after)
Provide a Case that will be run only if this one matches successfully
The result of the current case will be the input into the supplied case
Both cases have to be successful to return a result from match
|
default <T1> Case<T,T1,java.util.function.Function<T,T1>> |
andThen(Cases<R,T1,? extends java.util.function.Function<R,T1>> after)
Add a set of Cases that will be run if this case matches successfull
|
default Case<T,T,java.util.function.Function<T,T>> |
andThenFunction(java.util.function.Function<R,T> after)
Creates a new Case that will execute the supplied function after current function if current predicate holds.
|
default Case<T,R,java.util.function.Function<T,R>> |
andWithValues(java.lang.Object... with)
Insert a guard that decomposes input values and compares against the supplied values
Recursive decomposition is possible via Predicates.type and Predicates.with methods
|
default <T1> Case<T1,R,java.util.function.Function<T1,R>> |
compose(Case<T1,T,? extends java.util.function.Function<T1,T>> before)
Provide a Case that will be executed before the current one.
|
default <T1> Case<T1,R,java.util.function.Function<T1,R>> |
composeAnd(java.util.function.Predicate<T1> and,
java.util.function.Function<T1,T> before)
Compose a new Case which executes the Predicate and function supplied before the current predicate
and function.
|
default <T1> Case<T1,R,java.util.function.Function<T1,R>> |
composeFunction(java.util.function.Function<T1,T> before)
Creates a new Case that will execute the supplied function before current function if current predicate holds.
|
default <T1> Case<T1,R,java.util.function.Function<T1,R>> |
composeOr(Case<T1,T,? extends java.util.function.Function<T1,T>> before)
Provide a Case that will be executed before the current one.
|
static <T,R,X extends java.util.function.Function<T,R>> |
empty() |
default Case<T,R,X> |
filter(java.util.function.Predicate<Two<java.util.function.Predicate<T>,X>> predicate)
Filter this case with the supplied predicate
Some options here are to filter based on
success of predicate execution
return values from function execution
if using ActionWithReturn for function instances on Generic parameter types
Custom function types with additional info can be used with Case
|
default <T1,R1,X1 extends java.util.function.Function<T1,R1>> |
flatMap(java.util.function.Function<Two<java.util.function.Predicate<T>,X>,Case<T1,R1,X1>> mapper)
Create a new Case from the supplied Function
|
Two<java.util.function.Predicate<T>,X> |
get() |
default X |
getAction() |
default java.util.function.Predicate<T> |
getPredicate() |
boolean |
isEmpty() |
default boolean |
isNotEmpty() |
default <T1,R1,X1 extends java.util.function.Function<T1,R1>> |
map(java.util.function.Function<Two<java.util.function.Predicate<T>,X>,Two<java.util.function.Predicate<T1>,X1>> mapper)
Allows both the predicate and function in the current case to be replaced in a new Case
|
default <R1,X1 extends java.util.function.Function<T,R1>> |
mapFunction(java.util.function.Function<java.util.function.Function<T,R>,X1> mapper)
Allows the current function to be replaced by another
|
default Case<T,R,X> |
mapPredicate(java.util.function.Function<java.util.function.Predicate<T>,java.util.function.Predicate<T>> mapper)
Allows the predicate to be replaced with one returned from the supplied function
E.g.
|
default java.util.Optional<R> |
match(T value)
Match against the supplied value.
|
default java.util.concurrent.CompletableFuture<java.util.Optional<R>> |
matchAsync(java.util.concurrent.Executor executor,
T value)
Similar to Match, but executed asynchonously on supplied Executor.
|
default Case<T,R,X> |
negate() |
default Case<T,R,X> |
negate(X action) |
static <T,R,X extends java.util.function.Function<T,R>> |
of(java.util.function.Predicate<T> predicate,
X action)
Construct an instance of Case from supplied predicate and action
|
static <T,R,X extends java.util.function.Function<T,R>> |
of(Two<java.util.function.Predicate<T>,X> pattern)
Construct an instance of Case from supplied Tuple of predicate and action
|
default Case<T,R,java.util.function.Function<T,R>> |
or(java.util.function.Predicate<T> or)
Create a new Case that will loosen the current predicate.
|
default <T1> Case<T1,R,java.util.function.Function<T1,R>> |
or(java.util.function.Predicate<T1> or,
java.util.function.Function<T1,T> fn)
Syntax sugar for composeOr
|
static final Case empty
boolean isEmpty()
default java.util.function.Predicate<T> getPredicate()
default X getAction()
default Case<T,R,X> negate()
Case.of((Integer num) -> num >100,(Integer num) -> num * 10).negate();
Results in a case that will multiply numbers 100 or less by 10default Case<T,R,X> negate(X action)
action - New action to replace current one
Case.of((Integer num) -> num >100,(Integer num) -> num * 10).negate(num -> num * 5);
Results in a case that will multiply numbers 100 or less by 5default Case<T,R,X> filter(java.util.function.Predicate<Two<java.util.function.Predicate<T>,X>> predicate)
empty = Case.of(t->true,input->10).filter(p->false);
assertThat(empty,instanceOf(EmptyCase.class));
ActionWithReturn<String,Integer> act = hello ->10;
caze = Case.of(t->true, act);
predicate - default Case<T,R,X> mapPredicate(java.util.function.Function<java.util.function.Predicate<T>,java.util.function.Predicate<T>> mapper)
case1 =Case.of(input->true,input->input+10); assertThat(case1.mapPredicate(p-> t->false).match(100).isPresent(),is(false));
mapper - Function that supplies the new predicatedefault <R1,X1 extends java.util.function.Function<T,R1>> Case<T,R1,X1> mapFunction(java.util.function.Function<java.util.function.Function<T,R>,X1> mapper)
case1 =Case.of(input->true,input->input+10); assertThat(case1.mapFunction(fn-> input->input+20).match(100).get(),is(120));Returns 100+20 rather than 100+10
mapper - Function that supplies the new functiondefault <T1,R1,X1 extends java.util.function.Function<T1,R1>> Case<T1,R1,X1> map(java.util.function.Function<Two<java.util.function.Predicate<T>,X>,Two<java.util.function.Predicate<T1>,X1>> mapper)
case1 =Case.of(input->false,input->input+10);
Tuple2<Predicate<Integer>,Function<Integer,Integer>> tuple = Tuple.tuple( t->true,(Integer input)->input+20);
assertThat(case1.map(tuple2 -> tuple).match(100).get(),is(120));
mapper - Function that generates the new predicate and action (function)default <T1,R1,X1 extends java.util.function.Function<T1,R1>> Case<T1,R1,X1> flatMap(java.util.function.Function<Two<java.util.function.Predicate<T>,X>,Case<T1,R1,X1>> mapper)
case1 =Case.of(input->false,input->input+10); assertThat(case1.flatMap(tuple2 -> Case.of(tuple2.v1,(Integer input)->input+20)).match(100).get(),is(120));
mapper - Function that creates a new Casedefault <T1> Case<T,T1,java.util.function.Function<T,T1>> andThen(Case<R,T1,? extends java.util.function.Function<R,T1>> after)
case1 =Case.of(input->false,input->input+10); assertThat(case1.andThen(Case.of(input->true,input->input+1)).match(100).get(),is(111));
after - Case that will be run after this one, if it matches successfullydefault <T1> Case<T,T1,java.util.function.Function<T,T1>> andThen(Cases<R,T1,? extends java.util.function.Function<R,T1>> after)
after - Cases to run if this case matchesdefault <T1> Case<T1,R,java.util.function.Function<T1,R>> compose(Case<T1,T,? extends java.util.function.Function<T1,T>> before)
case1 =Case.of(input->false,input->input+10);
assertThat(case1.compose(Case.of((Integer input)->true,input->input*2)).match(100).get(),is(210))
(100*2)+10=210before - Case to be run before this onedefault <T1> Case<T1,R,java.util.function.Function<T1,R>> composeOr(Case<T1,T,? extends java.util.function.Function<T1,T>> before)
case1 =Case.of(input->false,input->input+10); assertThat(case1.composeOr(Case.of((Integer input)->false,input->input*2)).match(100).get(),is(210));
before - Case to be run before this onedefault <T1> Case<T1,R,java.util.function.Function<T1,R>> composeFunction(java.util.function.Function<T1,T> before)
case1 =Case.of(input->false,input->input+10); assertThat(case1.composeFunction((Integer input)->input*2).match(100).get(),is(210));
before - Function to execute before current functiondefault Case<T,T,java.util.function.Function<T,T>> andThenFunction(java.util.function.Function<R,T> after)
case1 =Case.of(input->false,input->input+10); assertThat(case1.andThenFunction(input->input*2).match(100).get(),is(220));
after - Function to execute after the current functiondefault Case<T,R,java.util.function.Function<T,R>> or(java.util.function.Predicate<T> or)
case1 =Case.of(input->true,input->input+10); offCase = case1.mapPredicate(p-> p.negate()); assertThat(offCase.or(t->true).match(100).get(),is(110));
or - Predicate that will be or'd with the current predicatedefault <T1> Case<T1,R,java.util.function.Function<T1,R>> or(java.util.function.Predicate<T1> or, java.util.function.Function<T1,T> fn)
or - Predicate for before casefn - Function for before caseProvide a Case that will be executed before the current one. The function from the supplied Case will be executed
first and it;s output will be provided to the function of the current case - if either predicate holds.
case1 =Case.of(input->false,input->input+10);
assertThat(case1.composeOr(Case.of((Integer input)->false,input->input*2)).match(100).get(),is(210));
default Case<T,R,java.util.function.Function<T,R>> and(java.util.function.Predicate<T> and)
assertThat(case1.and(p->false).match(100).isPresent(),is(false));
assertThat(case1.and(p->true).match(100).isPresent(),is(true));
and - New Predicate to be and'd with currentdefault Case<T,R,java.util.function.Function<T,R>> andOfType(java.lang.Class<T> and)
case1 =Case.of(input->true,input->input+10); assertThat(case1.andOfType(Integer.class).match(100).isPresent(),is(true)); assertThat(((Case)case1).andOfType(String.class).match(100).isPresent(),is(false));
and - Class to check type againstdefault Case<T,R,java.util.function.Function<T,R>> andOfValue(T and)
case1 =Case.of(input->true,input->input+10); assertThat(((Case)case1).andOfValue(5).match(100).isPresent(),is(false)); assertThat(case1.andOfValue(100).match(100).isPresent(),is(true));
and - Class to check type againstdefault Case<T,R,java.util.function.Function<T,R>> andWithValues(java.lang.Object... with)
with - values to compare to - or predicates or hamcrest matchersPredicates.type(java.lang.Class<T>),
{@code
val case2 = Case.of((Person p)->p.age>18,p->p.name + " can vote");
assertThat(case2.andWithValues(__,__,Predicates.with(__,__,"Ireland")).match(new Person("bob",19,new Address(10,"dublin","Ireland"))).isPresent(),is(true));
assertThat(case2.andWithValues(__,__,with(__,__,"Ireland")).match(new Person("bob",17,new Address(10,"dublin","Ireland"))).isPresent(),is(false));
\@Value static final class Person implements Decomposable{ String name; int age; Address address; }
\@Value static final class Address implements Decomposable { int number; String city; String country;}
}default <T1> Case<T1,R,java.util.function.Function<T1,R>> composeAnd(java.util.function.Predicate<T1> and, java.util.function.Function<T1,T> before)
e.g. match(100)
new Predicate passes (100) -> apply new function (100) returns 20
-> predicate from current case recieves (20) passes
-> apply function from current case recieves (20)
both predicates must pass or Optional.empty() is returned from match.
case1 =Case.of(input->true,input->input+10);
assertThat(case1.composeAnd(p->false,(Integer input)->input*2).match(100).isPresent(),is(false));
assertThat(case1.composeAnd(p->true,(Integer input)->input*2).match(100).get(),is(210));
and - Predicate to be and'd with current predicatebefore - default boolean isNotEmpty()
default java.util.Optional<R> match(T value)
value - To match againstdefault java.util.concurrent.CompletableFuture<java.util.Optional<R>> matchAsync(java.util.concurrent.Executor executor, T value)
executor - Executor to execute matching onvalue - Value to match againstMatch against the supplied value.
Value will be passed into the current predicate
If it passes / holds, value will be passed to the current function.
The result of function application will be returned wrapped in an Optional.
If the predicate does not hold, Optional.empty() is returned.static <T,R,X extends java.util.function.Function<T,R>> Case<T,R,X> of(java.util.function.Predicate<T> predicate, X action)
predicate - That will be used to matchaction - Function that is executed on succesful matchstatic <T,R,X extends java.util.function.Function<T,R>> Case<T,R,X> of(Two<java.util.function.Predicate<T>,X> pattern)
pattern - containing the predicate that will be used to match and the function that is executed on succesful matchstatic <T,R,X extends java.util.function.Function<T,R>> Case<T,R,X> empty()