com.pivovarit.collectors.AsyncParallelCollector 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.Collection;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.concurrent.CompletableFuture.completedFuture;
/**
* @author Grzegorz Piwowarek
*/
final class AsyncParallelCollector>
extends AbstractParallelCollector>
implements AutoCloseable {
private final Dispatcher dispatcher;
private final Function operation;
private final Supplier collectionFactory;
AsyncParallelCollector(
Function operation,
Supplier collection,
Executor executor,
int parallelism) {
this.dispatcher = new ThrottlingDispatcher<>(executor, parallelism);
this.collectionFactory = collection;
this.operation = operation;
}
AsyncParallelCollector(
Function operation,
Supplier collection,
Executor executor) {
this.dispatcher = new UnboundedDispatcher<>(executor);
this.collectionFactory = collection;
this.operation = operation;
}
@Override
public BiConsumer>, T> accumulator() {
return (acc, e) -> acc.add(dispatcher.enqueue(() -> operation.apply(e)));
}
@Override
public Function>, CompletableFuture> finisher() {
if (dispatcher.getWorkingQueue().size() != 0) {
dispatcher.start();
return foldLeftFutures(collectionFactory).andThen(f -> supplyWithResources(() -> f, dispatcher::close));
} else {
return supplyWithResources(() -> (__) -> completedFuture(collectionFactory
.get()), dispatcher::close);
}
}
@Override
public Set characteristics() {
return EnumSet.of(Characteristics.UNORDERED);
}
@Override
public void close() {
dispatcher.close();
}
}