Skip to content
Permalink
Browse files
feat: add ApiFutures.successfulAsList (#163)
  • Loading branch information
Brian Chen committed Jul 17, 2020
1 parent 8fad49d commit 6af8e24a6252da7217e7c53f86cbb2b321327fc4
Showing with 26 additions and 0 deletions.
  1. +15 −0 src/main/java/com/google/api/core/ApiFutures.java
  2. +11 −0 src/test/java/com/google/api/core/ApiFuturesTest.java
@@ -172,6 +172,21 @@ public ListenableFuture<? extends V> apply(ApiFuture<? extends V> apiFuture) {
}
})));
}

@BetaApi
public static <V> ApiFuture<List<V>> successfulAsList(
Iterable<? extends ApiFuture<? extends V>> futures) {
return new ListenableFutureToApiFuture<>(
Futures.successfulAsList(
Iterables.transform(
futures,
new Function<ApiFuture<? extends V>, ListenableFuture<? extends V>>() {
public ListenableFuture<? extends V> apply(ApiFuture<? extends V> apiFuture) {
return listenableFutureForApiFuture(apiFuture);
}
})));
}

/*
* @deprecated Use {@linkplain #transformAsync(ApiFuture, ApiFunction, Executor) the
* overload that requires an executor}. For identical behavior, pass {@link
@@ -154,6 +154,17 @@ public void testAllAsList() throws Exception {
assertThat(listFuture.get()).containsExactly(1, 2).inOrder();
}

@Test
public void successfulAllAsList() throws Exception {
SettableApiFuture<Integer> inputFuture1 = SettableApiFuture.<Integer>create();
SettableApiFuture<Integer> inputFuture2 = SettableApiFuture.<Integer>create();
ApiFuture<List<Integer>> listFuture =
ApiFutures.successfulAsList(ImmutableList.of(inputFuture1, inputFuture2));
inputFuture1.set(1);
inputFuture2.setException(new Exception());
assertThat(listFuture.get()).containsExactly(1, null).inOrder();
}

@Test
public void testTransformAsync() throws Exception {
ApiFuture<Integer> inputFuture = ApiFutures.immediateFuture(0);

0 comments on commit 6af8e24

Please sign in to comment.