Class CompletableFutureUtils

java.lang.Object
io.foldright.cffu.CompletableFutureUtils

@ParametersAreNonnullByDefault @ReturnValuesAreNonnullByDefault public final class CompletableFutureUtils extends Object
This class contains the enhanced and backport methods for CompletableFuture.
Author:
Jerry Lee (oldratlee at gmail dot com)
  • Method Details

    • allOf

      public static CompletableFuture<Void> allOf(CompletionStage<?>... cfs)
      Returns a new CompletableFuture that is completed when all the given stages complete. If any of the given stages complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. Otherwise, the results, if any, of the given stages are not reflected in the returned CompletableFuture, but may be obtained by inspecting them individually. If no stages are provided, returns a CompletableFuture completed with the value null.

      if you need the results of given stages, prefer below methods:

      This method is the same as CompletableFuture.allOf(CompletableFuture[]), except the parameter type is more generic(CompletionStage).

      Parameters:
      cfs - the stages
      Returns:
      a new CompletableFuture that is completed when all the given stages complete
      Throws:
      NullPointerException - if the array or any of its elements are null
    • allResultsOf

      @Contract(pure=true) @SafeVarargs public static <T> CompletableFuture<List<T>> allResultsOf(CompletionStage<? extends T>... cfs)
      Returns a new CompletableFuture with the results in the same order of all the given CompletableFutures, the new CompletableFuture is completed when all the given CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. If no CompletableFutures are provided, returns a CompletableFuture completed with the value empty list.

      This method is the same as CompletableFuture.allOf(CompletableFuture[]), except the returned CompletableFuture contains the results of the given CompletableFutures.

      Parameters:
      cfs - the CompletableFutures
      Returns:
      a new CompletableFuture that is completed when all the given CompletableFutures complete
      Throws:
      NullPointerException - if the array or any of its elements are null
      See Also:
    • allOfFastFail

      @Contract(pure=true) public static CompletableFuture<Void> allOfFastFail(CompletionStage<?>... cfs)
      Returns a new CompletableFuture that is successful when all the given CompletableFutures success, the results(CompletableFuture<Void>) of the given CompletableFutures are not reflected in the returned CompletableFuture, but may be obtained by inspecting them individually. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, with a CompletionException holding this exception as its cause. If no CompletableFutures are provided, returns a CompletableFuture completed with the value null.

      This method is the same as CompletableFuture.allOf(CompletableFuture[]) except for the fast-fail behavior.

      Parameters:
      cfs - the CompletableFutures
      Returns:
      a new CompletableFuture that is successful when all the given CompletableFutures success
      Throws:
      NullPointerException - if the array or any of its elements are null
      See Also:
    • allResultsOfFastFail

      @Contract(pure=true) @SafeVarargs public static <T> CompletableFuture<List<T>> allResultsOfFastFail(CompletionStage<? extends T>... cfs)
      Returns a new CompletableFuture with the results in the same order of all the given CompletableFutures, the new CompletableFuture success when all the given CompletableFutures success. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, with a CompletionException holding this exception as its cause. If no CompletableFutures are provided, returns a CompletableFuture completed with the value empty list.

      This method is the same as allOfFastFail(CompletionStage[]), except the returned CompletableFuture contains the results of the given CompletableFutures.

      This method is the same as allResultsOf(CompletionStage[]) except for the fast-fail behavior.

      Parameters:
      cfs - the CompletableFutures
      Returns:
      a new CompletableFuture that is successful when all the given CompletableFutures success
      Throws:
      NullPointerException - if the array or any of its elements are null
      See Also:
    • anyOf

      @Contract(pure=true) @SafeVarargs public static <T> CompletableFuture<T> anyOf(CompletionStage<? extends T>... cfs)
      Returns a new CompletableFuture that is completed when any of the given CompletableFutures(stages) complete, with the same result. Otherwise, if it completed exceptionally, the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause. If no CompletableFutures are provided, returns an incomplete CompletableFuture.

      This method is the same as CompletableFuture.anyOf(CompletableFuture[]), except the parameter type is more generic type CompletionStage. and the return result type is generic type instead of Object.

      Parameters:
      cfs - the CompletableFutures
      Returns:
      a new CompletableFuture that is completed with the result or exception from any of the given CompletableFutures when one completes
      Throws:
      NullPointerException - if the array or any of its elements are null
      See Also:
    • anyOfSuccess

      @Contract(pure=true) @SafeVarargs public static <T> CompletableFuture<T> anyOfSuccess(CompletionStage<? extends T>... cfs)
      Returns a new CompletableFuture that is successful when any of the given CompletableFutures success, with the same result. Otherwise, all the given CompletableFutures complete exceptionally, the returned CompletableFuture also does so, with a CompletionException holding an exception from any of the given CompletableFutures as its cause. If no CompletableFutures are provided, returns a new CompletableFuture that is already completed exceptionally with a CompletionException holding a NoCfsProvidedException as its cause.

      This method is the same as anyOf(CompletionStage[]) except for the any-success behavior(not any-complete).

      Parameters:
      cfs - the CompletableFutures
      Returns:
      a new CompletableFuture that is successful when any of the given CompletableFutures success, with the same result
      Throws:
      NullPointerException - if the array or any of its elements are null
      See Also:
    • allTupleOf

      @Contract(pure=true) public static <T1, T2> CompletableFuture<Tuple2<T1,T2>> allTupleOf(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2)
      Returns a new CompletableFuture that is completed when the given two CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause.
      Returns:
      a new CompletableFuture that is completed when the given 2 CompletableFutures complete
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOfFastFail

      @Contract(pure=true) public static <T1, T2> CompletableFuture<Tuple2<T1,T2>> allTupleOfFastFail(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2)
      Returns a new CompletableFuture that is successful when the given two CompletableFutures success. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, with a CompletionException holding this exception as its cause.

      This method is the same as allTupleOf(CompletionStage, CompletionStage) except for the fast-fail behavior.

      Returns:
      a new CompletableFuture that is successful when the given two CompletableFutures success
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOf

      @Contract(pure=true) public static <T1, T2, T3> CompletableFuture<Tuple3<T1,T2,T3>> allTupleOf(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3)
      Returns a new CompletableFuture that is completed when the given three CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause.
      Returns:
      a new CompletableFuture that is completed when the given 3 CompletableFutures complete
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOfFastFail

      @Contract(pure=true) public static <T1, T2, T3> CompletableFuture<Tuple3<T1,T2,T3>> allTupleOfFastFail(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3)
      Returns a new CompletableFuture that is successful when the given three CompletableFutures success. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, with a CompletionException holding this exception as its cause.

      This method is the same as allTupleOf(CompletionStage, CompletionStage, CompletionStage) except for the fast-fail behavior.

      Returns:
      a new CompletableFuture that is successful when the given three CompletableFutures success
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOf

      @Contract(pure=true) public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1,T2,T3,T4>> allTupleOf(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4)
      Returns a new CompletableFuture that is completed when the given 4 CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause.
      Returns:
      a new CompletableFuture that is completed when the given 4 CompletableFutures complete
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOfFastFail

      @Contract(pure=true) public static <T1, T2, T3, T4> CompletableFuture<Tuple4<T1,T2,T3,T4>> allTupleOfFastFail(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4)
      Returns a new CompletableFuture that is successful when the given 4 CompletableFutures success. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, with a CompletionException holding this exception as its cause.

      This method is the same as allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage) except for the fast-fail behavior.

      Returns:
      a new CompletableFuture that is successful when the given 4 CompletableFutures success
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOf

      @Contract(pure=true) public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1,T2,T3,T4,T5>> allTupleOf(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5)
      Returns a new CompletableFuture that is completed when the given 5 CompletableFutures complete. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so, with a CompletionException holding this exception as its cause.
      Returns:
      a new CompletableFuture that is completed when the given 5 CompletableFutures complete
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • allTupleOfFastFail

      @Contract(pure=true) public static <T1, T2, T3, T4, T5> CompletableFuture<Tuple5<T1,T2,T3,T4,T5>> allTupleOfFastFail(CompletionStage<? extends T1> cf1, CompletionStage<? extends T2> cf2, CompletionStage<? extends T3> cf3, CompletionStage<? extends T4> cf4, CompletionStage<? extends T5> cf5)
      Returns a new CompletableFuture that is successful when the given 5 CompletableFutures success. If any of the given CompletableFutures complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletableFutures, with a CompletionException holding this exception as its cause.

      This method is the same as allTupleOf(CompletionStage, CompletionStage, CompletionStage, CompletionStage, CompletionStage) except for the fast-fail behavior.

      Returns:
      a new CompletableFuture that is successful when the given 5 CompletableFutures success
      Throws:
      NullPointerException - if any of the given CompletableFutures are null
      See Also:
    • runAfterBothFastFail

      public static CompletableFuture<Void> runAfterBothFastFail(CompletionStage<?> cf1, CompletionStage<?> cf2, Runnable action)
      Returns a new CompletableFuture that, when two given stages both complete normally, executes the given action. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.runAfterBoth(CompletionStage, Runnable) except for the fast-fail behavior.

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • runAfterBothFastFailAsync

      public static CompletableFuture<Void> runAfterBothFastFailAsync(CompletionStage<?> cf1, CompletionStage<?> cf2, Runnable action)
      Returns a new CompletableFuture that, when two given stages both complete normally, executes the given action using CompletableFuture's default asynchronous execution facility. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.runAfterBothAsync(CompletionStage, Runnable) except for the fast-fail behavior.

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • runAfterBothFastFailAsync

      public static CompletableFuture<Void> runAfterBothFastFailAsync(CompletionStage<?> cf1, CompletionStage<?> cf2, Runnable action, Executor executor)
      Returns a new CompletableFuture that, when two given stages both complete normally, executes the given action using the supplied executor. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.runAfterBothAsync(CompletionStage, Runnable, Executor) except for the fast-fail behavior.

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • thenAcceptBothFastFail

      public static <T, U> CompletableFuture<Void> thenAcceptBothFastFail(CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2, BiConsumer<? super T,? super U> action)
      Returns a new CompletableFuture that, when tow given stage both complete normally, is executed with the two results as arguments to the supplied action. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.thenAcceptBoth(CompletionStage, BiConsumer) except for the fast-fail behavior.

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • thenAcceptBothFastFailAsync

      public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2, BiConsumer<? super T,? super U> action)
      Returns a new CompletableFuture that, when tow given stage both complete normally, is executed using CompletableFuture's default asynchronous execution facility, with the two results as arguments to the supplied action. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.thenAcceptBothAsync(CompletionStage, BiConsumer) except for the fast-fail behavior.

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • thenAcceptBothFastFailAsync

      public static <T, U> CompletableFuture<Void> thenAcceptBothFastFailAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2, BiConsumer<? super T,? super U> action, Executor executor)
      Returns a new CompletableFuture that, when tow given stage both complete normally, is executed using the supplied executor, with the two results as arguments to the supplied action. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.thenAcceptBothAsync(CompletionStage, BiConsumer, Executor) except for the fast-fail behavior.

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • thenCombineFastFail

      public static <T, U, V> CompletableFuture<V> thenCombineFastFail(CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2, BiFunction<? super T,? super U,? extends V> fn)
      Returns a new CompletableFuture that, when tow given stage both complete normally, is executed with the two results as arguments to the supplied function. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.thenCombine(CompletionStage, BiFunction) except for the fast-fail behavior.

      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • thenCombineFastFailAsync

      public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2, BiFunction<? super T,? super U,? extends V> fn)
      Returns a new CompletableFuture that, when tow given stage both complete normally, is executed using CompletableFuture's default asynchronous execution facility, with the two results as arguments to the supplied function. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.thenCombineAsync(CompletionStage, BiFunction) except for the fast-fail behavior.

      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • thenCombineFastFailAsync

      public static <T, U, V> CompletableFuture<V> thenCombineFastFailAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends U> cf2, BiFunction<? super T,? super U,? extends V> fn, Executor executor)
      Returns a new CompletableFuture that, when tow given stage both complete normally, is executed using the supplied executor, with the two results as arguments to the supplied function. if any of the given stage complete exceptionally, then the returned CompletableFuture also does so *without* waiting other incomplete given CompletionStage, with a CompletionException holding this exception as its cause.

      This method is the same as CompletionStage.thenCombineAsync(CompletionStage, BiFunction, Executor) except for the fast-fail behavior.

      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • runAfterEitherSuccess

      public static CompletableFuture<Void> runAfterEitherSuccess(CompletionStage<?> cf1, CompletionStage<?> cf2, Runnable action)
      Returns a new CompletableFuture that, when either given stage success, executes the given action. Otherwise, all two given CompletionStage complete exceptionally, the returned CompletableFuture also does so, with a CompletionException holding an exception from any of the given CompletionStage as its cause.

      This method is the same as CompletableFuture.runAfterEither(CompletionStage, Runnable) except for the either-success behavior(not either-complete).

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • runAfterEitherSuccessAsync

      public static CompletableFuture<Void> runAfterEitherSuccessAsync(CompletionStage<?> cf1, CompletionStage<?> cf2, Runnable action)
      Returns a new CompletableFuture that, when either given stage success, executes the given action using CompletableFuture's default asynchronous execution facility. Otherwise, all two given CompletionStage complete exceptionally, the returned CompletableFuture also does so, with a CompletionException holding an exception from any of the given CompletionStage as its cause.

      This method is the same as CompletionStage.runAfterEitherAsync(CompletionStage, Runnable) except for the either-success behavior(not either-complete).

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • runAfterEitherSuccessAsync

      public static CompletableFuture<Void> runAfterEitherSuccessAsync(CompletionStage<?> cf1, CompletionStage<?> cf2, Runnable action, Executor executor)
      Returns a new CompletableFuture that, when either given stage success, executes the given action using the supplied executor. Otherwise, all two given CompletionStage complete exceptionally, the returned CompletableFuture also does so, with a CompletionException holding an exception from any of the given CompletionStage as its cause.

      This method is the same as CompletionStage.runAfterEitherAsync(CompletionStage, Runnable, Executor) except for the either-success behavior(not either-complete).

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • acceptEitherSuccess

      public static <T> CompletableFuture<Void> acceptEitherSuccess(CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Consumer<? super T> action)
      Returns a new CompletableFuture that, when either given stage success, is executed with the corresponding result as argument to the supplied action.

      This method is the same as CompletionStage.acceptEither(CompletionStage, Consumer) except for the either-success behavior(not either-complete).

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • acceptEitherSuccessAsync

      public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Consumer<? super T> action)
      Returns a new CompletionStage that, when either given stage success, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied action.

      This method is the same as CompletionStage.acceptEitherAsync(CompletionStage, Consumer) except for the either-success behavior(not either-complete).

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • acceptEitherSuccessAsync

      public static <T> CompletableFuture<Void> acceptEitherSuccessAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Consumer<? super T> action, Executor executor)
      Returns a new CompletionStage that, when either given stage success, is executed using the supplied executor, with the corresponding result as argument to the supplied action.

      This method is the same as CompletionStage.acceptEitherAsync(CompletionStage, Consumer, Executor) except for the either-success behavior(not either-complete).

      Parameters:
      action - the action to perform before completing the returned CompletableFuture
      executor - the executor to use for asynchronous execution
      Returns:
      the new CompletableFuture
      See Also:
    • applyToEitherSuccess

      public static <T, U> CompletableFuture<U> applyToEitherSuccess(CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Function<? super T,? extends U> fn)
      Returns a new CompletionStage that, when either given stage success, is executed with the corresponding result as argument to the supplied function.

      This method is the same as CompletionStage.applyToEither(CompletionStage, Function) except for the either-success behavior(not either-complete).

      Type Parameters:
      U - the function's return type
      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • applyToEitherSuccessAsync

      public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Function<? super T,? extends U> fn)
      Returns a new CompletionStage that, when either given stage success, is executed using this stage's default asynchronous execution facility, with the corresponding result as argument to the supplied function.

      This method is the same as CompletionStage.applyToEitherAsync(CompletionStage, Function) except for the either-success behavior(not either-complete).

      Type Parameters:
      U - the function's return type
      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture
      Returns:
      the new CompletableFuture
      See Also:
    • applyToEitherSuccessAsync

      public static <T, U> CompletableFuture<U> applyToEitherSuccessAsync(CompletionStage<? extends T> cf1, CompletionStage<? extends T> cf2, Function<? super T,? extends U> fn, Executor executor)
      Returns a new CompletionStage that, when either given stage success, is executed using the supplied executor, with the corresponding result as argument to the supplied function.

      This method is the same as CompletionStage.applyToEitherAsync(CompletionStage, Function, Executor) except for the either-success behavior(not either-complete).

      Type Parameters:
      U - the function's return type
      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture
      executor - the executor to use for asynchronous execution
      Returns:
      the new CompletableFuture
      See Also:
    • peek

      public static <T, C extends CompletionStage<? extends T>> C peek(C cf, BiConsumer<? super T,? super Throwable> action)
      Peeks the result by executing the given action when given stage completes, returns the given stage.

      When the given stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of given stage as arguments. Whether the supplied action throws an exception or not, do NOT affect this cffu.

      Unlike method handle and like method whenComplete, this method is not designed to translate completion outcomes.

      Parameters:
      action - the action to perform
      Returns:
      the given stage
      See Also:
    • peekAsync

      public static <T, C extends CompletionStage<? extends T>> C peekAsync(C cf, BiConsumer<? super T,? super Throwable> action)
      Peeks the result by executing the given action when given stage completes, executes the given action using given stage's default asynchronous execution facility, returns the given stage.

      When the given stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of given stage as arguments. Whether the supplied action throws an exception or not, do NOT affect this cffu.

      Unlike method handle and like method whenComplete, this method is not designed to translate completion outcomes.

      Parameters:
      action - the action to perform
      Returns:
      the given stage
      See Also:
    • peekAsync

      public static <T, C extends CompletionStage<? extends T>> C peekAsync(C cf, BiConsumer<? super T,? super Throwable> action, Executor executor)
      Peeks the result by executing the given action when given stage completes, executes the given action using the supplied Executor, returns the given stage.

      When the given stage is complete, the given action is invoked with the result (or null if none) and the exception (or null if none) of given stage as arguments. Whether the supplied action throws an exception or not, do NOT affect this cffu.

      Unlike method handle and like method whenComplete, this method is not designed to translate completion outcomes.

      Parameters:
      action - the action to perform
      Returns:
      the given stage
      See Also:
    • failedFuture

      @Contract(pure=true) public static <T> CompletableFuture<T> failedFuture(Throwable ex)
      Returns a new CompletableFuture that is already completed exceptionally with the given exception.
      Type Parameters:
      T - the type of the value
      Parameters:
      ex - the exception
      Returns:
      the exceptionally completed CompletableFuture
    • completedStage

      @Contract(pure=true) public static <T> CompletionStage<T> completedStage(@Nullable T value)
      Returns a new CompletionStage that is already completed with the given value and supports only those methods in interface CompletionStage.

      CAUTION:
      if run on old Java 8, just return a *normal* CompletableFuture which is NOT with a *minimal* CompletionStage.

      Type Parameters:
      T - the type of the value
      Parameters:
      value - the value
      Returns:
      the completed CompletionStage
    • failedStage

      @Contract(pure=true) public static <T> CompletionStage<T> failedStage(Throwable ex)
      Returns a new CompletionStage that is already completed exceptionally with the given exception and supports only those methods in interface CompletionStage.

      CAUTION:
      if run on old Java 8, just return a *normal* CompletableFuture which is NOT with a *minimal* CompletionStage.

      Type Parameters:
      T - the type of the value
      Parameters:
      ex - the exception
      Returns:
      the exceptionally completed CompletionStage
    • delayedExecutor

      @Contract(pure=true) public static Executor delayedExecutor(long delay, TimeUnit unit)
      Returns a new Executor that submits a task to the default executor after the given delay (or no delay if non-positive). Each delay commences upon invocation of the returned executor's execute method.
      Parameters:
      delay - how long to delay, in units of unit
      unit - a TimeUnit determining how to interpret the delay parameter
      Returns:
      the new delayed executor
    • delayedExecutor

      @Contract(pure=true) public static Executor delayedExecutor(long delay, TimeUnit unit, Executor executor)
      Returns a new Executor that submits a task to the given base executor after the given delay (or no delay if non-positive). Each delay commences upon invocation of the returned executor's execute method.
      Parameters:
      delay - how long to delay, in units of unit
      unit - a TimeUnit determining how to interpret the delay parameter
      executor - the base executor
      Returns:
      the new delayed executor
    • exceptionallyAsync

      public static <T> CompletableFuture<T> exceptionallyAsync(CompletableFuture<T> cf, Function<Throwable,? extends T> fn)
      Returns a new CompletionStage that, when given stage completes exceptionally, is executed with given stage's exception as the argument to the supplied function, using given stage's default asynchronous execution facility. Otherwise, if given stage completes normally, then the returned stage also completes normally with the same value.
      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture if given CompletionStage completed exceptionally
      Returns:
      the new CompletableFuture
    • exceptionallyAsync

      public static <T> CompletableFuture<T> exceptionallyAsync(CompletableFuture<T> cf, Function<Throwable,? extends T> fn, Executor executor)
      Returns a new CompletionStage that, when given stage completes exceptionally, is executed with given stage's exception as the argument to the supplied function, using the supplied Executor. Otherwise, if given stage completes normally, then the returned stage also completes normally with the same value.
      Parameters:
      fn - the function to use to compute the value of the returned CompletableFuture if given CompletionStage completed exceptionally
      executor - the executor to use for asynchronous execution
      Returns:
      the new CompletableFuture
    • orTimeout

      public static <C extends CompletableFuture<?>> C orTimeout(C cf, long timeout, TimeUnit unit)
      Exceptionally completes given CompletableFuture with a TimeoutException if not otherwise completed before the given timeout.
      Parameters:
      timeout - how long to wait before completing exceptionally with a TimeoutException, in units of unit
      unit - a TimeUnit determining how to interpret the timeout parameter
      Returns:
      given CompletableFuture
    • completeOnTimeout

      public static <T, C extends CompletableFuture<? super T>> C completeOnTimeout(C cf, @Nullable T value, long timeout, TimeUnit unit)
      Completes given CompletableFuture with the given value if not otherwise completed before the given timeout.
      Parameters:
      value - the value to use upon timeout
      timeout - how long to wait before completing normally with the given value, in units of unit
      unit - a TimeUnit determining how to interpret the timeout parameter
      Returns:
      given CompletableFuture
    • exceptionallyCompose

      public static <T> CompletableFuture<T> exceptionallyCompose(CompletableFuture<T> cf, Function<Throwable,? extends CompletionStage<T>> fn)
      Returns a new CompletionStage that, when given stage completes exceptionally, is composed using the results of the supplied function applied to given stage's exception.
      Parameters:
      fn - the function to use to compute the returned CompletableFuture if given CompletionStage completed exceptionally
      Returns:
      the new CompletableFuture
    • exceptionallyComposeAsync

      public static <T> CompletableFuture<T> exceptionallyComposeAsync(CompletableFuture<T> cf, Function<Throwable,? extends CompletionStage<T>> fn)
      Returns a new CompletionStage that, when given stage completes exceptionally, is composed using the results of the supplied function applied to given stage's exception, using given stage's default asynchronous execution facility.
      Parameters:
      fn - the function to use to compute the returned CompletableFuture if given CompletionStage completed exceptionally
      Returns:
      the new CompletableFuture
    • exceptionallyComposeAsync

      public static <T> CompletableFuture<T> exceptionallyComposeAsync(CompletableFuture<T> cf, Function<Throwable,? extends CompletionStage<T>> fn, Executor executor)
      Returns a new CompletionStage that, when given stage completes exceptionally, is composed using the results of the supplied function applied to given stage's exception, using the supplied Executor.
      Parameters:
      fn - the function to use to compute the returned CompletableFuture if given CompletionStage completed exceptionally
      executor - the executor to use for asynchronous execution
      Returns:
      the new CompletableFuture
    • join

      @Blocking @Nullable public static <T> T join(CompletableFuture<T> cf, long timeout, TimeUnit unit)
      Waits if necessary for at most the given time for the computation to complete, and then retrieves its result value when complete, or throws an (unchecked) exception if completed exceptionally.

      NOTE:
      Calling this method

      result = CompletableFutureUtils.join(cf, timeout, unit);

      is the same as:

      result = cf.copy() // defensive copy to avoid writing this cf unexpectedly
           .orTimeout(timeout, unit)
           .join();
       
      CAUTION:
      if the wait timed out, this method throws an (unchecked) CompletionException with the TimeoutException as its cause; NOT throws a (checked) TimeoutException like CompletableFuture.get(long, TimeUnit).
      Parameters:
      timeout - the maximum time to wait
      unit - the time unit of the timeout argument
      Returns:
      the result value
      Throws:
      CancellationException - if the computation was cancelled
      CompletionException - if given future completed exceptionally or a completion computation threw an exception or the wait timed out(with the TimeoutException as its cause)
      See Also:
    • resultNow

      @Contract(pure=true) @Nullable public static <T> T resultNow(CompletableFuture<? extends T> cf)
      Returns the computed result, without waiting.

      This method is for cases where the caller knows that the task has already completed successfully, for example when filtering a stream of Future objects for the successful tasks and using a mapping operation to obtain a stream of results.

      results = futures.stream()
           .filter(f -> f.state() == Future.State.SUCCESS)
           .map(Future::resultNow)
           .toList();
       
    • exceptionNow

      @Contract(pure=true) public static Throwable exceptionNow(CompletableFuture<?> cf)
      Returns the exception thrown by the task, without waiting.

      This method is for cases where the caller knows that the task has already completed with an exception.

      Returns:
      the exception thrown by the task
      Throws:
      IllegalStateException - if the task has not completed, the task completed normally, or the task was cancelled
      See Also:
    • state

      @Contract(pure=true) public static CffuState state(CompletableFuture<?> cf)
      Returns the computation state(CffuState), this method is equivalent to CompletableFuture.state() with java version compatibility logic, so you can invoke in old java 18-.
      Returns:
      the computation state
      See Also:
    • completeAsync

      public static <T, C extends CompletableFuture<? super T>> C completeAsync(C cf, Supplier<? extends T> supplier)
      Completes given CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the default executor.
      Parameters:
      supplier - a function returning the value to be used to complete given CompletableFuture
      Returns:
      given CompletableFuture
    • completeAsync

      public static <T, C extends CompletableFuture<? super T>> C completeAsync(C cf, Supplier<? extends T> supplier, Executor executor)
      Completes given CompletableFuture with the result of the given Supplier function invoked from an asynchronous task using the given executor.
      Parameters:
      supplier - a function returning the value to be used to complete given CompletableFuture
      executor - the executor to use for asynchronous execution
      Returns:
      given CompletableFuture
    • minimalCompletionStage

      @Contract(pure=true) public static <T> CompletionStage<T> minimalCompletionStage(CompletableFuture<T> cf)
      Returns a new CompletionStage that is completed normally with the same value as given CompletableFuture when it completes normally, and cannot be independently completed or otherwise used in ways not defined by the methods of interface CompletionStage. If given CompletableFuture completes exceptionally, then the returned CompletionStage completes exceptionally with a CompletionException with given exception as cause.

      CAUTION:
      if run on old Java 8, just return a *normal* CompletableFuture which is NOT with a *minimal* CompletionStage.

      Returns:
      the new CompletionStage
    • copy

      @Contract(pure=true) public static <T> CompletableFuture<T> copy(CompletableFuture<T> cf)
      Returns a new CompletableFuture that is completed normally with the same value as this CompletableFuture when it completes normally. If this CompletableFuture completes exceptionally, then the returned CompletableFuture completes exceptionally with a CompletionException with this exception as cause. The behavior is equivalent to thenApply(x -> x). This method may be useful as a form of "defensive copying", to prevent clients from completing, while still being able to arrange dependent actions.
      Returns:
      the new CompletableFuture
    • newIncompleteFuture

      @Contract(pure=true) public static <U> CompletableFuture<U> newIncompleteFuture(CompletableFuture<?> cf)
      Returns a new incomplete CompletableFuture of the type to be returned by a CompletionStage method.
      Type Parameters:
      U - the type of the value
      Returns:
      a new CompletableFuture
    • defaultExecutor

      @Contract(pure=true) public static Executor defaultExecutor()
      Returns the default Executor used for async methods that do not specify an Executor. This class uses the ForkJoinPool.commonPool() if it supports more than one parallel thread, or else an Executor using one thread per async task.
      CAUTION: This executor may be not suitable for common biz use(io intensive).
      Returns:
      the executor
    • toCompletableFutureArray

      @Contract(pure=true) @SafeVarargs public static <T> CompletableFuture<T>[] toCompletableFutureArray(CompletionStage<T>... stages)
      A convenient util method for converting input CompletionStage (including Cffu/CompletableFuture) array element by CompletionStage.toCompletableFuture().
      See Also:
    • completableFutureListToArray

      @Contract(pure=true) public static <T> CompletableFuture<T>[] completableFutureListToArray(List<CompletableFuture<T>> cfList)
      A convenient util method for converting input CompletableFuture list to CompletableFuture array.
      See Also: