diff options
author | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-12-09 16:25:27 +0100 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@verizonmedia.com> | 2021-12-10 10:28:28 +0100 |
commit | 134af4f00cb0302f1ba465e51973fe44e1646e19 (patch) | |
tree | 5724df56111808aca6c33161e6f2d6740be0f8dd /vespajlib | |
parent | 4bc0e6916d4532f00b735a79905e40f3b8eb51ea (diff) |
Remove use of Guava ListenableFuture from com.yahoo.jdisc.handler
This change is not 100% API compatible. Many classes from this package inherited types from Guava.
The classes will have the same methods as before, but their type has obviously changed.
Two options; merge now with the small risk of breakage or wait for Vespa 8 branch.
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/concurrent/CompletableFutures.java | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/concurrent/CompletableFutures.java b/vespajlib/src/main/java/com/yahoo/concurrent/CompletableFutures.java index 711ce403d50..a1235c3821d 100644 --- a/vespajlib/src/main/java/com/yahoo/concurrent/CompletableFutures.java +++ b/vespajlib/src/main/java/com/yahoo/concurrent/CompletableFutures.java @@ -5,6 +5,7 @@ import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; import com.yahoo.yolean.UncheckedInterruptedException; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; @@ -70,6 +71,23 @@ public class CompletableFutures { return combiner.combined; } + /** Similar to {@link CompletableFuture#allOf(CompletableFuture[])} but returns a list of the results */ + public static <T> CompletableFuture<List<T>> allOf(List<CompletableFuture<T>> futures) { + return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new)) + .thenApply(__ -> { + List<T> results = new ArrayList<>(); + for (CompletableFuture<T> f : futures) { + try { + results.add(f.get()); + } catch (InterruptedException | ExecutionException e) { + // Should not happen since all futures are completed without exception + throw new IllegalStateException(e); + } + } + return results; + }); + } + /** * Helper for migrating from {@link ListenableFuture} to {@link CompletableFuture} in Vespa public apis * @deprecated to be removed in Vespa 8 |