Add more convenient version of CompletableFuture.allOf()
Change-Id: I40a2db5de22870adf524a9d0e1895c721291a50f
Showing
1 changed file
with
17 additions
and
0 deletions
| ... | @@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit; | ... | @@ -49,6 +49,7 @@ import java.util.concurrent.TimeUnit; |
| 49 | import java.util.concurrent.TimeoutException; | 49 | import java.util.concurrent.TimeoutException; |
| 50 | import java.util.function.Function; | 50 | import java.util.function.Function; |
| 51 | import java.util.function.Supplier; | 51 | import java.util.function.Supplier; |
| 52 | +import java.util.stream.Collectors; | ||
| 52 | import java.util.stream.Stream; | 53 | import java.util.stream.Stream; |
| 53 | import java.util.stream.StreamSupport; | 54 | import java.util.stream.StreamSupport; |
| 54 | 55 | ||
| ... | @@ -532,6 +533,22 @@ public abstract class Tools { | ... | @@ -532,6 +533,22 @@ public abstract class Tools { |
| 532 | } | 533 | } |
| 533 | 534 | ||
| 534 | /** | 535 | /** |
| 536 | + * Returns a new CompletableFuture completed with a list of computed values | ||
| 537 | + * when all of the given CompletableFuture complete. | ||
| 538 | + * | ||
| 539 | + * @param futures the CompletableFutures | ||
| 540 | + * @param <T> value type of CompletableFuture | ||
| 541 | + * @return a new CompletableFuture that is completed when all of the given CompletableFutures complete | ||
| 542 | + */ | ||
| 543 | + public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures) { | ||
| 544 | + return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])) | ||
| 545 | + .thenApply(v -> futures.stream() | ||
| 546 | + .map(CompletableFuture::join) | ||
| 547 | + .collect(Collectors.toList()) | ||
| 548 | + ); | ||
| 549 | + } | ||
| 550 | + | ||
| 551 | + /** | ||
| 535 | * Returns the contents of {@code ByteBuffer} as byte array. | 552 | * Returns the contents of {@code ByteBuffer} as byte array. |
| 536 | * <p> | 553 | * <p> |
| 537 | * WARNING: There is a performance cost due to array copy | 554 | * WARNING: There is a performance cost due to array copy | ... | ... |
-
Please register or login to post a comment