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 class Cases<T,R> extends java.lang.Object implements java.util.function.Function<T,Maybe<R>>
Modifier and Type | Method and Description |
---|---|
Cases<T,R> |
append(int index,
Case<T,R> pattern)
Append an individual case with supplied Cases inserted at index
|
Maybe<R> |
apply(T t) |
<T1,X> java.util.function.Function<T1,X> |
asUnwrappedFunction()
assertThat(Cases.of(Case.of(input->true,input->"hello")).asUnwrappedFunction().apply(10),is("hello"));
|
org.pcollections.PStack<Case<T,R>> |
get() |
<R> Maybe<R> |
match(java.lang.Object... t)
Aggregates supplied objects into a List for matching against
|
<R> Maybe<R> |
match(T t) |
<R> java.util.concurrent.CompletableFuture<Maybe<R>> |
matchAsync(java.util.concurrent.Executor executor,
java.lang.Object... t)
Aggregates supplied objects into a List for matching asynchronously against
|
<R> java.util.stream.Stream<R> |
matchFromStream(java.util.stream.Stream<T> s)
Each input element can generated a single matched value
|
<R> java.util.concurrent.CompletableFuture<java.util.stream.Stream<R>> |
matchFromStreamAsync(java.util.concurrent.Executor executor,
java.util.stream.Stream<T> s)
Execute matchFromStream asynchronously
|
static <T,R> Cases<T,R> |
of(Case<T,R>... cazes)
Construct a Cases instance from an array Pattern Matching Cases
Will execute sequentially when Match is called.
|
static <T,R> Cases<T,R> |
ofList(java.util.List<Case<T,R>> cases)
Construct a Cases instance from a list of Pattern Matching Cases
Will execute sequentially when Match is called.
|
static <T,R> Cases<T,R> |
ofPStack(org.pcollections.PStack<Case<T,R>> cases)
Construct a Cases instance from a persistent stack of Pattern Matching Cases
Will execute sequentially when Match is called.
|
int |
size() |
java.util.stream.Stream<Case<T,R>> |
stream() |
<R> Maybe<R> |
unapply(Decomposable t)
Decomposes the supplied input via it's unapply method
Provides a List to the Matcher of values to match on
|
public static <T,R> Cases<T,R> ofPStack(org.pcollections.PStack<Case<T,R>> cases)
cases
- Persistent Stack of cases to build Cases frompublic static <T,R> Cases<T,R> ofList(java.util.List<Case<T,R>> cases)
cases
- Persistent Stack of cases to build Cases frompublic static <T,R> Cases<T,R> of(Case<T,R>... cazes)
cazes
- Array of cases to build Cases instance frompublic Cases<T,R> append(int index, Case<T,R> pattern)
index
- to insert supplied cases in new Case instancepattern
- Cases to append / insert#merge
public int size()
public <T1,X> java.util.function.Function<T1,X> asUnwrappedFunction()
assertThat(Cases.of(Case.of(input->true,input->"hello")).asUnwrappedFunction().apply(10),is("hello"));
public <R> java.util.stream.Stream<R> matchFromStream(java.util.stream.Stream<T> s)
List<String> results = Cases .of(Case.of((Integer input) -> 10 == input, input -> "hello"), Case.of((Integer input) -> 10 == input, input -> "ignored"), Case.of(input -> 11 == input, input -> "world")) .<String> matchFromStream(Stream.of(1, 11, 10)).toList(); assertThat(results.size(), is(2)); assertThat(results, hasItem("hello")); assertThat(results, hasItem("world"));
s
- Stream of data to match against (input to matcher)public <R> java.util.concurrent.CompletableFuture<java.util.stream.Stream<R>> matchFromStreamAsync(java.util.concurrent.Executor executor, java.util.stream.Stream<T> s)
executor
- executor Executor to execute task asynchronouslys
- Stream of data
List<String> results = Cases
.of(Case.of((Integer input) -> 10 == input, input -> "hello"),
Case.of((Integer input) -> 10 == input, input -> "ignored"),
Case.of(input -> 11 == input, input -> "world"))
.<String> matchFromStreamAsync(ForkJoinPool.commonPool(),Stream.of(1, 11, 10)).join().toList();
assertThat(results.size(), is(2));
assertThat(results, hasItem("hello"));
assertThat(results, hasItem("world"));
public <R> Maybe<R> match(java.lang.Object... t)
assertThat(Cases.of(Case.of((List<Integer> input) -> input.size()==3, input -> "hello"), Case.of((List<Integer> input) -> input.size()==2, input -> "ignored"), Case.of((List<Integer> input) -> input.size()==1, input -> "world")).match(1,2,3).get(),is("hello"));
t
- Array to match onpublic <R> java.util.concurrent.CompletableFuture<Maybe<R>> matchAsync(java.util.concurrent.Executor executor, java.lang.Object... t)
assertThat(Cases.of(Case.of((List<Integer> input) -> input.size()==3, input -> "hello"), Case.of((List<Integer> input) -> input.size()==2, input -> "ignored"), Case.of((List<Integer> input) -> input.size()==1, input -> "world")) .matchAsync(ForkJoinPool.commonPool(),1,2,3).join().get(),is("hello"));
executor
- Executor to perform the async taskt
- Array to match onpublic <R> Maybe<R> unapply(Decomposable t)
assertThat(Cases.of(Case.of((List input) -> input.size()==3, input -> "hello"), Case.of((List input) -> input.size()==2, input -> "ignored"), Case.of((List input) -> input.size()==1, input -> "world")) .unapply(new MyClass(1,"hello")).get(),is("ignored")); \@Value static class MyClass implements Decomposable{ int value; String name; }
t
- Object to decompose and match onpublic <R> Maybe<R> match(T t)
t
- Object to match against supplied cases