Class FutureUtils


  • public class FutureUtils
    extends Object
    • Constructor Summary

      Constructors 
      Constructor Description
      FutureUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> com.google.common.util.concurrent.ListenableFuture<List<Either<Throwable,​T>>> coalesce​(List<com.google.common.util.concurrent.ListenableFuture<T>> futures)
      Like Futures.successfulAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...), but returns Either instead of using null in case of error.
      static <T> com.google.common.util.concurrent.ListenableFuture<T> futureWithBaggage​(com.google.common.util.concurrent.ListenableFuture<T> future, Closeable baggage)
      Returns a future that resolves when "future" resolves and "baggage" has been closed.
      static <T> T get​(com.google.common.util.concurrent.ListenableFuture<T> future, boolean cancelIfInterrupted)
      Waits for a given future and returns its value, like future.get().
      static <T> T getUnchecked​(com.google.common.util.concurrent.ListenableFuture<T> future, boolean cancelIfInterrupted)
      Waits for a given future and returns its value, like future.get().
      static <T> T getUncheckedImmediately​(com.google.common.util.concurrent.ListenableFuture<T> future)
      Gets the result of a given future immediately.
      static <T,​R>
      com.google.common.util.concurrent.ListenableFuture<R>
      transform​(com.google.common.util.concurrent.ListenableFuture<T> future, Function<T,​R> fn)
      Like Futures.transform(com.google.common.util.concurrent.ListenableFuture<I>, com.google.common.base.Function<? super I, ? extends O>, java.util.concurrent.Executor), but works better with lambdas due to not having overloads.
      static <T,​R>
      com.google.common.util.concurrent.ListenableFuture<R>
      transformAsync​(com.google.common.util.concurrent.ListenableFuture<T> future, com.google.common.util.concurrent.AsyncFunction<T,​R> fn)
      Like Futures.transformAsync(ListenableFuture, AsyncFunction, java.util.concurrent.Executor), but works better with lambdas due to not having overloads.
    • Constructor Detail

      • FutureUtils

        public FutureUtils()
    • Method Detail

      • get

        public static <T> T get​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                boolean cancelIfInterrupted)
                         throws InterruptedException,
                                ExecutionException
        Waits for a given future and returns its value, like future.get(). On InterruptedException, cancels the provided future if cancelIfInterrupted, then re-throws the original InterruptedException. Passes through CancellationExceptions and ExecutionExceptions as-is.
        Throws:
        InterruptedException
        ExecutionException
      • getUnchecked

        public static <T> T getUnchecked​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                         boolean cancelIfInterrupted)
        Waits for a given future and returns its value, like future.get(). On InterruptException, cancels the provided future if cancelIfInterrupted, and in either case, throws a RuntimeException that wraps the original InterruptException. Passes through CancellationExceptions as-is. Re-wraps the causes of ExecutionExceptions using RuntimeException.
      • transform

        public static <T,​R> com.google.common.util.concurrent.ListenableFuture<R> transform​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                                                                                  Function<T,​R> fn)
        Like Futures.transform(com.google.common.util.concurrent.ListenableFuture<I>, com.google.common.base.Function<? super I, ? extends O>, java.util.concurrent.Executor), but works better with lambdas due to not having overloads. One can write FutureUtils.transform(future, v -> ...) instead of Futures.transform(future, (Function<? super T, ?>) v -> ...)
      • transformAsync

        public static <T,​R> com.google.common.util.concurrent.ListenableFuture<R> transformAsync​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                                                                                       com.google.common.util.concurrent.AsyncFunction<T,​R> fn)
        Like Futures.transformAsync(ListenableFuture, AsyncFunction, java.util.concurrent.Executor), but works better with lambdas due to not having overloads. One can write FutureUtils.transformAsync(future, v -> ...) instead of Futures.transform(future, (Function<? super T, ?>) v -> ...)
      • coalesce

        public static <T> com.google.common.util.concurrent.ListenableFuture<List<Either<Throwable,​T>>> coalesce​(List<com.google.common.util.concurrent.ListenableFuture<T>> futures)
        Like Futures.successfulAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...), but returns Either instead of using null in case of error.
      • futureWithBaggage

        public static <T> com.google.common.util.concurrent.ListenableFuture<T> futureWithBaggage​(com.google.common.util.concurrent.ListenableFuture<T> future,
                                                                                                  Closeable baggage)
        Returns a future that resolves when "future" resolves and "baggage" has been closed. If the baggage is closed successfully, the returned future will have the same value (or exception status) as the input future. If the baggage is not closed successfully, the returned future will resolve to an exception.