Skip to content

Matchables

johnmcclean-aol edited this page Nov 21, 2016 · 2 revisions

The Matchables class provides a large number of methods for structural pattern matching on common JDK and cyclops-react type - for example to match on a URL

Match on a URL

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<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["correct"]

Match on whether a Future has completed successfully

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<Integer> result = Matchables.future(FutureW.ofResult(1))
                                 .matches(c-> c.is( when(some(1)), then(10)), 
                                          c->c.is(when(instanceOf(RuntimeException.class)), then(2)),
                                          otherwise(3));
        
//Eval.now[10]

Eval<Integer> result = Matchables.future(FutureW.ofError(new RuntimeException()))
                                                .matches(c-> c.is( when(some(10)), then(2)), 
                                                             c->c.is(when(instanceOf(RuntimeException.class)), then(2)),
                                                             otherwise(3));
        
//Eval.now(2)
Clone this wiki locally