Skip to content

Commit

Permalink
Move requireValidParallelism() to Preconditions (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit authored Sep 15, 2024
1 parent f2317ac commit 3d034a8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.pivovarit.collectors.BatchingSpliterator.batching;
import static com.pivovarit.collectors.BatchingSpliterator.partitioned;
import static com.pivovarit.collectors.Preconditions.requireValidExecutor;
import static com.pivovarit.collectors.Preconditions.requireValidParallelism;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.allOf;
import static java.util.concurrent.CompletableFuture.supplyAsync;
Expand Down Expand Up @@ -164,12 +165,6 @@ private static <T> CompletableFuture<Stream<T>> combine(List<CompletableFuture<T
: new AsyncParallelCollector<>(mapper, Dispatcher.from(executor, parallelism), s -> s.collect(collector));
}

static void requireValidParallelism(int parallelism) {
if (parallelism < 1) {
throw new IllegalArgumentException("Parallelism can't be lower than 1");
}
}

static <T, R, RR> Collector<T, ?, CompletableFuture<RR>> asyncCollector(Function<T, R> mapper, Executor executor, Function<Stream<R>, RR> finisher) {
return collectingAndThen(toList(), list -> supplyAsync(() -> {
Stream.Builder<R> acc = Stream.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import java.util.stream.Collector;
import java.util.stream.Stream;

import static com.pivovarit.collectors.AsyncParallelCollector.requireValidParallelism;
import static com.pivovarit.collectors.BatchingSpliterator.batching;
import static com.pivovarit.collectors.BatchingSpliterator.partitioned;
import static com.pivovarit.collectors.CompletionStrategy.ordered;
import static com.pivovarit.collectors.CompletionStrategy.unordered;
import static com.pivovarit.collectors.Preconditions.requireValidExecutor;
import static com.pivovarit.collectors.Preconditions.requireValidParallelism;
import static java.util.Collections.emptySet;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.collectingAndThen;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/pivovarit/collectors/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ final class Preconditions {
private Preconditions() {
}

static void requireValidParallelism(int parallelism) {
if (parallelism < 1) {
throw new IllegalArgumentException("Parallelism can't be lower than 1");
}
}

static void requireValidExecutor(Executor executor) {
if (executor instanceof ThreadPoolExecutor tpe) {
switch (tpe.getRejectedExecutionHandler()) {
Expand Down

0 comments on commit 3d034a8

Please sign in to comment.