Allow Collector instance to be configured in parameters

This commit is contained in:
Keir Nellyer 2016-07-27 16:49:50 +01:00
parent 97d72e97e4
commit 4dbbe6c32a
1 changed files with 25 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class UtilFuture public class UtilFuture
@ -16,20 +17,39 @@ public class UtilFuture
* This is a workaround for {@link CompletableFuture#anyOf(CompletableFuture[])} returning void. * This is a workaround for {@link CompletableFuture#anyOf(CompletableFuture[])} returning void.
* *
* @param futures the futures to wait to complete * @param futures the futures to wait to complete
* @param <T> the return type of the supplied futures * @param <T> the type of item(s)
* @return a future which will complete when all supplied futures have completed * @return a future which will complete when all supplied futures have completed
*/ */
public static <T> CompletableFuture<List<T>> sequence(List<CompletableFuture<T>> futures) public static <T> CompletableFuture<List<T>> sequence(Collection<CompletableFuture<T>> 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 <R> the collection type
* @param <T> the type of the collection's items
* @return a future which will complete when all supplied futures have completed
*/
public static <R, T> CompletableFuture<R> sequence(Collection<CompletableFuture<T>> futures, Collector<T, ?, R> collector)
{ {
CompletableFuture<Void> futuresCompletedFuture = CompletableFuture<Void> futuresCompletedFuture =
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])); CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()]));
return futuresCompletedFuture.thenApply(v -> return futuresCompletedFuture.thenApply(v ->
futures.stream().map(CompletableFuture::join).collect(Collectors.toList())); futures.stream().map(CompletableFuture::join).collect(collector));
}
public static <T> CompletableFuture<List<T>> filter(List<T> list, Function<T, CompletableFuture<Boolean>> futureFunction)
{
return filter(list, futureFunction, Collectors.toList());
} }
// TODO javadocs // TODO javadocs
public static <T> CompletableFuture<List<T>> filter(List<T> list, Function<T, CompletableFuture<Boolean>> futureFunction) public static <R, T> CompletableFuture<R> filter(List<T> list, Function<T, CompletableFuture<Boolean>> futureFunction, Collector<T, ?, R> collector)
{ {
Map<T, CompletableFuture<Boolean>> elementFutureMap = list.stream() Map<T, CompletableFuture<Boolean>> elementFutureMap = list.stream()
.collect(Collectors.toMap( .collect(Collectors.toMap(
@ -43,6 +63,6 @@ public class UtilFuture
elementFutureMap.entrySet().stream() elementFutureMap.entrySet().stream()
.filter(entry -> entry.getValue().join()) // this doesn't block as all futures have completed .filter(entry -> entry.getValue().join()) // this doesn't block as all futures have completed
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
.collect(Collectors.toList())); .collect(collector));
} }
} }