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

feat: add ApiFutures.successfulAsList #163

Merged
merged 1 commit into from Jul 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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