
com.pivovarit.collectors.FutureCollectors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of parallel-collectors Show documentation
Show all versions of parallel-collectors Show documentation
Parallel collection processing with customizable thread pools
package com.pivovarit.collectors;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
/**
* @author Grzegorz Piwowarek
*/
class FutureCollectors {
static Collector, ?, CompletableFuture> toFuture(Collector collector) {
return Collectors.collectingAndThen(toList(), list -> {
CompletableFuture future = CompletableFuture
.allOf(list.toArray(new CompletableFuture[0]))
.thenApply(__ -> list.stream()
.map(CompletableFuture::join)
.collect(collector));
for (CompletableFuture f : list) {
f.exceptionally((throwable) -> {
future.completeExceptionally(throwable);
return null;
});
}
return future;
});
}
static Collector, ?, CompletableFuture>> toFuture() {
return toFuture(toList());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy