/** * Converts an {@link ApiFuture} to a {@link CompletableFuture}. * * @param future the {@link ApiFuture} to wrap. * @return a {@link CompletableFuture} that completes when the original future completes. */ public static <V> CompletableFuture<V> toCompletableFuture(ApiFuture<V> future) { return toCompletableFuture(future, MoreExecutors.directExecutor()); }
/** * Wrap a {@link CompletableFuture} in a {@link ListenableFuture}. The returned future will * complete with the same result or failure as the original future. * * @param future The {@link CompletableFuture} to wrap in a {@link ListenableFuture}. * @return A {@link ListenableFuture} that completes when the original future completes. */ public static <V> ListenableFuture<V> toListenableFuture( CompletableFuture<V> future) { return toListenableFuture((CompletionStage<V>) future); }
/** * 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. * * <p>This differs from * {@link java.util.concurrent.CompletionStage#exceptionally(java.util.function.Function)} * in that the function should return a {@link java.util.concurrent.CompletionStage} rather than * the value directly. * * @param stage the {@link CompletionStage} to compose * @param fn the function to use to compute the value of the * returned {@link CompletionStage} if this stage completed * exceptionally * @return the new {@link CompletionStage} */ public static <T> CompletionStage<T> exceptionallyCompose( CompletionStage<T> stage, Function<Throwable, ? extends CompletionStage<T>> fn) { return dereference(wrap(stage).exceptionally(fn)); }
/** * Get the value of a completed stage. * * @param stage a completed {@link CompletionStage}. * @return the value of the stage if it has one. * @throws IllegalStateException if the stage is not completed. * @throws java.util.concurrent.CompletionException if the future completed exceptionally. */ public static <T> T getCompleted(CompletionStage<T> stage) { CompletableFuture<T> future = stage.toCompletableFuture(); checkCompleted(future); return future.join(); }
/** * 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. * * <p>When this stage is complete, the given function is invoked * with the result (or {@code null} if none) and the exception (or * {@code null} if none) of this stage as arguments, and the * function's result is used to complete the returned stage. * * <p>This differs from * {@link java.util.concurrent.CompletionStage#handle(java.util.function.BiFunction)} * in that the function should return a {@link java.util.concurrent.CompletionStage} rather than * the value directly. * * @param stage the {@link CompletionStage} to compose * @param fn the function to use to compute the value of the * returned {@link CompletionStage} * @param <U> the function's return type * @return the new {@link CompletionStage} */ public static <T, U> CompletionStage<U> handleCompose( CompletionStage<T> stage, BiFunction<? super T, Throwable, ? extends CompletionStage<U>> fn) { return dereference(stage.handle(fn)); }