-
Notifications
You must be signed in to change notification settings - Fork 136
FutureW
FutureW is a wrapper around CompletableFuture to give it a more standard API. FutureW extends the following cyclops types (and others) ApplicativeFunctor, Filterable, Foldable, Functor, MonadicValue1, To, Value,Visitable and Zippable.
CompletableFuture has a non-standard API, FutureW provides a more standard API and implements the same interfaces as other non-scalar control types such as Maybe or Eval (including Functor, MonadicValue, ApplicativeFunctor). See cyclops-react Type Interfaces here for more details.
Stream and Optional use map / flatMap but CompletableFuture uses thenApply and thenCompose. We provide common interfaces so you can abstract across sequences (Streams), optionality (Optional), asynchronousity (Futures).
### CompletableFuture
)
Return a List of results of a quorum of FutureWs
FutureW<Integer> one = FutureW.ofSupplier(()->1);
FutureW<Integer> two = FutureW.ofSupplier(()->2);
FutureW<Integer> three = FutureW.future(); //started but not complete
FutureW<ListX<Integer>> strings = FutureW.quorum(status -> status.getCompleted() > 1,
,one,two,three);
assertThat(strings.get().size(), is(greaterThan(1)));
FutureW<Integer> ft = FutureW.future();
FutureW<Integer> ft = FutureW.ofError(new NoSuchElementException());
FutureW<Integer> result = FutureW.firstSuccess(FutureW.ofSupplier(()->1),ft);
ft.complete(10);
assertThat(result.get(), is(equalTo(1)));
FutureW<Integer> just = FutureW.ofResult(10);
FutureW<Integer> none = FutureW.ofError(new NoSuchElementException());
FutureW<PSetX<Integer>> futures = FutureW.accumulateSuccess(ListX.of(just,none,FutureW.ofResult(1)),Reducers.toPSetX());
assertThat(futures.get(),equalTo(PSetX.of(10,1)));
Recover orElse and coflatMap can be used to manage FutureW's that aren't present and provide a default value.
FutureW.ofResult(10)
.recover(-1); //FutureW[10]
FutureW.ofError(new RuntimeException())
.recover(-1); //FutureW[-1]
int result = FutureW.ofError(new RuntimeException())
.orElse(-1); //-1
FutureW.ofError(new RuntimeException())
.coflatMap(ft->ft.visit(v->v,e->-1).get()); //FutureW[-1]
### Pattern matching
Async pattern matching (v1.1.0 of cyclops-react) In cyclops we can use the visit operator to pattern match on the state the FutureW is in.
FutureW.ofResult(10)
.visit(success->success+5,error->-1) // visit returns a FutureW for continued processing
.map(i->i*2);
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;
FutureW.ofResult(10).matches(c->c.is(when(10),then("hello")),
c->c.is(when(instanceOf(Throwable.class)), then("error")),
otherwise("miss"));
//FutureW["hello"]
FutureW.ofResult(10).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"));
//FutureW["hello"]
FutureW.ofResult(10).matches(c->c.is(when(1),then("hello"))
.is(when(2),then(()->"hello"))
.is(when(3),then(()->"hello")),
c->c.is(when(Predicates.instanceOf(Throwable.class)), then("error")),
otherwise("miss"));
//FutureW["miss"]
Combine values of any type asynchronously
FutureW.ofResult(10)
.combine(Maybe.just(10),(a,b)->a+b);
//FutureW[20]
oops - my bad