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

Commit 6af8e24

Browse files
author
Brian Chen
authored
feat: add ApiFutures.successfulAsList (#163)
1 parent 8fad49d commit 6af8e24

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/com/google/api/core/ApiFutures.java

+15
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ public ListenableFuture<? extends V> apply(ApiFuture<? extends V> apiFuture) {
172172
}
173173
})));
174174
}
175+
176+
@BetaApi
177+
public static <V> ApiFuture<List<V>> successfulAsList(
178+
Iterable<? extends ApiFuture<? extends V>> futures) {
179+
return new ListenableFutureToApiFuture<>(
180+
Futures.successfulAsList(
181+
Iterables.transform(
182+
futures,
183+
new Function<ApiFuture<? extends V>, ListenableFuture<? extends V>>() {
184+
public ListenableFuture<? extends V> apply(ApiFuture<? extends V> apiFuture) {
185+
return listenableFutureForApiFuture(apiFuture);
186+
}
187+
})));
188+
}
189+
175190
/*
176191
* @deprecated Use {@linkplain #transformAsync(ApiFuture, ApiFunction, Executor) the
177192
* overload that requires an executor}. For identical behavior, pass {@link

src/test/java/com/google/api/core/ApiFuturesTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,17 @@ public void testAllAsList() throws Exception {
154154
assertThat(listFuture.get()).containsExactly(1, 2).inOrder();
155155
}
156156

157+
@Test
158+
public void successfulAllAsList() throws Exception {
159+
SettableApiFuture<Integer> inputFuture1 = SettableApiFuture.<Integer>create();
160+
SettableApiFuture<Integer> inputFuture2 = SettableApiFuture.<Integer>create();
161+
ApiFuture<List<Integer>> listFuture =
162+
ApiFutures.successfulAsList(ImmutableList.of(inputFuture1, inputFuture2));
163+
inputFuture1.set(1);
164+
inputFuture2.setException(new Exception());
165+
assertThat(listFuture.get()).containsExactly(1, null).inOrder();
166+
}
167+
157168
@Test
158169
public void testTransformAsync() throws Exception {
159170
ApiFuture<Integer> inputFuture = ApiFutures.immediateFuture(0);

0 commit comments

Comments
 (0)