Class CompletableFuturesExtra


  • public class CompletableFuturesExtra
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> void checkCompleted​(java.util.concurrent.CompletionStage<T> stage)
      check that a stage is completed.
      static <T> java.util.concurrent.CompletionStage<T> dereference​(java.util.concurrent.CompletionStage<? extends java.util.concurrent.CompletionStage<T>> stage)
      This takes a stage of a stage of a value and returns a plain stage of a value.
      static <T> java.util.concurrent.CompletableFuture<T> exceptionallyCompletedFuture​(java.lang.Throwable throwable)
      Returns a new CompletableFuture that is already exceptionally completed with the given exception.
      static <T> java.util.concurrent.CompletionStage<T> exceptionallyCompose​(java.util.concurrent.CompletionStage<T> stage, java.util.function.Function<java.lang.Throwable,​? extends java.util.concurrent.CompletionStage<T>> fn)
      Returns a new stage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function.
      static <T> T getCompleted​(java.util.concurrent.CompletionStage<T> stage)
      Get the value of a completed stage.
      static <T,​U>
      java.util.concurrent.CompletionStage<U>
      handleCompose​(java.util.concurrent.CompletionStage<T> stage, java.util.function.BiFunction<? super T,​java.lang.Throwable,​? extends java.util.concurrent.CompletionStage<U>> fn)
      Returns a new stage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function.
      static <V> com.google.api.core.ApiFuture<V> toApiFuture​(java.util.concurrent.CompletionStage<V> future)
      Wrap a CompletionStage in a ApiFuture.
      static <V> java.util.concurrent.CompletableFuture<V> toCompletableFuture​(com.google.api.core.ApiFuture<V> future)
      static <V> java.util.concurrent.CompletableFuture<V> toCompletableFuture​(com.google.api.core.ApiFuture<V> future, java.util.concurrent.Executor executor)
      static <V> java.util.concurrent.CompletableFuture<V> toCompletableFuture​(com.google.common.util.concurrent.ListenableFuture<V> future)
      static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture​(java.util.concurrent.CompletableFuture<V> future)
      Wrap a CompletableFuture in a ListenableFuture.
      static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture​(java.util.concurrent.CompletionStage<V> future)
      Wrap a CompletionStage in a ListenableFuture.
      private static <T> java.util.concurrent.CompletionStage<java.util.concurrent.CompletionStage<T>> wrap​(java.util.concurrent.CompletionStage<T> future)  
      • Methods inherited from class java.lang.Object

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

      • CompletableFuturesExtra

        private CompletableFuturesExtra()
    • Method Detail

      • toListenableFuture

        public static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture​(java.util.concurrent.CompletableFuture<V> future)
        Wrap a CompletableFuture in a ListenableFuture. The returned future will complete with the same result or failure as the original future.
        Parameters:
        future - The CompletableFuture to wrap in a ListenableFuture.
        Returns:
        A ListenableFuture that completes when the original future completes.
      • toListenableFuture

        public static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture​(java.util.concurrent.CompletionStage<V> future)
        Wrap a CompletionStage in a ListenableFuture. The returned future will complete with the same result or failure as the original future.
        Parameters:
        future - The CompletionStage to wrap in a ListenableFuture.
        Returns:
        A ListenableFuture that completes when the original future completes.
      • toCompletableFuture

        @Deprecated
        public static <V> java.util.concurrent.CompletableFuture<V> toCompletableFuture​(com.google.common.util.concurrent.ListenableFuture<V> future)
        Wrap a ListenableFuture in a CompletableFuture. The returned future will complete with the same result or failure as the original future. Completing the returned future does not complete the original future.
        Parameters:
        future - The ListenableFuture to wrap in a CompletableFuture.
        Returns:
        A CompletableFuture that completes when the original future completes.
      • toCompletableFuture

        @Deprecated
        public static <V> java.util.concurrent.CompletableFuture<V> toCompletableFuture​(com.google.api.core.ApiFuture<V> future)
        Converts an ApiFuture to a CompletableFuture.
        Parameters:
        future - the ApiFuture to wrap.
        Returns:
        a CompletableFuture that completes when the original future completes.
      • toCompletableFuture

        @Deprecated
        public static <V> java.util.concurrent.CompletableFuture<V> toCompletableFuture​(com.google.api.core.ApiFuture<V> future,
                                                                                        java.util.concurrent.Executor executor)
        Converts an ApiFuture to a CompletableFuture.
        Parameters:
        future - the ApiFuture to wrap.
        executor - the executor where the listener is running.
        Returns:
        a CompletableFuture that completes when the original future completes.
      • toApiFuture

        public static <V> com.google.api.core.ApiFuture<V> toApiFuture​(java.util.concurrent.CompletionStage<V> future)
        Wrap a CompletionStage in a ApiFuture. The returned future will complete with the same result or failure as the original future.
        Parameters:
        future - The CompletionStage to wrap in a ApiFuture.
        Returns:
        A ApiFuture that completes when the original future completes.
      • exceptionallyCompletedFuture

        public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyCompletedFuture​(java.lang.Throwable throwable)
        Returns a new CompletableFuture that is already exceptionally completed with the given exception.
        Parameters:
        throwable - the exception
        Returns:
        the exceptionally completed CompletableFuture
      • handleCompose

        public static <T,​U> java.util.concurrent.CompletionStage<U> handleCompose​(java.util.concurrent.CompletionStage<T> stage,
                                                                                        java.util.function.BiFunction<? super T,​java.lang.Throwable,​? extends java.util.concurrent.CompletionStage<U>> fn)
        Returns a new stage that, when this stage completes either normally or exceptionally, is executed with this stage's result and exception as arguments to the supplied function.

        When this stage is complete, the given function is invoked with the result (or null if none) and the exception (or null if none) of this stage as arguments, and the function's result is used to complete the returned stage.

        This differs from CompletionStage.handle(java.util.function.BiFunction) in that the function should return a CompletionStage rather than the value directly.

        Type Parameters:
        U - the function's return type
        Parameters:
        stage - the CompletionStage to compose
        fn - the function to use to compute the value of the returned CompletionStage
        Returns:
        the new CompletionStage
      • exceptionallyCompose

        public static <T> java.util.concurrent.CompletionStage<T> exceptionallyCompose​(java.util.concurrent.CompletionStage<T> stage,
                                                                                       java.util.function.Function<java.lang.Throwable,​? extends java.util.concurrent.CompletionStage<T>> fn)
        Returns a new stage that, when this stage completes exceptionally, is executed with this stage's exception as the argument to the supplied function. Otherwise, if this stage completes normally, then the returned stage also completes normally with the same value.

        This differs from CompletionStage.exceptionally(java.util.function.Function) in that the function should return a CompletionStage rather than the value directly.

        Parameters:
        stage - the CompletionStage to compose
        fn - the function to use to compute the value of the returned CompletionStage if this stage completed exceptionally
        Returns:
        the new CompletionStage
      • checkCompleted

        public static <T> void checkCompleted​(java.util.concurrent.CompletionStage<T> stage)
        check that a stage is completed.
        Parameters:
        stage - a CompletionStage.
        Throws:
        java.lang.IllegalStateException - if the stage is not completed.
      • getCompleted

        public static <T> T getCompleted​(java.util.concurrent.CompletionStage<T> stage)
        Get the value of a completed stage.
        Parameters:
        stage - a completed CompletionStage.
        Returns:
        the value of the stage if it has one.
        Throws:
        java.lang.IllegalStateException - if the stage is not completed.
        java.util.concurrent.CompletionException - if the future completed exceptionally.
      • dereference

        public static <T> java.util.concurrent.CompletionStage<T> dereference​(java.util.concurrent.CompletionStage<? extends java.util.concurrent.CompletionStage<T>> stage)
        This takes a stage of a stage of a value and returns a plain stage of a value.
        Parameters:
        stage - a CompletionStage of a CompletionStage of a value
        Returns:
        the CompletionStage of the value
      • wrap

        private static <T> java.util.concurrent.CompletionStage<java.util.concurrent.CompletionStage<T>> wrap​(java.util.concurrent.CompletionStage<T> future)