| 1 | /* | |
| 2 | * Copyright 2014-2026 Grzegorz Piwowarek, https://4comprehension.com/ | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package com.pivovarit.collectors; | |
| 17 | ||
| 18 | import java.util.HashSet; | |
| 19 | import java.util.List; | |
| 20 | import java.util.Objects; | |
| 21 | import java.util.Set; | |
| 22 | import java.util.concurrent.Executor; | |
| 23 | import java.util.concurrent.ExecutorService; | |
| 24 | import java.util.concurrent.Executors; | |
| 25 | ||
| 26 | import static java.util.Objects.requireNonNull; | |
| 27 | ||
| 28 | final class ConfigProcessor { | |
| 29 | ||
| 30 | private static final ExecutorService DEFAULT_EXECUTOR = Executors.newThreadPerTaskExecutor(Thread.ofVirtual() | |
| 31 | .name("parallel-collectors-", 0) | |
| 32 | .factory()); | |
| 33 | ||
| 34 | record Config(boolean ordered, boolean batching, int parallelism, Executor executor) { | |
| 35 | Config { | |
| 36 | Objects.requireNonNull(executor, "executor can't be null"); | |
| 37 | } | |
| 38 | } | |
| 39 | ||
| 40 | static Config process(List<Option> options) { | |
| 41 | requireNonNull(options, "options can't be null"); | |
| 42 | ||
| 43 | Set<Class<? extends Option>> seen = new HashSet<>(); | |
| 44 | ||
| 45 | Boolean batching = null; | |
| 46 | Boolean ordered = null; | |
| 47 | Integer parallelism = null; | |
| 48 | Executor executor = null; | |
| 49 | ||
| 50 | for (var option : options) { | |
| 51 |
1
1. process : negated conditional → KILLED |
if (!seen.add(option.getClass())) { |
| 52 | throw new IllegalArgumentException("each option can be used at most once, and you configured '%s' multiple times".formatted(toHumanReadableString(option))); | |
| 53 | } | |
| 54 | ||
| 55 | switch (option) { | |
| 56 | case Option.Batched ignored -> batching = true; | |
| 57 |
1
1. process : negated conditional → KILLED |
case Option.Parallelism(var p) -> parallelism = p; |
| 58 | case Option.ThreadPool(var e) -> executor = e; | |
| 59 | case Option.Ordered ignored -> ordered = true; | |
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 |
1
1. process : replaced return value with null for com/pivovarit/collectors/ConfigProcessor::process → KILLED |
return new Config( |
| 64 | Objects.requireNonNullElse(ordered, false), | |
| 65 | Objects.requireNonNullElse(batching, false), | |
| 66 | Objects.requireNonNullElse(parallelism, 0), | |
| 67 | Objects.requireNonNullElse(executor, DEFAULT_EXECUTOR)); | |
| 68 | } | |
| 69 | ||
| 70 | private static String toHumanReadableString(Option option) { | |
| 71 |
1
1. toHumanReadableString : replaced return value with "" for com/pivovarit/collectors/ConfigProcessor::toHumanReadableString → KILLED |
return switch (option) { |
| 72 | case Option.Batched ignored -> "batching"; | |
| 73 | case Option.Parallelism ignored -> "parallelism"; | |
| 74 | case Option.ThreadPool ignored -> "executor"; | |
| 75 | case Option.Ordered ignored -> "ordered"; | |
| 76 | }; | |
| 77 | } | |
| 78 | ||
| 79 | public sealed interface Option { | |
| 80 | ||
| 81 | enum Ordered implements Option { | |
| 82 | INSTANCE | |
| 83 | } | |
| 84 | ||
| 85 | enum Batched implements Option { | |
| 86 | INSTANCE | |
| 87 | } | |
| 88 | ||
| 89 | record ThreadPool(Executor executor) implements Option { | |
| 90 | public ThreadPool { | |
| 91 | Preconditions.requireValidExecutor(executor); | |
| 92 | } | |
| 93 | } | |
| 94 | ||
| 95 | record Parallelism(int parallelism) implements Option { | |
| 96 | public Parallelism { | |
| 97 | Preconditions.requireValidParallelism(parallelism); | |
| 98 | } | |
| 99 | } | |
| 100 | } | |
| 101 | } | |
Mutations | ||
| 51 |
1.1 |
|
| 57 |
1.1 |
|
| 63 |
1.1 |
|
| 71 |
1.1 |