Package com.spotify.futures
Class CompletableFuturesExtra
java.lang.Object
com.spotify.futures.CompletableFuturesExtra
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumprivate static enum -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidcheckCompleted(CompletionStage<T> stage) check that a stage is completed.static <T> CompletionStage<T>dereference(CompletionStage<? extends CompletionStage<T>> stage) This takes a stage of a stage of a value and returns a plain stage of a value.static <T> CompletableFuture<T>exceptionallyCompletedFuture(Throwable throwable) Returns a new CompletableFuture that is already exceptionally completed with the given exception.static <T> CompletionStage<T>exceptionallyCompose(CompletionStage<T> stage, Function<Throwable, ? extends 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> TgetCompleted(CompletionStage<T> stage) Get the value of a completed stage.static ThrowablegetCompletedException(CompletionStage<?> stage) Get the exception from an exceptionally completed stage.static <T,U> CompletionStage<U> handleCompose(CompletionStage<T> stage, BiFunction<? super T, Throwable, ? extends 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(CompletionStage<V> future) Wrap aCompletionStagein aApiFuture.static <V> CompletableFuture<V>toCompletableFuture(com.google.api.core.ApiFuture<V> future) Deprecated.static <V> CompletableFuture<V>toCompletableFuture(com.google.api.core.ApiFuture<V> future, Executor executor) Deprecated.static <V> CompletableFuture<V>toCompletableFuture(com.google.common.util.concurrent.ListenableFuture<V> future) Deprecated.static <V> com.google.common.util.concurrent.ListenableFuture<V>toListenableFuture(CompletableFuture<V> future) Wrap aCompletableFuturein aListenableFuture.static <V> com.google.common.util.concurrent.ListenableFuture<V>toListenableFuture(CompletionStage<V> future) Wrap aCompletionStagein aListenableFuture.private static <T> CompletionStage<CompletionStage<T>>wrap(CompletionStage<T> future)
-
Constructor Details
-
CompletableFuturesExtra
private CompletableFuturesExtra()
-
-
Method Details
-
toListenableFuture
public static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture(CompletableFuture<V> future) Wrap aCompletableFuturein aListenableFuture. The returned future will complete with the same result or failure as the original future.- Parameters:
future- TheCompletableFutureto wrap in aListenableFuture.- Returns:
- A
ListenableFuturethat completes when the original future completes.
-
toListenableFuture
public static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture(CompletionStage<V> future) Wrap aCompletionStagein aListenableFuture. The returned future will complete with the same result or failure as the original future.- Parameters:
future- TheCompletionStageto wrap in aListenableFuture.- Returns:
- A
ListenableFuturethat completes when the original future completes.
-
toCompletableFuture
@Deprecated public static <V> CompletableFuture<V> toCompletableFuture(com.google.common.util.concurrent.ListenableFuture<V> future) Deprecated.Wrap aListenableFuturein aCompletableFuture. 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- TheListenableFutureto wrap in aCompletableFuture.- Returns:
- A
CompletableFuturethat completes when the original future completes.
-
toCompletableFuture
@Deprecated public static <V> CompletableFuture<V> toCompletableFuture(com.google.api.core.ApiFuture<V> future) Deprecated.Converts anApiFutureto aCompletableFuture.- Parameters:
future- theApiFutureto wrap.- Returns:
- a
CompletableFuturethat completes when the original future completes.
-
toCompletableFuture
@Deprecated public static <V> CompletableFuture<V> toCompletableFuture(com.google.api.core.ApiFuture<V> future, Executor executor) Deprecated.Converts anApiFutureto aCompletableFuture.- Parameters:
future- theApiFutureto wrap.executor- the executor where the listener is running.- Returns:
- a
CompletableFuturethat completes when the original future completes.
-
toApiFuture
Wrap aCompletionStagein aApiFuture. The returned future will complete with the same result or failure as the original future.- Parameters:
future- TheCompletionStageto wrap in aApiFuture.- Returns:
- A
ApiFuturethat completes when the original future completes.
-
exceptionallyCompletedFuture
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> CompletionStage<U> handleCompose(CompletionStage<T> stage, BiFunction<? super T, Throwable, ? extends 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
nullif none) and the exception (ornullif 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 aCompletionStagerather than the value directly.- Type Parameters:
U- the function's return type- Parameters:
stage- theCompletionStageto composefn- the function to use to compute the value of the returnedCompletionStage- Returns:
- the new
CompletionStage
-
exceptionallyCompose
public static <T> CompletionStage<T> exceptionallyCompose(CompletionStage<T> stage, Function<Throwable, ? extends 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 aCompletionStagerather than the value directly.- Parameters:
stage- theCompletionStageto composefn- the function to use to compute the value of the returnedCompletionStageif this stage completed exceptionally- Returns:
- the new
CompletionStage
-
checkCompleted
check that a stage is completed.- Parameters:
stage- aCompletionStage.- Throws:
IllegalStateException- if the stage is not completed.
-
getCompleted
Get the value of a completed stage.- Parameters:
stage- a completedCompletionStage.- Returns:
- the value of the stage if it has one.
- Throws:
IllegalStateException- if the stage is not completed.CompletionException- if the future completed exceptionally.
-
getCompletedException
Get the exception from an exceptionally completed stage.- Parameters:
stage- a completedCompletionStage.- Returns:
- the exception of the stage if it has one.
- Throws:
IllegalStateException- if the stage is not completed, or not completed exceptionally.
-
dereference
public static <T> CompletionStage<T> dereference(CompletionStage<? extends CompletionStage<T>> stage) This takes a stage of a stage of a value and returns a plain stage of a value.- Parameters:
stage- aCompletionStageof aCompletionStageof a value- Returns:
- the
CompletionStageof the value
-
wrap
-
ApiFuturesExtra.toCompletableFuture(ApiFuture)