Class XCompletionStageUtils

    • Method Detail

      • allOf

        @SafeVarargs
        public static <T> CompletionStage<List<T>> allOf​(CompletionStage<? extends T>... stages)
        Similar to CompletableFuture.allOf(), but grants immediate access to the individual values.

        Combines a list of stages into a single stage with the values from all stages. The returned stage is completed when all given stages are completed. The list contains all values in the order as the given stages. When a stage is null, the resulting list will contain a null value. When any stage completes exceptionally, the returned stage also completes exceptionally.

        Type Parameters:
        T - Type of the eventual result of the stages.
        Parameters:
        stages - Stages to await.
        Returns:
        A new stage that is completed with the list of all values when all of the given stages have completed.
      • allOf

        public static <T> CompletionStage<List<T>> allOf​(int chunkSize,
                                                         Iterable<? extends Supplier<? extends CompletionStage<? extends T>>> stageFactories)
        Similar to allOf(Iterable), but additional partitions the stages into chunks of a given size and processes the chunks in order. This is useful when you need some degree of parallelism, but do not want to start a potentially unlimited number of tasks all at once.

        Calls each given factory to obtain the corresponding stage, and returns a stage that completes with a list of values from all given stages, in the order as given, when all stages complete. In additional, this method ensures that the factories of a chunk are only called when all stages of the previous chunk have completed.

        When a stage is null, the resulting list will contain a null value. When any stage completes exceptionally, the returned stage also completes exceptionally.

         // Reads the data of the first 10 users, then the data of the next 10 users, etc.
         // If fetchUserData starts a network request, at most 10 networks request will be active at any given time
         var userData = allOf(10, users.stream().map(user -> () -> fetchUserData(user)));
         
        Type Parameters:
        T - Type of the eventual result of the stages.
        Parameters:
        chunkSize - Chunk size, must be positive.
        stageFactories - Stages to await.
        Returns:
        A new stage that is completed with the list of all values when all stages obtained from the given factories have completed.
      • allOf

        public static <T> CompletionStage<List<T>> allOf​(int chunkSize,
                                                         Stream<? extends Supplier<? extends CompletionStage<? extends T>>> stageFactories)
        Similar to allOf(Stream), but additional partitions the stages into chunks of a given size and processes the chunks in order. This is useful when you need some degree of parallelism, but do not want to start a potentially unlimited number of tasks all at once.

        Calls each given factory to obtain the corresponding stage, and returns a stage that completes with a list of values from all given stages, in the order as given, when all stages complete. In additional, this method ensures that the factories of a chunk are only called when all stages of the previous chunk have completed.

        When a stage is null, the resulting list will contain a null value. When any stage completes exceptionally, the returned stage also completes exceptionally.

         // Reads the data of the first 10 users, then the data of the next 10 users, etc.
         // If fetchUserData starts a network request, at most 10 networks request will be active at any given time
         var userData = allOf(10, users.stream().map(user -> () -> fetchUserData(user)));
         
        Type Parameters:
        T - Type of the eventual result of the stages.
        Parameters:
        chunkSize - Chunk size, must be positive.
        stageFactories - Stages to await.
        Returns:
        A new stage that is completed with the list of all values when all stages obtained from the given factories have completed.
      • allOf

        @SafeVarargs
        public static <T> CompletionStage<List<T>> allOf​(int chunkSize,
                                                         Supplier<? extends CompletionStage<? extends T>>... stageFactories)
        Similar to allOf(CompletionStage...), but additional partitions the stages into chunks of a given size and processes the chunks in order. This is useful when you need some degree of parallelism, but do not want to start a potentially unlimited number of tasks all at once.

        Calls each given factory to obtain the corresponding stage, and returns a stage that completes with a list of values from all given stages, in the order as given, when all stages complete. In additional, this method ensures that the factories of a chunk are only called when all stages of the previous chunk have completed.

        When a stage is null, the resulting list will contain a null value. When any stage completes exceptionally, the returned stage also completes exceptionally.

         // Reads the data of the first 10 users, then the data of the next 10 users, etc.
         // If fetchUserData starts a network request, at most 10 networks request will be active at any given time
         var userData = allOf(10, users.stream().map(user -> () -> fetchUserData(user)));
         
        Type Parameters:
        T - Type of the eventual result of the stages.
        Parameters:
        chunkSize - Chunk size, must be positive.
        stageFactories - Stages to await.
        Returns:
        A new stage that is completed with the list of all values when all stages obtained from the given factories have completed.
      • allOf

        public static <T> CompletionStage<List<T>> allOf​(Iterable<? extends CompletionStage<? extends T>> stages)
        Similar to CompletableFuture.allOf(), but grants immediate access to the individual values.

        Combines a list of stages into a single stage with the values from all stages. The returned stage is completed when all given stages are completed. The list contains all values in the order as the given stages. When a stage is null, the resulting list will contain a null value. When any stage completes exceptionally, the returned stage also completes exceptionally.

        Type Parameters:
        T - Type of the eventual result of the stages.
        Parameters:
        stages - Stages to await.
        Returns:
        A new stage that is completed with the list of all values when all of the given stages have completed.
      • allOf

        public static <T> CompletionStage<List<T>> allOf​(Stream<? extends CompletionStage<? extends T>> stages)
        Similar to CompletableFuture.allOf(), but grants immediate access to the individual values.

        Combines a list of stages into a single stage with the values from all stages. The returned stage is completed when all given stages are completed. The list contains all values in the order as the given stages. When a stage is null, the resulting list will contain a null value. When any stage completes exceptionally, the returned stage also completes exceptionally.

        Type Parameters:
        T - Type of the eventual result of the stages.
        Parameters:
        stages - Stages to await.
        Returns:
        A new stage that is completed with the list of all values when all of the given stages have completed.
      • fn

        public static <T,​R> Function<T,​R> fn​(XCompletionStageUtils.CheckedFunction<T,​R> fn)
        Converts a function that may throw a checked exception into a function that throws an unchecked CompletionException in that case.
        Type Parameters:
        T - Type of the function's argument.
        R - Type of the function's return value.
        Parameters:
        fn - The function which may throw a checked exception.
        Returns:
        A function that throws a CompletionException if the given function throws a checked exception.