Class CompletableFutureUtils


  • public final class CompletableFutureUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T,​S extends java.util.Collection<T>>
      java.util.concurrent.CompletableFuture<S>
      collectionOfFuturesToFutureOfCollection​(java.util.Collection<? extends java.util.concurrent.CompletionStage<T>> futures, java.util.stream.Collector<T,​?,​S> collector)
      Transforms a collection of CompletionStage into a CompletionStage of a collection, that will be completed once all the elements of the given futures are completed.
      static <T,​S,​U extends java.util.Collection<S>>
      java.util.concurrent.CompletableFuture<U>
      mapValuesToFutureOfCompletedValues​(java.util.Collection<T> values, java.util.function.Function<T,​java.util.concurrent.CompletionStage<S>> mapper, java.util.stream.Collector<S,​?,​U> collector)
      Creates a Future containing a collection of value results after the mapper function is applied to each value in the supplied collection and completed it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • mapValuesToFutureOfCompletedValues

        @Nonnull
        public static <T,​S,​U extends java.util.Collection<S>> java.util.concurrent.CompletableFuture<U> mapValuesToFutureOfCompletedValues​(@Nonnull
                                                                                                                                                       java.util.Collection<T> values,
                                                                                                                                                       @Nonnull
                                                                                                                                                       java.util.function.Function<T,​java.util.concurrent.CompletionStage<S>> mapper,
                                                                                                                                                       @Nonnull
                                                                                                                                                       java.util.stream.Collector<S,​?,​U> collector)
        Creates a Future containing a collection of value results after the mapper function is applied to each value in the supplied collection and completed it. The type of the returned collection is decided by the supplied collector.
        Type Parameters:
        T - The type of the values.
        S - The type of the mapped completed values.
        U - The type of the collection returned in the future.
        Parameters:
        values - collection of values to apply a mapper function that would map each to a CompletionStage.
        mapper - function to map each value to a CompletionStage
        collector - the collector to define the type of the collection returned.
        Returns:
        a future containing a collection of completed stage results of the values after the mapper function was applied to each value in the supplied collection.
      • collectionOfFuturesToFutureOfCollection

        @Nonnull
        public static <T,​S extends java.util.Collection<T>> java.util.concurrent.CompletableFuture<S> collectionOfFuturesToFutureOfCollection​(@Nonnull
                                                                                                                                                    java.util.Collection<? extends java.util.concurrent.CompletionStage<T>> futures,
                                                                                                                                                    @Nonnull
                                                                                                                                                    java.util.stream.Collector<T,​?,​S> collector)
        Transforms a collection of CompletionStage into a CompletionStage of a collection, that will be completed once all the elements of the given futures are completed. In case multiple stages end exceptionally only one error is kept. The type of the returned collection is decided by the supplied collector.

        Note: Null futures in the collection are filtered out.

        Type Parameters:
        T - the element obtained from the set of CompletionStage
        S - The type of the collection returned in the future.
        Parameters:
        futures - collection of CompletionStage
        collector - the collector to define the type of the collection returned.
        Returns:
        the CompletableFuture of a collection of elements