From 4dbbe6c32ae8fc583d99f54045d8cbb9f8320d33 Mon Sep 17 00:00:00 2001 From: Keir Nellyer Date: Wed, 27 Jul 2016 16:49:50 +0100 Subject: [PATCH] Allow Collector instance to be configured in parameters --- .../mineplex/core/common/util/UtilFuture.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilFuture.java b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilFuture.java index 330ac1957..7e9aa87aa 100644 --- a/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilFuture.java +++ b/Plugins/Mineplex.Core.Common/src/mineplex/core/common/util/UtilFuture.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.function.Function; +import java.util.stream.Collector; import java.util.stream.Collectors; public class UtilFuture @@ -16,20 +17,39 @@ public class UtilFuture * This is a workaround for {@link CompletableFuture#anyOf(CompletableFuture[])} returning void. * * @param futures the futures to wait to complete - * @param the return type of the supplied futures + * @param the type of item(s) * @return a future which will complete when all supplied futures have completed */ - public static CompletableFuture> sequence(List> futures) + public static CompletableFuture> sequence(Collection> futures) + { + return sequence(futures, Collectors.toList()); + } + + /** + * Returns a {@link CompletableFuture} which will complete when supplied futures have completed. + * This is a workaround for {@link CompletableFuture#anyOf(CompletableFuture[])} returning void. + * + * @param futures the futures to wait to complete + * @param the collection type + * @param the type of the collection's items + * @return a future which will complete when all supplied futures have completed + */ + public static CompletableFuture sequence(Collection> futures, Collector collector) { CompletableFuture futuresCompletedFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); return futuresCompletedFuture.thenApply(v -> - futures.stream().map(CompletableFuture::join).collect(Collectors.toList())); + futures.stream().map(CompletableFuture::join).collect(collector)); + } + + public static CompletableFuture> filter(List list, Function> futureFunction) + { + return filter(list, futureFunction, Collectors.toList()); } // TODO javadocs - public static CompletableFuture> filter(List list, Function> futureFunction) + public static CompletableFuture filter(List list, Function> futureFunction, Collector collector) { Map> elementFutureMap = list.stream() .collect(Collectors.toMap( @@ -43,6 +63,6 @@ public class UtilFuture elementFutureMap.entrySet().stream() .filter(entry -> entry.getValue().join()) // this doesn't block as all futures have completed .map(Map.Entry::getKey) - .collect(Collectors.toList())); + .collect(collector)); } }