Class MoreFutures

java.lang.Object
io.airlift.concurrent.MoreFutures

public final class MoreFutures extends Object
  • Method Details

    • asVoid

      public static <T> com.google.common.util.concurrent.ListenableFuture<Void> asVoid(com.google.common.util.concurrent.ListenableFuture<T> future)
      Transforms a ListenableFuture<T> to ListenableFuture<Void>.
    • toVoid

      public static <T> Void toVoid(T value)
      Converts a value to Void. This is useful for providing named fluent style Future transforms to Void values.

      Example:

       ListenableFuture<Void> voidFuture = FluentFuture.from(future)
               .transform(MoreFutures::toVoid, directExecutor())
       
    • propagateCancellation

      public static <X, Y> void propagateCancellation(com.google.common.util.concurrent.ListenableFuture<? extends X> source, Future<? extends Y> destination, boolean mayInterruptIfRunning)
      Cancels the destination Future if the source Future is cancelled.
    • mirror

      public static <T> void mirror(com.google.common.util.concurrent.ListenableFuture<? extends T> source, com.google.common.util.concurrent.SettableFuture<? super T> destination, boolean mayInterruptIfRunning)
      Mirrors all results of the source Future to the destination Future.

      This also propagates cancellations from the destination Future back to the source Future.

    • unwrapCompletionException

      public static Throwable unwrapCompletionException(Throwable throwable)
      Attempts to unwrap a throwable that has been wrapped in a CompletionException.
    • unmodifiableFuture

      @Deprecated public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future)
      Deprecated.
      Returns a future that can not be completed or canceled.
    • unmodifiableFuture

      @Deprecated public static <V> CompletableFuture<V> unmodifiableFuture(CompletableFuture<V> future, boolean propagateCancel)
      Deprecated.
      Returns a future that can not be completed or optionally canceled.
    • failedFuture

      @Deprecated public static <V> CompletableFuture<V> failedFuture(Throwable throwable)
      Deprecated.
      Returns a failed future containing the specified throwable.
    • getFutureValue

      public static <V> V getFutureValue(Future<V> future)
      Waits for the value from the future. If the future is failed, the exception is thrown directly if unchecked or wrapped in a RuntimeException. If the thread is interrupted, the thread interruption flag is set and the original InterruptedException is wrapped in a RuntimeException and thrown.
    • getFutureValue

      public static <V, E extends Exception> V getFutureValue(Future<V> future, Class<E> exceptionType) throws E
      Waits for the value from the future. If the future is failed, the exception is thrown directly if it is an instance of the specified exception type or unchecked, or it is wrapped in a RuntimeException. If the thread is interrupted, the thread interruption flag is set and the original InterruptedException is wrapped in a RuntimeException and thrown.
      Throws:
      E extends Exception
    • tryGetFutureValue

      public static <T> Optional<T> tryGetFutureValue(Future<T> future)
      Gets the current value of the future without waiting. If the future value is null, an empty Optional is still returned, and in this case the caller must check the future directly for the null value.
    • tryGetFutureValue

      public static <V> Optional<V> tryGetFutureValue(Future<V> future, int timeout, TimeUnit timeUnit)
      Waits for the the value from the future for the specified time. If the future value is null, an empty Optional is still returned, and in this case the caller must check the future directly for the null value. If the future is failed, the exception is thrown directly if unchecked or wrapped in a RuntimeException. If the thread is interrupted, the thread interruption flag is set and the original InterruptedException is wrapped in a RuntimeException and thrown.
    • tryGetFutureValue

      public static <V, E extends Exception> Optional<V> tryGetFutureValue(Future<V> future, int timeout, TimeUnit timeUnit, Class<E> exceptionType) throws E
      Waits for the the value from the future for the specified time. If the future value is null, an empty Optional is still returned, and in this case the caller must check the future directly for the null value. If the future is failed, the exception is thrown directly if it is an instance of the specified exception type or unchecked, or it is wrapped in a RuntimeException. If the thread is interrupted, the thread interruption flag is set and the original InterruptedException is wrapped in a RuntimeException and thrown.
      Throws:
      E extends Exception
    • getDone

      public static <T> T getDone(Future<T> future)
      Returns the result of the input Future, which must have already completed.

      Similar to FuturesFutures.getDone(Future), but does not throw checked exceptions.

    • checkSuccess

      public static void checkSuccess(Future<?> future, String errorMessage)
      Checks that the completed future completed successfully.
    • whenAnyComplete

      public static <V> com.google.common.util.concurrent.ListenableFuture<V> whenAnyComplete(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures)
      Creates a future that completes when the first future completes either normally or exceptionally. Cancellation of the future propagates to the supplied futures.
    • whenAnyCompleteCancelOthers

      public static <V> com.google.common.util.concurrent.ListenableFuture<V> whenAnyCompleteCancelOthers(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures)
      Creates a future that completes when the first future completes either normally or exceptionally. All other futures are cancelled when one completes. Cancellation of the returned future propagates to the supplied futures.

      It is critical for the performance of this function that guava.concurrent.generate_cancellation_cause is false, which is the default since Guava v20.

    • firstCompletedFuture

      @Deprecated public static <V> CompletableFuture<V> firstCompletedFuture(Iterable<? extends CompletionStage<? extends V>> futures)
      Deprecated.
      Creates a future that completes when the first future completes either normally or exceptionally. Cancellation of the future does not propagate to the supplied futures.
    • firstCompletedFuture

      @Deprecated public static <V> CompletableFuture<V> firstCompletedFuture(Iterable<? extends CompletionStage<? extends V>> futures, boolean propagateCancel)
      Deprecated.
      Creates a future that completes when the first future completes either normally or exceptionally. Cancellation of the future will optionally propagate to the supplied futures.
    • allAsList

      @Deprecated public static <V> CompletableFuture<List<V>> allAsList(List<CompletableFuture<? extends V>> futures)
      Deprecated.
      Returns an unmodifiable future that is completed when all of the given futures complete. If any of the given futures complete exceptionally, then the returned future also does so immediately, with a CompletionException holding this exception as its cause. Otherwise, the results of the given futures are reflected in the returned future as a list of results matching the input order. If no futures are provided, returns a future completed with an empty list.
    • addTimeout

      public static <T> com.google.common.util.concurrent.ListenableFuture<T> addTimeout(com.google.common.util.concurrent.ListenableFuture<T> future, Callable<T> onTimeout, io.airlift.units.Duration timeout, ScheduledExecutorService executorService)
      Returns a new future that is completed when the supplied future completes or when the timeout expires. If the timeout occurs or the returned CompletableFuture is canceled, the supplied future will be canceled.
    • addTimeout

      @Deprecated public static <T> CompletableFuture<T> addTimeout(CompletableFuture<T> future, Callable<T> onTimeout, io.airlift.units.Duration timeout, ScheduledExecutorService executorService)
      Deprecated.
      Returns a new future that is completed when the supplied future completes or when the timeout expires. If the timeout occurs or the returned CompletableFuture is canceled, the supplied future will be canceled.
    • toCompletableFuture

      public static <V> CompletableFuture<V> toCompletableFuture(com.google.common.util.concurrent.ListenableFuture<V> listenableFuture)
      Converts a ListenableFuture to a CompletableFuture. Cancellation of the CompletableFuture will be propagated to the ListenableFuture.
    • toListenableFuture

      public static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture(CompletableFuture<V> completableFuture)
      Converts a CompletableFuture to a ListenableFuture. Cancellation of the ListenableFuture will be propagated to the CompletableFuture.
    • addSuccessCallback

      public static <T> void addSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<T> successCallback)
      Invokes the callback if the future completes successfully. Note, this uses the direct executor, so the callback should not be resource intensive.
    • addSuccessCallback

      public static <T> void addSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<T> successCallback, Executor executor)
      Invokes the callback, using the specified executor, if the future completes successfully.
    • addSuccessCallback

      public static <T> void addSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable successCallback)
      Invokes the callback if the future completes successfully. Note, this uses the direct executor, so the callback should not be resource intensive.
    • addSuccessCallback

      public static <T> void addSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable successCallback, Executor executor)
      Invokes the callback, using the specified executor, if the future completes successfully.
    • addExceptionCallback

      public static <T> void addExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<Throwable> exceptionCallback)
      Invokes the callback if the future fails. Note, this uses the direct executor, so the callback should not be resource intensive.
    • addExceptionCallback

      public static <T> void addExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<Throwable> exceptionCallback, Executor executor)
      Invokes the callback, using the specified executor, if the future fails.
    • addExceptionCallback

      public static <T> void addExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable exceptionCallback)
      Invokes the callback if the future fails. Note, this uses the direct executor, so the callback should not be resource intensive.
    • addExceptionCallback

      public static <T> void addExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable exceptionCallback, Executor executor)
      Invokes the callback, using the specified executor, if the future fails.
    • allAsListWithCancellationOnFailure

      public static <V> com.google.common.util.concurrent.ListenableFuture<List<V>> allAsListWithCancellationOnFailure(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures)
      Same as Futures.allAsList(ListenableFuture[]), but additionally cancels any remaining component Futures if the result has already failed.
      Parameters:
      futures - futures to combine
      Returns:
      a future that provides a list of the results of the component futures