- java.lang.Object
-
- com.spotify.futures.CompletableFuturesExtra
-
public class CompletableFuturesExtra extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classCompletableFuturesExtra.Identityprivate static classCompletableFuturesExtra.WrapFunction
-
Constructor Summary
Constructors Modifier Constructor Description privateCompletableFuturesExtra()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> voidcheckCompleted(java.util.concurrent.CompletionStage<T> stage)check that a stage is completed.static <T> java.util.concurrent.CompletionStage<T>dereference(java.util.concurrent.CompletionStage<? extends java.util.concurrent.CompletionStage<T>> stage)This takes a stage of a stage of a value and returns a plain stage of a value.static <T> java.util.concurrent.CompletableFuture<T>exceptionallyCompletedFuture(java.lang.Throwable throwable)Returns a new CompletableFuture that is already exceptionally completed with the given exception.static <T> java.util.concurrent.CompletionStage<T>exceptionallyCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.Function<java.lang.Throwable,? extends java.util.concurrent.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(java.util.concurrent.CompletionStage<T> stage)Get the value of a completed stage.static <T,U>
java.util.concurrent.CompletionStage<U>handleCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.BiFunction<? super T,java.lang.Throwable,? extends java.util.concurrent.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(java.util.concurrent.CompletionStage<V> future)Wrap aCompletionStagein aApiFuture.static <V> java.util.concurrent.CompletableFuture<V>toCompletableFuture(com.google.api.core.ApiFuture<V> future)Deprecated.static <V> java.util.concurrent.CompletableFuture<V>toCompletableFuture(com.google.api.core.ApiFuture<V> future, java.util.concurrent.Executor executor)Deprecated.static <V> java.util.concurrent.CompletableFuture<V>toCompletableFuture(com.google.common.util.concurrent.ListenableFuture<V> future)Deprecated.static <V> com.google.common.util.concurrent.ListenableFuture<V>toListenableFuture(java.util.concurrent.CompletableFuture<V> future)Wrap aCompletableFuturein aListenableFuture.static <V> com.google.common.util.concurrent.ListenableFuture<V>toListenableFuture(java.util.concurrent.CompletionStage<V> future)Wrap aCompletionStagein aListenableFuture.private static <T> java.util.concurrent.CompletionStage<java.util.concurrent.CompletionStage<T>>wrap(java.util.concurrent.CompletionStage<T> future)
-
-
-
Method Detail
-
toListenableFuture
public static <V> com.google.common.util.concurrent.ListenableFuture<V> toListenableFuture(java.util.concurrent.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(java.util.concurrent.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> java.util.concurrent.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> java.util.concurrent.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> java.util.concurrent.CompletableFuture<V> toCompletableFuture(com.google.api.core.ApiFuture<V> future, java.util.concurrent.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
public static <V> com.google.api.core.ApiFuture<V> toApiFuture(java.util.concurrent.CompletionStage<V> future)
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
public static <T> java.util.concurrent.CompletableFuture<T> exceptionallyCompletedFuture(java.lang.Throwable throwable)
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> java.util.concurrent.CompletionStage<U> handleCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.BiFunction<? super T,java.lang.Throwable,? extends java.util.concurrent.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> java.util.concurrent.CompletionStage<T> exceptionallyCompose(java.util.concurrent.CompletionStage<T> stage, java.util.function.Function<java.lang.Throwable,? extends java.util.concurrent.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
public static <T> void checkCompleted(java.util.concurrent.CompletionStage<T> stage)
check that a stage is completed.- Parameters:
stage- aCompletionStage.- Throws:
java.lang.IllegalStateException- if the stage is not completed.
-
getCompleted
public static <T> T getCompleted(java.util.concurrent.CompletionStage<T> stage)
Get the value of a completed stage.- Parameters:
stage- a completedCompletionStage.- Returns:
- the value of the stage if it has one.
- Throws:
java.lang.IllegalStateException- if the stage is not completed.java.util.concurrent.CompletionException- if the future completed exceptionally.
-
dereference
public static <T> java.util.concurrent.CompletionStage<T> dereference(java.util.concurrent.CompletionStage<? extends java.util.concurrent.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
private static <T> java.util.concurrent.CompletionStage<java.util.concurrent.CompletionStage<T>> wrap(java.util.concurrent.CompletionStage<T> future)
-
-