diff --git a/src/main/java/com/google/api/core/ApiFutures.java b/src/main/java/com/google/api/core/ApiFutures.java index 115cdf7a9..33f82981b 100644 --- a/src/main/java/com/google/api/core/ApiFutures.java +++ b/src/main/java/com/google/api/core/ApiFutures.java @@ -46,6 +46,13 @@ 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. + */ + @Deprecated public static void addCallback( final ApiFuture future, final ApiFutureCallback callback) { addCallback(future, callback, directExecutor()); @@ -69,15 +76,31 @@ 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. + */ + @Deprecated public static ApiFuture catching( ApiFuture input, Class exceptionType, ApiFunction callback) { + return catching(input, exceptionType, callback, directExecutor()); + } + + public static ApiFuture catching( + ApiFuture input, + Class exceptionType, + ApiFunction callback, + Executor executor) { ListenableFuture catchingFuture = Futures.catching( listenableFutureForApiFuture(input), exceptionType, - new GaxFunctionToGuavaFunction(callback)); + new GaxFunctionToGuavaFunction(callback), + directExecutor()); return new ListenableFutureToApiFuture(catchingFuture); } @@ -93,11 +116,16 @@ 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. + */ + @Deprecated public static ApiFuture transform( ApiFuture input, final ApiFunction function) { - return new ListenableFutureToApiFuture<>( - Futures.transform( - listenableFutureForApiFuture(input), new GaxFunctionToGuavaFunction(function))); + return transform(input, function, directExecutor()); } public static ApiFuture transform( @@ -123,20 +151,16 @@ 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. + */ + @Deprecated public static ApiFuture transformAsync( ApiFuture input, final ApiAsyncFunction function) { - ListenableFuture listenableInput = listenableFutureForApiFuture(input); - ListenableFuture listenableOutput = - Futures.transformAsync( - listenableInput, - new AsyncFunction() { - @Override - public ListenableFuture apply(I input) throws Exception { - return listenableFutureForApiFuture(function.apply(input)); - } - }); - return new ListenableFutureToApiFuture<>(listenableOutput); + return transformAsync(input, function, directExecutor()); } public static ApiFuture transformAsync( diff --git a/src/test/java/com/google/api/core/ApiFuturesTest.java b/src/test/java/com/google/api/core/ApiFuturesTest.java index c97ff694a..eec0fd8f3 100644 --- a/src/test/java/com/google/api/core/ApiFuturesTest.java +++ b/src/test/java/com/google/api/core/ApiFuturesTest.java @@ -31,6 +31,7 @@ package com.google.api.core; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.util.concurrent.MoreExecutors.directExecutor; import com.google.common.collect.ImmutableList; import java.util.List; @@ -58,7 +59,8 @@ public void onSuccess(Integer i) { public void onFailure(Throwable t) { flag.set(-1); } - }); + }, + directExecutor()); future.set(0); assertThat(flag.get()).isEqualTo(1); } @@ -75,7 +77,8 @@ public void testCatch() throws Exception { public Integer apply(Exception ex) { return 42; } - }); + }, + directExecutor()); future.setException(new Exception()); assertThat(fallback.get()).isEqualTo(42); } @@ -91,7 +94,8 @@ public void testTransform() throws Exception { public String apply(Integer input) { return input.toString(); } - }); + }, + directExecutor()); inputFuture.set(6); assertThat(transformedFuture.get()).isEqualTo("6"); } @@ -143,7 +147,8 @@ public void testTransformAsync() throws Exception { public ApiFuture apply(Integer input) { return ApiFutures.immediateFuture(input + 1); } - }); + }, + directExecutor()); assertThat(outputFuture.get()).isEqualTo(1); }