Package org.apache.druid.common.guava
Class FutureUtils
- java.lang.Object
-
- org.apache.druid.common.guava.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)
LikeFutures.successfulAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...)
, but returnsEither
instead of usingnull
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, likefuture.get()
.static <T> T
getUnchecked(com.google.common.util.concurrent.ListenableFuture<T> future, boolean cancelIfInterrupted)
Waits for a given future and returns its value, likefuture.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)
LikeFutures.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)
LikeFutures.transformAsync(ListenableFuture, AsyncFunction, java.util.concurrent.Executor)
, but works better with lambdas due to not having overloads.
-
-
-
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, likefuture.get()
. On InterruptedException, cancels the provided future ifcancelIfInterrupted
, then re-throws the original InterruptedException. Passes through CancellationExceptions and ExecutionExceptions as-is.
-
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, likefuture.get()
. On InterruptException, cancels the provided future ifcancelIfInterrupted
, 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.
-
getUncheckedImmediately
public static <T> T getUncheckedImmediately(com.google.common.util.concurrent.ListenableFuture<T> future)
Gets the result of a given future immediately. Equivalent togetUnchecked(com.google.common.util.concurrent.ListenableFuture<T>, boolean)
if the future is ready. Otherwise, throws anIllegalStateException
.
-
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)
LikeFutures.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 writeFutureUtils.transform(future, v -> ...)
instead ofFutures.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)
LikeFutures.transformAsync(ListenableFuture, AsyncFunction, java.util.concurrent.Executor)
, but works better with lambdas due to not having overloads. One can writeFutureUtils.transformAsync(future, v -> ...)
instead ofFutures.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)
LikeFutures.successfulAsList(com.google.common.util.concurrent.ListenableFuture<? extends V>...)
, but returnsEither
instead of usingnull
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.
-
-