public class Combinators extends Object
CompletionStage into a single
CompletionStage| Modifier and Type | Method | Description |
|---|---|---|
static CompletionStage<Void> |
allOf(Collection<? extends CompletionStage<?>> stages) |
Given a collection of stages, returns a new
CompletionStage that is completed when all
input stages are complete. |
static <T> CompletionStage<Collection<T>> |
collect(Collection<? extends CompletionStage<T>> stages) |
Given a collection of stages all of the same type, returns a new
CompletionStage that
is completed with a collection of the results of all input stages when all stages complete. |
static <T,A,R> CompletionStage<R> |
collect(Collection<? extends CompletionStage<T>> stages,
Collector<? super T,A,R> collector) |
Applies a collector to the results of all
stages after all complete, returning a
CompletionStage of the collected result. |
static <K,V> CompletionStage<Map<K,V>> |
keyedAll(Map<K,? extends CompletionStage<V>> stageMap) |
Given a Map from some key type K to
CompletionStages of values, returns
a CompletionStage which completes with a Map<K, V> when all the
CompletionStages in the input map have completed. |
public static CompletionStage<Void> allOf(Collection<? extends CompletionStage<?>> stages)
CompletionStage that is completed when all
input stages are complete. If any stage completes exceptionally, the returned stage will
complete exceptionally.stages - a Collection of CompletionStageCompletionStage which will complete after every stage in stages
completesNullPointerException - if stages or any of its elements are nullpublic static <T> CompletionStage<Collection<T>> collect(Collection<? extends CompletionStage<T>> stages)
CompletionStage that
is completed with a collection of the results of all input stages when all stages complete. If
the input collection has a defined order, the order will be preserved in the returned
collection. If an element of stages completes exceptionally, so too will the
CompletionStage returned by this method.stages - a Collection of CompletionStage all of type TCompletionStage which will complete with a collection of the elements
produced by stages when all stages completeNullPointerException - if stages or any of its elements are nullpublic static <T,A,R> CompletionStage<R> collect(Collection<? extends CompletionStage<T>> stages, Collector<? super T,A,R> collector)
stages after all complete, returning a
CompletionStage of the collected result. There is no need nor benefit for the Collector
to have the CONCURRENT characteristic, the
collector will be applied in a single thread. If any of the input stages completes
exceptionally, so too will the CompletionStage returned by this method.T - The type of the elements in stages which will be collected by
collectorA - The intermediate collection typeR - The final type returned by collectorstages - a Collection of stages all of type Tcollector - a Collector which will be applied to the results of stages to
produce the final R result.CompletionStage which will complete with the R typed object that is produced
by collector when all input stages have completed.NullPointerException - if stages or any of its elements are nullpublic static <K,V> CompletionStage<Map<K,V>> keyedAll(Map<K,? extends CompletionStage<V>> stageMap)
CompletionStages of values, returns
a CompletionStage which completes with a Map<K, V> when all the
CompletionStages in the input map have completed. For example, if we have an asynchronous
method to lookup student grade point averages.
Map<Student, CompletionStage<Double>> gpaFutures =
students
.stream()
.collect(Collectors.toMap(Functions.identity(), student -> getGpaAsync(student));
Map<Student, Double> studentGpas = keyedAll(gpaFutures).toCompletableFuture().join();
If a value in stageMap completes exceptionally, so too will the CompletionStage
returned by this method.K - the input and output key typeV - the value type for the map that will be produced by the returned
CompletionStagestageMap - a Map with keys of type K and CompletionStages of type
V for valuesCompletionStage that will be completed with a map mapping keys of type K to
the values returned by the CompletionStages in stageMapNullPointerException - if stageMap or any of its values are nullCopyright © 2018. All rights reserved.