Added applySequenceAsync to CompletableFuture utils

This commit is contained in:
Andrea Cavalli 2020-07-08 18:17:01 +02:00
parent 27ec2c3a40
commit ce9fc49cb1

View File

@ -12,6 +12,7 @@ import java.util.TreeSet;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -175,4 +176,12 @@ public class CompletableFutureUtils {
} }
return result; return result;
} }
public static <T> CompletableFuture<T> applySequenceAsync(T initialValue, Collection<Function<T, CompletableFuture<T>>> collection, ExecutorService executorService) {
var result = CompletableFuture.completedFuture(initialValue);
for (Function<T, CompletableFuture<T>> item : collection) {
result = result.thenComposeAsync(item, executorService);
}
return result;
}
} }