Package io.airlift.concurrent
Class MoreFutures
java.lang.Object
io.airlift.concurrent.MoreFutures
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidaddExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable exceptionCallback) Invokes the callback if the future fails.static <T> voidaddExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable exceptionCallback, Executor executor) Invokes the callback, using the specified executor, if the future fails.static <T> voidaddExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<Throwable> exceptionCallback) Invokes the callback if the future fails.static <T> voidaddExceptionCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<Throwable> exceptionCallback, Executor executor) Invokes the callback, using the specified executor, if the future fails.static <T> voidaddSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable successCallback) Invokes the callback if the future completes successfully.static <T> voidaddSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Runnable successCallback, Executor executor) Invokes the callback, using the specified executor, if the future completes successfully.static <T> voidaddSuccessCallback(com.google.common.util.concurrent.ListenableFuture<T> future, Consumer<T> successCallback) Invokes the callback if the future completes successfully.static <T> voidaddSuccessCallback(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.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.static <T> CompletableFuture<T>addTimeout(CompletableFuture<T> future, Callable<T> onTimeout, io.airlift.units.Duration timeout, ScheduledExecutorService executorService) Deprecated.static <V> CompletableFuture<List<V>>allAsList(List<CompletableFuture<? extends V>> futures) Deprecated.static <V> com.google.common.util.concurrent.ListenableFuture<List<V>>allAsListWithCancellationOnFailure(Iterable<? extends com.google.common.util.concurrent.ListenableFuture<? extends V>> futures) Same asFutures.allAsList(ListenableFuture[]), but additionally cancels any remaining component Futures if the result has already failed.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>.static voidcheckSuccess(Future<?> future, String errorMessage) Checks that the completed future completed successfully.static <V> CompletableFuture<V>failedFuture(Throwable throwable) Deprecated.static <V> CompletableFuture<V>firstCompletedFuture(Iterable<? extends CompletionStage<? extends V>> futures) Deprecated.static <V> CompletableFuture<V>firstCompletedFuture(Iterable<? extends CompletionStage<? extends V>> futures, boolean propagateCancel) Deprecated.static <T> TReturns the result of the inputFuture, which must have already completed.static <V> VgetFutureValue(Future<V> future) Waits for the value from the future.static <V,E extends Exception>
VgetFutureValue(Future<V> future, Class<E> exceptionType) Waits for the value from the future.static <T> voidmirror(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.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.static <V> CompletableFuture<V>toCompletableFuture(com.google.common.util.concurrent.ListenableFuture<V> listenableFuture) Converts a ListenableFuture to a CompletableFuture.static <V> com.google.common.util.concurrent.ListenableFuture<V>toListenableFuture(CompletableFuture<V> completableFuture) Converts a CompletableFuture to a ListenableFuture.static <T> VoidtoVoid(T value) Converts a value to Void.static <T> Optional<T>tryGetFutureValue(Future<T> future) Gets the current value of the future without waiting.static <V> Optional<V>tryGetFutureValue(Future<V> future, int timeout, TimeUnit timeUnit) Waits for the the value from the future for the specified time.tryGetFutureValue(Future<V> future, int timeout, TimeUnit timeUnit, Class<E> exceptionType) Waits for the the value from the future for the specified time.static <V> CompletableFuture<V>unmodifiableFuture(CompletableFuture<V> future) Deprecated.static <V> CompletableFuture<V>unmodifiableFuture(CompletableFuture<V> future, boolean propagateCancel) Deprecated.static ThrowableunwrapCompletionException(Throwable throwable) Attempts to unwrap a throwable that has been wrapped in aCompletionException.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.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.
-
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
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
Attempts to unwrap a throwable that has been wrapped in aCompletionException. -
unmodifiableFuture
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.Returns a failed future containing the specified throwable. -
getFutureValue
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
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
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
Returns the result of the inputFuture, which must have already completed.Similar to Futures
Futures.getDone(Future), but does not throw checked exceptions. -
checkSuccess
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_causeis 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 asFutures.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
-