diff --git a/api-common-java/src/main/java/com/google/api/core/ApiFutures.java b/api-common-java/src/main/java/com/google/api/core/ApiFutures.java index 2189a5e533..4c263ce845 100644 --- a/api-common-java/src/main/java/com/google/api/core/ApiFutures.java +++ b/api-common-java/src/main/java/com/google/api/core/ApiFutures.java @@ -45,11 +45,16 @@ public final class ApiFutures { private ApiFutures() {} - /* - * @deprecated Use {@linkplain #addCallback(ApiFuture, ApiFutureCallback, Executor) the - * overload that requires an executor}. For identical behavior, pass {@link - * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether - * another executor would be safer. + /** + * Registers a callback to be run when the {@link ApiFuture}'s computation is complete or, if the + * computation is already complete, immediately. + * + * @param future The future attach the callback to + * @param callback The callback to invoke when future is completed + * @deprecated Use {@linkplain #addCallback(ApiFuture, ApiFutureCallback, Executor) the overload + * that requires an executor}. For identical behavior, pass {@link + * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether + * another executor would be safer. */ @Deprecated public static void addCallback( @@ -57,6 +62,18 @@ public static void addCallback( addCallback(future, callback, directExecutor()); } + /** + * Registers a callback to be run when the {@link ApiFuture}'s computation is complete or, if the + * computation is already complete, immediately. + * + *

Note that this method is a delegate of {@link Futures#addCallback(ListenableFuture, + * FutureCallback, Executor)}. + * + * @param future The future attach the callback to + * @param callback The callback to invoke when future is completed + * @param executor The executor to run callback when the future completes + * @see Futures#addCallback(ListenableFuture, FutureCallback, Executor) + */ public static void addCallback( final ApiFuture future, final ApiFutureCallback callback, Executor executor) { Futures.addCallback( @@ -75,11 +92,20 @@ public void onSuccess(V v) { executor); } - /* - * @deprecated Use {@linkplain #catching(ApiFuture, Class, ApiFunction, Executor) the - * overload that requires an executor}. For identical behavior, pass {@link - * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether - * another executor would be safer. + /** + * Returns an {@link ApiFuture} whose result is taken from the given primary input or, if the + * primary input fails with the given exceptionType, from the result provided by the callback. + * + * @param input The primary input {@code ApiFuture} + * @param exceptionType The exception type that triggers use of {@code fallback} + * @param callback The {@link ApiFunction} to be called if input fails with the expected exception + * type + * @return A future whose result is taken either from the given {@code input} or by the {@code + * callback} + * @deprecated Use {@linkplain #catching(ApiFuture, Class, ApiFunction, Executor) the overload + * that requires an executor}. For identical behavior, pass {@link + * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether + * another executor would be safer. */ @Deprecated public static ApiFuture catching( @@ -89,6 +115,22 @@ public static ApiFuture catching( return catching(input, exceptionType, callback, directExecutor()); } + /** + * Returns an {@link ApiFuture} whose result is taken from the given primary input or, if the + * primary input fails with the given exceptionType, from the result provided by the callback. + * + *

Note that this method is a delegate of {@link Futures#catching(ListenableFuture, Class, + * Function, Executor)}. + * + * @param input The primary input {@code ApiFuture} + * @param exceptionType The exception type that triggers use of {@code fallback} + * @param callback The {@link ApiFunction} to be called if input fails with the expected exception + * type + * @param executor The executor that runs {@code fallback} if {@code input} fails + * @return A future whose result is taken either from the given {@code input} or by the {@code + * callback} + * @see Futures#catching(ListenableFuture, Class, Function, Executor) + */ public static ApiFuture catching( ApiFuture input, Class exceptionType, @@ -103,6 +145,22 @@ public static ApiFuture catching( return new ListenableFutureToApiFuture(catchingFuture); } + /** + * Returns a {@link ApiFuture} whose result is taken from the given primary input or, if the + * primary input fails with the given exceptionType, from the result provided by the callback. + * + *

Note that this method is a delegate of {@link Futures#catchingAsync(ListenableFuture, Class, + * AsyncFunction, Executor)} + * + * @param input The primary input {@code ApiFuture} + * @param exceptionType The exception type that triggers use of {@code fallback}. + * @param callback The {@link ApiAsyncFunction} to be called if {@code input} fails with the + * expected * exception type. + * @param executor The executor that runs {@code fallback} if {@code input} fails + * @return A future whose result is taken either from the given {@code input} or by the {@code + * callback} + * @see Futures#catchingAsync(ListenableFuture, Class, AsyncFunction, Executor) + */ @BetaApi public static ApiFuture catchingAsync( ApiFuture input, @@ -124,23 +182,57 @@ public ListenableFuture apply(X exception) throws Exception { return new ListenableFutureToApiFuture<>(catchingFuture); } + /** + * Creates a {@code ApiFuture} which has its value set immediately upon construction. + * + *

Note that this method is a delegate of {@link Futures#immediateFuture(Object)}. + * + * @param value The value set to the {@code ApiFuture} upon construction + * @return A future that holds {@code value} + * @see Futures#immediateFuture(Object) + */ public static ApiFuture immediateFuture(V value) { return new ListenableFutureToApiFuture<>(Futures.immediateFuture(value)); } + /** + * Returns a {@code ApiFuture} which has an exception set immediately upon construction. + * + *

Note that this method is a delegate of {@link Futures#immediateFailedFuture(Throwable)}. + * + * @param throwable The exception set to the {@code ApiFuture} upon construction + * @return A future that holds an exception + * @see Futures#immediateFailedFuture(Throwable) + */ public static ApiFuture immediateFailedFuture(Throwable throwable) { return new ListenableFutureToApiFuture(Futures.immediateFailedFuture(throwable)); } + /** + * Creates a {@code ApiFuture} which is cancelled immediately upon construction, so that {@code + * isCancelled()} always returns {@code true}. + * + *

Note that this method is a delegate of {@link Futures#immediateCancelledFuture()}. + * + * @return A cancelled future + * @see Futures#immediateCancelledFuture() + */ public static ApiFuture immediateCancelledFuture() { return new ListenableFutureToApiFuture(Futures.immediateCancelledFuture()); } - /* - * @deprecated Use {@linkplain #transform(ApiFuture, ApiFunction, Executor) the - * overload that requires an executor}. For identical behavior, pass {@link - * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether - * another executor would be safer. + /** + * Returns a new {@code ApiFuture} whose result is derived from the result of the given {@code + * ApiFuture}. + * + * @param input The future to transform + * @param function A Function to transform the results of the provided future to the results of + * the returned future + * @return A future that holds result of the transformation + * @deprecated Use {@linkplain #transform(ApiFuture, ApiFunction, Executor) the overload that + * requires an executor}. For identical behavior, pass {@link + * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether + * another executor would be safer. */ @Deprecated public static ApiFuture transform( @@ -148,6 +240,20 @@ public static ApiFuture transform( return transform(input, function, directExecutor()); } + /** + * Returns a new {@code ApiFuture} whose result is derived from the result of the given {@code + * ApiFuture}. + * + *

Note that this method is a delegate of {@link Futures#transform(ListenableFuture, Function, + * Executor)}. + * + * @param input The future to transform + * @param function A Function to transform the results of the provided future to the results of + * the returned future. + * @param executor Executor to run the function in. + * @return A future that holds result of the transformation + * @see Futures#transform(ListenableFuture, Function, Executor) + */ public static ApiFuture transform( ApiFuture input, final ApiFunction function, @@ -159,6 +265,18 @@ public static ApiFuture transform( executor)); } + /** + * Creates a new {@code ApiFuture} whose value is a list containing the values of all its input + * futures, if all succeed. + * + *

The list of results is in the same order as the input list. + * + *

Note that this method is a delegate of {@link Futures#allAsList(Iterable)}. + * + * @param futures Futures to combine + * @return A future that provides a list of the results of the component futures + * @see Futures#allAsList(Iterable) + */ public static ApiFuture> allAsList( Iterable> futures) { return new ListenableFutureToApiFuture<>( @@ -172,6 +290,21 @@ public ListenableFuture apply(ApiFuture apiFuture) { }))); } + /** + * Creates a new {@code ApiFuture} whose value is a list containing the values of all its + * successful input futures. The list of results is in the same order as the input list, and if + * any of the provided futures fails or is canceled, its corresponding position will contain + * {@code null} (which is indistinguishable from the future having a successful value of {@code + * null}). + * + *

The list of results is in the same order as the input list. + * + *

Note that this method is a delegate of {@link Futures#successfulAsList(Iterable)}. + * + * @param futures Futures to combine + * @return A future that provides a list of the results of the component futures + * @see Futures#successfulAsList(Iterable) + */ @BetaApi public static ApiFuture> successfulAsList( Iterable> futures) { @@ -186,11 +319,20 @@ public ListenableFuture apply(ApiFuture apiFuture) { }))); } - /* - * @deprecated Use {@linkplain #transformAsync(ApiFuture, ApiFunction, Executor) the - * overload that requires an executor}. For identical behavior, pass {@link - * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether - * another executor would be safer. + /** + * Returns a new {@code ApiFuture} whose result is asynchronously derived from the result of the + * given {@code ApiFuture}. If the given {@code Future} fails, the returned {@code ApiFuture} + * fails with the same exception (and the function is not invoked). + * + * @param input The future to transform + * @param function A function to transform the result of the input future to the result of the + * output future + * @return A future that holds result of the function (if the input succeeded) or the original + * input's failure (if not) + * @deprecated Use {@linkplain #transformAsync(ApiFuture, ApiAsyncFunction, Executor)}, the + * overload that requires an executor. For identical behavior, pass {@link + * com.google.common.util.concurrent.MoreExecutors#directExecutor}, but consider whether + * another executor would be safer. */ @Deprecated public static ApiFuture transformAsync( @@ -198,6 +340,22 @@ public static ApiFuture transformAsync( return transformAsync(input, function, directExecutor()); } + /** + * Returns a new {@code ApiFuture} whose result is asynchronously derived from the result of the + * given {@code ApiFuture}. If the given {@code Future} fails, the returned {@code ApiFuture} + * fails with the same exception (and the function is not invoked). + * + *

Note that this method is a delegate of {@link Futures#transformAsync(ListenableFuture, + * AsyncFunction, Executor)}. + * + * @param input The future to transform + * @param function A function to transform the result of the input future to the result of the + * output future + * @param executor Executor to run the function in. + * @return A future that holds result of the function (if the input succeeded) or the original + * input's failure (if not) + * @see Futures#transformAsync(ListenableFuture, AsyncFunction, Executor) + */ public static ApiFuture transformAsync( ApiFuture input, final ApiAsyncFunction function, Executor executor) { ListenableFuture listenableInput = listenableFutureForApiFuture(input);