public interface Matchable<TYPE>
then(Supplier)
when(Predicate)
otherwise(Supplier)
Matchables} for precanned structural pattern matching against JDK classes
Use Matchable#of or Matchable#matchable (an alias for use with static imports) to pattern match via guards on an Object.
{@code
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
import static com.aol.cyclops.util.function.Predicates.decons;
Customer customer = new Customer("test",new Address(10,"hello","my city"));
String result = customer.match()
.on$_2() //match on address
.matches(c->c.is(when(decons(when(10,"hello","my city"))),then("hello")),
otherwise("miss"))
.get();
//"hello"
Modifier and Type | Interface and Description |
---|---|
static class |
Matchable.AsMatchable
Coerce a type to implement the MatchableObject interface
|
static class |
Matchable.AutoCloseableMatchableIterable<TYPE> |
static class |
Matchable.CheckValue1<T,R> |
static class |
Matchable.CheckValue2<T1,T2,R>
Class used to build a pattern matching class against two values
cyclops-react pattern matching uses the is /has methods to denote individual cases.
|
static class |
Matchable.CheckValue3<T1,T2,T3,R>
Class used to build a pattern matching class against three values
cyclops-react pattern matching uses the is /has methods to denote individual cases.
|
static class |
Matchable.CheckValue4<T1,T2,T3,T4,R>
Class used to build a pattern matching class against four values
cyclops-react pattern matching uses the is /has methods to denote individual cases.
|
static class |
Matchable.CheckValue5<T1,T2,T3,T4,T5,R>
Class used to build a pattern matching class against a five values
cyclops-react pattern matching uses the is /has methods to denote individual cases.
|
static class |
Matchable.CheckValueOpt<T,R>
Class used to build a pattern matching class against a single optional value
cyclops-react pattern matching uses the is /has methods to denote individual cases.
|
static class |
Matchable.CheckValues<T,R>
Class used to build a pattern matching class against multiple values
cyclops-react pattern matching uses the is /has methods to denote individual cases.
|
static interface |
Matchable.MatchableIterable<TYPE> |
static interface |
Matchable.MatchableObject<TYPE> |
static interface |
Matchable.MatchableOptional<T> |
static interface |
Matchable.MatchSelf<TYPE>
Interface that returns this as the Matchable
|
static interface |
Matchable.MTuple1<T1> |
static interface |
Matchable.MTuple2<T1,T2> |
static interface |
Matchable.MTuple3<T1,T2,T3> |
static interface |
Matchable.MTuple4<T1,T2,T3,T4> |
static interface |
Matchable.MTuple5<T1,T2,T3,T4,T5> |
static interface |
Matchable.MXor<T1,T2> |
static interface |
Matchable.ValueAndOptionalMatcher<T> |
Modifier and Type | Method and Description |
---|---|
static <T> Matchable.MTuple1<T> |
matchable(T o)
Match on the supplied Object
|
static <T> Matchable.MatchableObject<T> |
of(java.util.stream.Stream<T> o)
Create a new matchable that will match on the fields of the provided Stream
|
static <T> Matchable.MTuple1<T> |
of(T o)
Create a new matchable that will match on the fields of the provided Object
|
static <R> java.util.function.Supplier<R> |
otherwise(R value)
Matchable DSL operator for default results when no match is found
|
static <R> java.util.function.Supplier<R> |
otherwise(java.util.function.Supplier<? extends R> s)
Matchable DSL operator for default results when no match is found
|
static <T,R> java.util.function.Supplier<? extends R> |
then(R value)
Matchable DSL operator for outcomes
|
static <T,R> java.util.function.Supplier<? extends R> |
then(java.util.function.Supplier<? extends R> fn)
Matchable DSL operator for outcomes
|
static <T1> Matchable.MTuple1<java.util.function.Predicate<? super T1>> |
when(java.util.function.Predicate<? super T1> t1)
Matchable DSL operator for matching against a single value
|
static <T1,T2,T3> Matchable.MTuple2<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>> |
when(java.util.function.Predicate<? super T1> t1,
java.util.function.Predicate<? super T2> t2)
Matchable DSL operator for matching against two values using predicates
|
static <T1,T2,T3> Matchable.MTuple3<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>> |
when(java.util.function.Predicate<? super T1> t1,
java.util.function.Predicate<? super T2> t2,
java.util.function.Predicate<? super T3> t3)
Matchable DSL operator for matching against three values with predicates
|
static <T1,T2,T3,T4> |
when(java.util.function.Predicate<? super T1> t1,
java.util.function.Predicate<? super T2> t2,
java.util.function.Predicate<? super T3> t3,
java.util.function.Predicate<? super T4> t4)
Matchable DSL operator for matching against four values with predicates
|
static <T1,T2,T3,T4,T5> |
when(java.util.function.Predicate<? super T1> t1,
java.util.function.Predicate<? super T2> t2,
java.util.function.Predicate<? super T3> t3,
java.util.function.Predicate<? super T4> t4,
java.util.function.Predicate<? super T5> t5)
Matchable DSL operator for matching against five values with predicates
|
static <T1> Matchable.MTuple1<java.util.function.Predicate<? super T1>> |
when(T1 t1)
Matchable DSL operator for matching against a single value
|
static <T1,T2> Matchable.MTuple2<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>> |
when(T1 t1,
T2 t2)
Matchable DSL operator for matching against two values
|
static <T1,T2,T3> Matchable.MTuple3<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>> |
when(T1 t1,
T2 t2,
T3 t3)
Matchable DSL operator for matching against three values
|
static <T1,T2,T3,T4> |
when(T1 t1,
T2 t2,
T3 t3,
T4 t4)
Matchable DSL operator for matching against four values
|
static <T1,T2,T3,T4,T5> |
when(T1 t1,
T2 t2,
T3 t3,
T4 t4,
T5 t5)
Matchable DSL operator for matching against five values with predicates
|
static <T,V> Matchable.MTuple1<java.util.function.Predicate<? super T>> |
whenGuard(V... values)
Matchable DSL operator for matching recursively
|
static <T,V> java.lang.Iterable<java.util.function.Predicate<? super T>> |
whenGuard2(V... values) |
static <T1> java.lang.Iterable<java.util.function.Predicate<? super T1>> |
whenTrue(java.util.function.Predicate<? super T1>... t1) |
static <T1> java.lang.Iterable<java.util.function.Predicate<? super T1>> |
whenValues(T1... t1)
Create an Iterable of Predicates that check for equality against the provided values
|
static <T> Matchable.MTuple1<T> matchable(T o)
Matchable.matchable(100)
.matches(c->c.is(when(greaterThan(50)), ()->"large"), ()->"small");
//Eval["large"]
For a structural equivalent, matching on a single object @see Matchables.match(Object)
o
- static <T> Matchable.MTuple1<T> of(T o)
import static com.aol.cyclops.util.function.Predicates.__;
import static com.aol.cyclops.control.Matchable.whenGuard;
import static com.aol.cyclops.util.function.Predicates.has;
Matchable.of(new NestedCase(1,2,new NestedCase(3,4,null)))
.matches(cases->cases.is(whenGuard(1,__,has(3,4,__)),then("2")),otherwise("4"))
o
- Object to match on it's fieldsstatic <T> Matchable.MatchableObject<T> of(java.util.stream.Stream<T> o)
o
- Object to match on it's fieldsstatic <T,R> java.util.function.Supplier<? extends R> then(R value)
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
import static com.aol.cyclops.util.function.Predicates.instanceOf;
Eval<Integer> result = Matchables.future(CompletableFuture.completedFuture(10))
.matches(c->
c.is( when(some(10)), then(20)), //success
c->c.is(when(instanceOf(RuntimeException.class)), then(2)), //failure
otherwise(3) //no match
);
//Eval.now(20)
value
- Result valuestatic <T,R> java.util.function.Supplier<? extends R> then(java.util.function.Supplier<? extends R> fn)
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
Maybe.just(2).matches(c->c.is(when(1),then("hello"))
.is(when(2),then(()->load(1)))
.is(when(3),then(()->"hello")),otherwise("miss"));
//Eval[load(1)]
fn
- Supplier that generates the result valuestatic <R> java.util.function.Supplier<R> otherwise(R value)
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
Try.success(30).matches(c->c.is(when(10),then("hello")),
c->c.is(when(instanceOf(Throwable.class)), then("error")),
otherwise("miss")).get()
//miss
value
- Defult valuestatic <R> java.util.function.Supplier<R> otherwise(java.util.function.Supplier<? extends R> s)
import static com.aol.cyclops.control.Matchable.otherwise;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.when;
Try.success(30).matches(c->c.is(when(10),then("hello")),
c->c.is(when(instanceOf(Throwable.class)), then("error")),
otherwise(()->"miss")).get()
//miss
s
- Supplier that generates a default value@SafeVarargs static <T,V> java.lang.Iterable<java.util.function.Predicate<? super T>> whenGuard2(V... values)
@SafeVarargs static <T,V> Matchable.MTuple1<java.util.function.Predicate<? super T>> whenGuard(V... values)
{@code //given a recursively defined Case class
values
- To match target against with potential for recursively matching within provided valuesstatic <T1> java.lang.Iterable<java.util.function.Predicate<? super T1>> whenValues(T1... t1)
t1
- Values to create equality check predicates forstatic <T1> java.lang.Iterable<java.util.function.Predicate<? super T1>> whenTrue(java.util.function.Predicate<? super T1>... t1)
static <T1> Matchable.MTuple1<java.util.function.Predicate<? super T1>> when(T1 t1)
import static com.aol.cyclops.control.Matchable.when;
import static com.aol.cyclops.control.Matchable.then;
import static com.aol.cyclops.control.Matchable.otherwise;
Eval.later(()->10)
.matches(c->c.is(when(10),then("hello")),
otherwise("miss"))
//Eval["hello"]
t1
- Value to match against (equality check)static <T1> Matchable.MTuple1<java.util.function.Predicate<? super T1>> when(java.util.function.Predicate<? super T1> t1)
FutureW.ofResult(10)
.map(i->throw new RuntimeException())
.matches(c->c.is(when(10),then("hello")).is(when(2),then("hello")),
c->c.is(when(Predicates.instanceOf(Throwable.class)), then("error")),
otherwise("miss"));
//Eval["error"]
t1
- Predicate to match againststatic <T1,T2> Matchable.MTuple2<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>> when(T1 t1, T2 t2)
URL url = ReactiveSeq.of("input.file")
.map(getClass().getClassLoader()::getResource)
.single();
//Match on the first two lines of a file defined by an URL
String result = Matchables.lines(url)
.on$12___()
.matches(c->c.is(when("hello","world2"),then("incorrect"))
.is(when("hello","world"),then("correct")),
otherwise("miss"))
.get();
//"correct"
t1
- 1st value to match against (equality check)t2
- 2nd value to match against (equality check)static <T1,T2,T3> Matchable.MTuple2<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>> when(java.util.function.Predicate<? super T1> t1, java.util.function.Predicate<? super T2> t2)
Matchables.match2(100,2)
.matches(c->c.is(when(Predicates.greaterThan(50),Predicates.lessThan(10)), ()->"large and small"), ()->"not large and small")
.get(),
//"large and small"
t1
- 1st value to match againstt2
- 2nd value to match againststatic <T1,T2,T3> Matchable.MTuple3<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>> when(T1 t1, T2 t2, T3 t3)
Predicates.decons(when(10,"hello","my city"))
.test(new Address(10,"hello","my city").match()));
//true
String result = new Customer("test",new Address(10,"hello","my city"))
.match()
.on$_2()
.matches(c->c.is(when(decons(when(10,"hello","my city"))),then("hello")), otherwise("miss")).get();
//"hello")
t1
- 1st value to match againstt2
- 2nd value to match againstt3
- 3rd value to match againststatic <T1,T2,T3> Matchable.MTuple3<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>> when(java.util.function.Predicate<? super T1> t1, java.util.function.Predicate<? super T2> t2, java.util.function.Predicate<? super T3> t3)
Matchables.match3(100,2,1000)
.matches(c->c.is(when(Predicates.greaterThan(50),Predicates.lessThan(10),Predicates.greaterThan(500)), ()->"large and small and huge"), ()->"not large and small")
.get()
//"large and small and huge"
t1
- 1st value to match againstt2
- 2nd value to match againstt3
- 3rd value to match againststatic <T1,T2,T3,T4> Matchable.MTuple4<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>,java.util.function.Predicate<? super T4>> when(T1 t1, T2 t2, T3 t3, T4 t4)
Eval<String> url = Matchables.url(new URL("http://www.aol.com/path?q=hello"))
.on$12_45()
.matches(c->c.is(when("http","www.aol.com","/path","q=hello"), then("correct")),otherwise("miss"));
//Eval.now("correct")
t1
- 1st value to match againstt2
- 2nd value to match againstt3
- 3rd value to match againstt4
- 4th value to match againststatic <T1,T2,T3,T4> Matchable.MTuple4<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>,java.util.function.Predicate<? super T4>> when(java.util.function.Predicate<? super T1> t1, java.util.function.Predicate<? super T2> t2, java.util.function.Predicate<? super T3> t3, java.util.function.Predicate<? super T4> t4)
Matchables.url(new URL("http://www.aol.com/path?q=hello"))
.on$12_45()
.matches(c->c.is(when(eq("http"),in("www.aol.com","aol.com"),any(),not(eq("q=hello!"))), then("correct")),
otherwise("miss"));
//Eval["miss"]
t1
- 1st value to match againstt2
- 2nd value to match againstt3
- 3rd value to match againstt4
- 4th value to match againststatic <T1,T2,T3,T4,T5> Matchable.MTuple5<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>,java.util.function.Predicate<? super T4>,java.util.function.Predicate<? super T5>> when(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5)
String result = Matchables.chars("hello,world")
.matches(c->c.has(when('h','e','l','l','o'), then("startsWith")),
otherwise("miss"))
.get();
Eval["startsWith"]
t1
- 1st value to match againstt2
- 2nd value to match againstt3
- 3rd value to match againstt4
- 4th value to match againstt5
- 5th value to match againststatic <T1,T2,T3,T4,T5> Matchable.MTuple5<java.util.function.Predicate<? super T1>,java.util.function.Predicate<? super T2>,java.util.function.Predicate<? super T3>,java.util.function.Predicate<? super T4>,java.util.function.Predicate<? super T5>> when(java.util.function.Predicate<? super T1> t1, java.util.function.Predicate<? super T2> t2, java.util.function.Predicate<? super T3> t3, java.util.function.Predicate<? super T4> t4, java.util.function.Predicate<? super T5> t5)
String result = Matchables.chars("hello,world")
.matches(c->c.has(when(eq('h'),not(eq('e')),eq('l'),eq('l'),eq('o')), then("startsWith")),
otherwise("miss"))
.get();
Eval["miss"]
t1
- 1st value to match againstt2
- 2nd value to match againstt3
- 3rd value to match againstt4
- 4th value to match againstt5
- 5th value to match against