Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Commit

Permalink
feat: add ApiFutures.successfulAsList (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Chen committed Jul 17, 2020
1 parent 8fad49d commit 6af8e24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/google/api/core/ApiFutures.java
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/google/api/core/ApiFuturesTest.java
Expand Up @@ -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);
Expand Down

0 comments on commit 6af8e24

Please sign in to comment.