Skip to content
This repository was archived by the owner on Sep 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions src/main/java/com/google/api/core/ApiFutures.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <V> void addCallback(
final ApiFuture<V> future, final ApiFutureCallback<? super V> callback) {
addCallback(future, callback, directExecutor());
Expand All @@ -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 <V, X extends Throwable> ApiFuture<V> catching(
ApiFuture<? extends V> input,
Class<X> exceptionType,
ApiFunction<? super X, ? extends V> callback) {
return catching(input, exceptionType, callback, directExecutor());
}

public static <V, X extends Throwable> ApiFuture<V> catching(
ApiFuture<? extends V> input,
Class<X> exceptionType,
ApiFunction<? super X, ? extends V> callback,
Executor executor) {
ListenableFuture<V> catchingFuture =
Futures.catching(
listenableFutureForApiFuture(input),
exceptionType,
new GaxFunctionToGuavaFunction<X, V>(callback));
new GaxFunctionToGuavaFunction<X, V>(callback),
directExecutor());

This comment was marked as spam.

return new ListenableFutureToApiFuture<V>(catchingFuture);
}

Expand All @@ -93,11 +116,16 @@ public static <V> ApiFuture<V> immediateCancelledFuture() {
return new ListenableFutureToApiFuture<V>(Futures.<V>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 <V, X> ApiFuture<X> transform(
ApiFuture<? extends V> input, final ApiFunction<? super V, ? extends X> function) {
return new ListenableFutureToApiFuture<>(
Futures.transform(
listenableFutureForApiFuture(input), new GaxFunctionToGuavaFunction<V, X>(function)));
return transform(input, function, directExecutor());

This comment was marked as spam.

}

public static <V, X> ApiFuture<X> transform(
Expand All @@ -123,20 +151,16 @@ public ListenableFuture<? extends V> apply(ApiFuture<? extends V> 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 <I, O> ApiFuture<O> transformAsync(
ApiFuture<I> input, final ApiAsyncFunction<I, O> function) {
ListenableFuture<I> listenableInput = listenableFutureForApiFuture(input);
ListenableFuture<O> listenableOutput =
Futures.transformAsync(
listenableInput,
new AsyncFunction<I, O>() {
@Override
public ListenableFuture<O> apply(I input) throws Exception {
return listenableFutureForApiFuture(function.apply(input));
}
});
return new ListenableFutureToApiFuture<>(listenableOutput);
return transformAsync(input, function, directExecutor());

This comment was marked as spam.

}

public static <I, O> ApiFuture<O> transformAsync(
Expand Down
13 changes: 9 additions & 4 deletions src/test/java/com/google/api/core/ApiFuturesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -143,7 +147,8 @@ public void testTransformAsync() throws Exception {
public ApiFuture<Integer> apply(Integer input) {
return ApiFutures.immediateFuture(input + 1);
}
});
},
directExecutor());
assertThat(outputFuture.get()).isEqualTo(1);
}

Expand Down