public class CompletableFutures
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
CompletableFutures.Instances
Companion class for creating Type Class instances for working with CompletableFutures
|
Constructor and Description |
---|
CompletableFutures() |
Modifier and Type | Method and Description |
---|---|
static <T,R> java.util.concurrent.CompletableFuture<R> |
accumulate(CollectionX<java.util.concurrent.CompletableFuture<T>> fts,
java.util.function.Function<? super T,R> mapper,
Monoid<R> reducer)
Asynchronously accumulate the results of a batch of Futures which using the supplied mapping function to
convert the data from each Future before reducing them using the supplied supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids . |
static <T,R> java.util.concurrent.CompletableFuture<R> |
accumulate(CollectionX<java.util.concurrent.CompletableFuture<T>> fts,
Reducer<R> reducer)
Asynchronously accumulate the results of Futures, a single failure will cause a failed result, using the supplied Reducer
cyclops2.Reducers |
static <T> java.util.concurrent.CompletableFuture<T> |
accumulate(Monoid<T> reducer,
CollectionX<java.util.concurrent.CompletableFuture<T>> fts)
Asynchronously accumulate the results only from the provided Futures,
reducing them using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids . |
static <T,R> java.util.concurrent.CompletableFuture<R> |
accumulateSuccess(CollectionX<java.util.concurrent.CompletableFuture<T>> fts,
java.util.function.Function<? super T,R> mapper,
Monoid<R> reducer)
Asynchronously accumulate the results only from those Futures which have completed successfully, using the supplied mapping function to
convert the data from each Future before reducing them using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids . |
static <T,R> java.util.concurrent.CompletableFuture<R> |
accumulateSuccess(CollectionX<java.util.concurrent.CompletableFuture<T>> fts,
Reducer<R> reducer)
Asynchronously accumulate the results only from those Futures which have completed successfully.
|
static <T,R> java.util.concurrent.CompletableFuture<T> |
accumulateSuccess(Monoid<T> reducer,
CollectionX<java.util.concurrent.CompletableFuture<T>> fts)
Asynchronously accumulate the results only from those Futures which have completed successfully,
reducing them using the supplied Monoid (a combining BiFunction/BinaryOperator and identity element that takes two
input values of the same type and returns the combined result)
cyclops2.Monoids |
static <T1,T2,R> java.util.concurrent.CompletableFuture<R> |
combine(java.util.concurrent.CompletableFuture<? extends T1> f,
java.util.concurrent.CompletableFuture<? extends T2> v,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine a CompletableFuture with the provided CompletableFuture asynchronously (if not completed) using the supplied BiFunction
|
static <T1,T2,R> java.util.concurrent.CompletableFuture<R> |
combine(java.util.concurrent.CompletableFuture<? extends T1> f,
Value<? extends T2> v,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine a CompletableFuture with the provided Value asynchronously (if not completed) using the supplied BiFunction
|
static <T,R1,R> java.util.concurrent.CompletableFuture<R> |
forEach2(java.util.concurrent.CompletableFuture<? extends T> value1,
java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<R1>> value2,
java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
Perform a For Comprehension over a CompletableFuture, accepting a generating function.
|
static <T1,T2,R1,R2,R> |
forEach3(java.util.concurrent.CompletableFuture<? extends T1> value1,
java.util.function.Function<? super T1,? extends java.util.concurrent.CompletableFuture<R1>> value2,
java.util.function.BiFunction<? super T1,? super R1,? extends java.util.concurrent.CompletableFuture<R2>> value3,
Fn3<? super T1,? super R1,? super R2,? extends R> yieldingFunction)
Perform a For Comprehension over a CompletableFuture, accepting 2 generating function.
|
static <T1,T2,T3,R1,R2,R3,R> |
forEach4(java.util.concurrent.CompletableFuture<? extends T1> value1,
java.util.function.Function<? super T1,? extends java.util.concurrent.CompletableFuture<R1>> value2,
java.util.function.BiFunction<? super T1,? super R1,? extends java.util.concurrent.CompletableFuture<R2>> value3,
Fn3<? super T1,? super R1,? super R2,? extends java.util.concurrent.CompletableFuture<R3>> value4,
Fn4<? super T1,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
Perform a For Comprehension over a CompletableFuture, accepting 3 generating function.
|
static <T> java.util.concurrent.CompletableFuture<T> |
narrow(java.util.concurrent.CompletableFuture<? extends T> f)
Narrow covariant type parameter
|
static <T> java.util.concurrent.CompletableFuture<T> |
schedule(long delay,
java.util.concurrent.ScheduledExecutorService ex,
java.util.function.Supplier<T> t)
Schedule the population of a CompletableFuture from the provided Supplier after the specified delay.
|
static <T> java.util.concurrent.CompletableFuture<T> |
schedule(java.lang.String cron,
java.util.concurrent.ScheduledExecutorService ex,
java.util.function.Supplier<T> t)
Schedule the population of a CompletableFuture from the provided Supplier, the provided Cron (Quartz format) expression will be used to
trigger the population of the CompletableFuture.
|
static <T> java.util.concurrent.CompletableFuture<ListX<T>> |
sequence(CollectionX<java.util.concurrent.CompletableFuture<T>> fts)
Asynchronous sequence operation that convert a Collection of Futures to a Future with a List
|
static <T> java.util.concurrent.CompletableFuture<ReactiveSeq<T>> |
sequence(java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> fts)
Asynchronous sequence operation that convert a Stream of FutureWs to a Future with a Stream
|
static <T1,T2,R> java.util.concurrent.CompletableFuture<R> |
zip(java.util.concurrent.CompletableFuture<? extends T1> f,
java.lang.Iterable<? extends T2> v,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine an CompletableFuture with the provided Iterable (selecting one element if present) using the supplied BiFunction
|
static <T1,T2,R> java.util.concurrent.CompletableFuture<R> |
zip(org.reactivestreams.Publisher<? extends T2> p,
java.util.concurrent.CompletableFuture<? extends T1> f,
java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
Combine an CompletableFuture with the provided Publisher (selecting one element if present) using the supplied BiFunction
|
public static <T1,T2,T3,R1,R2,R3,R> java.util.concurrent.CompletableFuture<R> forEach4(java.util.concurrent.CompletableFuture<? extends T1> value1, java.util.function.Function<? super T1,? extends java.util.concurrent.CompletableFuture<R1>> value2, java.util.function.BiFunction<? super T1,? super R1,? extends java.util.concurrent.CompletableFuture<R2>> value3, Fn3<? super T1,? super R1,? super R2,? extends java.util.concurrent.CompletableFuture<R3>> value4, Fn4<? super T1,? super R1,? super R2,? super R3,? extends R> yieldingFunction)
import static com.aol.cyclops2.reactor.CompletableFutures.forEach4;
forEach4(CompletableFuture.just(1),
a-> CompletableFuture.just(a+1),
(a,b) -> CompletableFuture.<Integer>just(a+b),
a (a,b,c) -> CompletableFuture.<Integer>just(a+b+c),
Tuple::tuple)
value1
- top level CompletableFuturevalue2
- Nested CompletableFuturevalue3
- Nested CompletableFuturevalue4
- Nested CompletableFutureyieldingFunction
- Generates a result per combinationpublic static <T1,T2,R1,R2,R> java.util.concurrent.CompletableFuture<R> forEach3(java.util.concurrent.CompletableFuture<? extends T1> value1, java.util.function.Function<? super T1,? extends java.util.concurrent.CompletableFuture<R1>> value2, java.util.function.BiFunction<? super T1,? super R1,? extends java.util.concurrent.CompletableFuture<R2>> value3, Fn3<? super T1,? super R1,? super R2,? extends R> yieldingFunction)
import static com.aol.cyclops2.reactor.CompletableFutures.forEach3;
forEach3(CompletableFuture.just(1),
a-> CompletableFuture.just(a+1),
(a,b) -> CompletableFuture.<Integer>just(a+b),
Tuple::tuple)
value1
- top level CompletableFuturevalue2
- Nested CompletableFuturevalue3
- Nested CompletableFutureyieldingFunction
- Generates a result per combinationpublic static <T,R1,R> java.util.concurrent.CompletableFuture<R> forEach2(java.util.concurrent.CompletableFuture<? extends T> value1, java.util.function.Function<? super T,java.util.concurrent.CompletableFuture<R1>> value2, java.util.function.BiFunction<? super T,? super R1,? extends R> yieldingFunction)
import static com.aol.cyclops2.reactor.CompletableFutures.forEach;
forEach(CompletableFuture.just(1),
a-> CompletableFuture.just(a+1),
Tuple::tuple)
value1
- top level CompletableFuturevalue2
- Nested CompletableFutureyieldingFunction
- Generates a result per combinationpublic static <T> java.util.concurrent.CompletableFuture<ListX<T>> sequence(CollectionX<java.util.concurrent.CompletableFuture<T>> fts)
CompletableFuture<ListX<Integer>> futures =CompletableFuture.sequence(ListX.of(
CompletableFuture.completedFuture(10),
CompletableFuture.completedFuture(1)));
//ListX.of(10,1)
fts
- Collection of Futures to Sequence into a Future with a Listpublic static <T> java.util.concurrent.CompletableFuture<ReactiveSeq<T>> sequence(java.util.stream.Stream<java.util.concurrent.CompletableFuture<T>> fts)
CompletableFuture<ListX<Integer>> futures =CompletableFuture.sequence(ListX.of(
CompletableFuture.completedFuture(10),
CompletableFuture.completedFuture(1)));
//ListX.of(10,1)
fts
- Stream of Futures to Sequence into a Future with a Streampublic static <T,R> java.util.concurrent.CompletableFuture<R> accumulateSuccess(CollectionX<java.util.concurrent.CompletableFuture<T>> fts, Reducer<R> reducer)
accumulate(CollectionX, Reducer)
if you would like a failure to result in a CompletableFuture
with an error
CompletableFuture<Integer> just = CompletableFuture.completedFuture(10);
CompletableFuture<Integer> none = Future.ofError(new NoSuchElementException())
.getFuture();
CompletableFuture<PSetX<Integer>> futures = CompletableFutures.accumulateSuccess(ListX.of(just,none,CompletableFuture.completedFuture(1)),Reducers.toPSetX());
//CompletableFuture[PSetX[10,1]]
fts
- Collection of Futures to accumulate successesreducer
- Reducer to accumulate resultspublic static <T,R> java.util.concurrent.CompletableFuture<R> accumulateSuccess(CollectionX<java.util.concurrent.CompletableFuture<T>> fts, java.util.function.Function<? super T,R> mapper, Monoid<R> reducer)
cyclops2.Monoids
.
CompletableFuture<String> future = CompletableFutures.accumulate(ListX.of(CompletableFuture.completedFuture(10),CompletableFuture.completedFuture(1)),i->""+i,Monoids.stringConcat);
//CompletableFuture["101"]
fts
- Collection of Futures to accumulate successesmapper
- Mapping function to be applied to the result of each Futurereducer
- Monoid to combine values from each Futurepublic static <T,R> java.util.concurrent.CompletableFuture<T> accumulateSuccess(Monoid<T> reducer, CollectionX<java.util.concurrent.CompletableFuture<T>> fts)
cyclops2.Monoids
CompletableFuture<Integer> just =CompletableFuture.completedFuture(10);
CompletableFuture<Integer> future =CompletableFutures.accumulate(Monoids.intSum, ListX.of(just,CompletableFuture.completedFuture(1)));
//CompletableFuture[11]
fts
- Collection of Futures to accumulate successesreducer
- Monoid to combine values from each Futurepublic static <T,R> java.util.concurrent.CompletableFuture<R> accumulate(CollectionX<java.util.concurrent.CompletableFuture<T>> fts, Reducer<R> reducer)
cyclops2.Reducers
CompletableFuture<Integer> just =CompletableFuture.completedFuture(10);
CompletableFuture<Integer> none = Future.ofError(new NoSuchElementException()).getFuture();
CompletableFuture<PSetX<Integer>> futures = CompletableFutures.accumulateSuccess(ListX.of(just,none,CompletableFuture.completedFuture(1)),Reducers.toPSetX());
//CompletableFuture[PSetX[10,1]]
fts
- Collection of Futures to accumulate successesreducer
- Reducer to accumulate resultspublic static <T,R> java.util.concurrent.CompletableFuture<R> accumulate(CollectionX<java.util.concurrent.CompletableFuture<T>> fts, java.util.function.Function<? super T,R> mapper, Monoid<R> reducer)
cyclops2.Monoids
.
A single Failure results in a Failed Future.
CompletableFuture<String> future = Future.accumulate(ListX.of(CompletableFuture.completedFuture(10),CompletableFuture.completedFuture(1)),i->""+i,Monoids.stringConcat);
//CompletableFuture["101"]
fts
- Collection of Futures to accumulate successesmapper
- Mapping function to be applied to the result of each Futurereducer
- Monoid to combine values from each Futurepublic static <T> java.util.concurrent.CompletableFuture<T> accumulate(Monoid<T> reducer, CollectionX<java.util.concurrent.CompletableFuture<T>> fts)
cyclops2.Monoids
.
A single Failure results in a Failed Future.
CompletableFuture<Integer> just =CompletableFuture.completedFuture(10);
CompletableFuture<Integer> future =CompletableFutures.accumulate(Monoids.intSum,ListX.of(just,CompletableFuture.completableFuture(1)));
//CompletableFuture[11]
fts
- Collection of Futures to accumulate successesreducer
- Monoid to combine values from each Futurepublic static <T> java.util.concurrent.CompletableFuture<T> schedule(java.lang.String cron, java.util.concurrent.ScheduledExecutorService ex, java.util.function.Supplier<T> t)
CompletableFuture<String> future = CompletableFutures.schedule("* * * * * ?", Executors.newScheduledThreadPool(1), ()->"hello");
//CompletableFuture["hello"]
cron
- Cron expression in Quartz formatex
- ScheduledExecutorService used to execute the provided Suppliert
- The Supplier to execute to populate the CompletableFuturepublic static <T> java.util.concurrent.CompletableFuture<T> schedule(long delay, java.util.concurrent.ScheduledExecutorService ex, java.util.function.Supplier<T> t)
CompletableFuture<String> future = CompletableFutures.schedule(10l, Executors.newScheduledThreadPool(1), ()->"hello");
//CompletableFuture["hello"]
delay
- Delay after which the CompletableFuture should be populatedex
- ScheduledExecutorService used to execute the provided Suppliert
- he Supplier to execute to populate the CompletableFuturepublic static <T1,T2,R> java.util.concurrent.CompletableFuture<R> combine(java.util.concurrent.CompletableFuture<? extends T1> f, Value<? extends T2> v, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
CompletableFutures.combine(CompletableFuture.completedFuture(10),Maybe.just(20), this::add)
//CompletableFuture [30]
private int add(int a, int b) {
return a + b;
}
f
- CompletableFuture to combine with a valuev
- Value to combine withfn
- Combining functionpublic static <T1,T2,R> java.util.concurrent.CompletableFuture<R> combine(java.util.concurrent.CompletableFuture<? extends T1> f, java.util.concurrent.CompletableFuture<? extends T2> v, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
CompletableFutures.combine(CompletableFuture.completedFuture(10),CompletableFuture.completedFuture(20), this::add)
//CompletableFuture [30]
private int add(int a, int b) {
return a + b;
}
f
- CompletableFuture to combine with a valuev
- Value to combine withfn
- Combining functionpublic static <T1,T2,R> java.util.concurrent.CompletableFuture<R> zip(java.util.concurrent.CompletableFuture<? extends T1> f, java.lang.Iterable<? extends T2> v, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
CompletableFutures.zip(CompletableFuture.completedFuture(10),Arrays.asList(20), this::add)
//CompletableFuture[30]
private int add(int a, int b) {
return a + b;
}
f
- CompletableFuture to combine with first element in Iterable (if present)v
- Iterable to combinefn
- Combining functionpublic static <T1,T2,R> java.util.concurrent.CompletableFuture<R> zip(org.reactivestreams.Publisher<? extends T2> p, java.util.concurrent.CompletableFuture<? extends T1> f, java.util.function.BiFunction<? super T1,? super T2,? extends R> fn)
CompletableFutures.zip(Flux.just(10),CompletableFuture.completedResult(10), this::add)
//CompletableFuture[30]
private int add(int a, int b) {
return a + b;
}
p
- Publisher to combinef
- CompletableFuture to combine withfn
- Combining functionpublic static <T> java.util.concurrent.CompletableFuture<T> narrow(java.util.concurrent.CompletableFuture<? extends T> f)
broad
- CompletableFuture with covariant type parameter