Skip to content

FutureW

johnmcclean-aol edited this page Nov 23, 2016 · 7 revisions

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.

Why?

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).

Stream

Stream

Optional

Optional

### CompletableFuture

CompletableFuture

FutureW

FutureW)

Features

Quorum support

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)));

First success

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)));

Accumulate Success

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)));

### Pattern matching

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

Applicative Functor

Combine values of any type asynchronously

FutureW.ofResult(10)
       .combine(Maybe.just(10),(a,b)->a+b);
//FutureW[20]
Clone this wiki locally