-
Notifications
You must be signed in to change notification settings - Fork 51
Mixins : Matchable
johnmcclean-aol edited this page Feb 24, 2016
·
12 revisions
Cyclops has merged with simple-react. Please update your bookmarks (stars :) ) to https://github.com/aol/cyclops-react
All new develpoment on cyclops occurs in cyclops-react. Older modules are still available in maven central.

Implementing com.aol.cyclops.matcher.Matchable adds a Pattern Matching support to any object.
Any Object can be coerced to a Matchable via the following methods
- Matchable.of
- Matchable.listOfValues
- The host object will be provided as an argument to the Pattern Matcher
- If the host object implements Decomposable that will be used to recursively pattern match (if required)
- If the host doesn't implement Decomposable and recursive pattern matching is being used, it will be coerced to Decomposable
FeatureToggle<Data> apply;
Data d = apply.matches(
c -> c.isType((Enabled<Data> e)-> e.get()),
c -> c.isType( (Disabled<Data> d) -> Data.empty()
));
Matchable.listOfValues(1,2)
.matches(
c->c.hasValues(1,3).then(i->2),
c->c.hasValues(1,2).then(i->3)
);
//returns 3
String result = Matchable.listOfValues(1,new MyCase(4,5,6))
.matches(
c->c.hasValues(Predicates.__,Predicates.hasValues(4,5,6)).then(i->"rec1"),
c->c.hasValues(2,Predicates.hasValues(1,2,3)).then(i->"rec2")
);
//"rec1"
Matchable.of(Optional.empty())
.mayMatch(
o-> o.isEmpty().then(i->"hello"),
o-> o.hasValues(1).then(i->""+2)
)
With Lombok @Value
As.asMatchable(new MyCase2(1,2,3)).match(this::cases),equalTo("hello")
@Value static class MyCase { String key; int value;}