Class CompletableFutureUtils

    • Method Detail

      • failedFuture

        public static <U> CompletableFuture<U> failedFuture​(Throwable t)
        Convenience method for creating a future that is immediately completed exceptionally with the given Throwable.

        Similar to CompletableFuture#failedFuture which was added in Java 9.

        Type Parameters:
        U - The type of the element.
        Parameters:
        t - The failure.
        Returns:
        The failed future.
      • forwardExceptionTo

        public static <T> CompletableFuture<T> forwardExceptionTo​(CompletableFuture<T> src,
                                                                  CompletableFuture<?> dst)
        Forward the Throwable from src to dst.
        Parameters:
        src - The source of the Throwable.
        dst - The destination where the Throwable will be forwarded to.
        Returns:
        src.
      • forwardTransformedExceptionTo

        public static <T> CompletableFuture<T> forwardTransformedExceptionTo​(CompletableFuture<T> src,
                                                                             CompletableFuture<?> dst,
                                                                             Function<Throwable,​Throwable> transformationFunction)
        Forward the Throwable that can be transformed as per the transformationFunction from src to dst.
        Parameters:
        src - The source of the Throwable.
        dst - The destination where the Throwable will be forwarded to
        transformationFunction - Transformation function taht will be applied on to the forwarded exception.
        Returns:
      • forwardResultTo

        public static <T> CompletableFuture<T> forwardResultTo​(CompletableFuture<T> src,
                                                               CompletableFuture<T> dst)
        Completes the dst future based on the result of the src future asynchronously on the provided Executor and return the src future.
        Parameters:
        src - The source CompletableFuture
        dst - The destination where the Throwable or response will be forwarded to.
        Returns:
        the src future.
      • forwardResultTo

        public static <T> CompletableFuture<T> forwardResultTo​(CompletableFuture<T> src,
                                                               CompletableFuture<T> dst,
                                                               Executor executor)
        Completes the dst future based on the result of the src future asynchronously on the provided Executor and return the src future.
        Parameters:
        src - The source CompletableFuture
        dst - The destination where the Throwable or response will be forwarded to.
        executor - the executor to complete the des future
        Returns:
        the src future.
      • forwardTransformedResultTo

        public static <SourceT,​DestT> CompletableFuture<SourceT> forwardTransformedResultTo​(CompletableFuture<SourceT> src,
                                                                                                  CompletableFuture<DestT> dst,
                                                                                                  Function<SourceT,​DestT> function)
        Completes the dst future based on the result of the src future, synchronously, after applying the provided transformation Function if successful. If the function threw an exception, the destination future will be completed exceptionally with that exception.
        Parameters:
        src - The source CompletableFuture
        dst - The destination where the Throwable or transformed result will be forwarded to.
        Returns:
        the src future.
      • joinInterruptibly

        public static <T> T joinInterruptibly​(CompletableFuture<T> future)
      • joinInterruptiblyIgnoringFailures

        public static void joinInterruptiblyIgnoringFailures​(CompletableFuture<?> future)
      • joinLikeSync

        public static <T> T joinLikeSync​(CompletableFuture<T> future)
        Joins (interruptibly) on the future, and re-throws any RuntimeExceptions or Errors just like the async task would have thrown if it was executed synchronously.