| 1 | package com.pivovarit.collectors; | |
| 2 | ||
| 3 | import java.util.HashSet; | |
| 4 | import java.util.Optional; | |
| 5 | import java.util.OptionalInt; | |
| 6 | import java.util.Set; | |
| 7 | import java.util.concurrent.Executor; | |
| 8 | ||
| 9 | import static java.util.Objects.requireNonNull; | |
| 10 | ||
| 11 | final class ConfigProcessor { | |
| 12 | ||
| 13 | record Configuration( | |
| 14 | Optional<Boolean> ordered, | |
| 15 | Optional<Boolean> batching, | |
| 16 | OptionalInt parallelism, | |
| 17 | Optional<Executor> executor) { | |
| 18 | public Configuration { | |
| 19 | requireNonNull(ordered, "'ordered' can't be null"); | |
| 20 | requireNonNull(batching, "'batching' can't be null"); | |
| 21 | requireNonNull(parallelism, "'parallelism' can't be null"); | |
| 22 | requireNonNull(executor, "'executor' can't be null"); | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | static ConfigProcessor.Configuration process(Options.CollectingOption... options) { | |
| 27 | requireNonNull(options, "options can't be null"); | |
| 28 | ||
| 29 | Set<Class<? extends Options.CollectingOption>> seen = new HashSet<>(); | |
| 30 | ||
| 31 | Optional<Boolean> batching = Optional.empty(); | |
| 32 | Optional<Boolean> ordered = Optional.empty(); | |
| 33 | OptionalInt parallelism = OptionalInt.empty(); | |
| 34 | Optional<Executor> executor = Optional.empty(); | |
| 35 | ||
| 36 | for (var option : options) { | |
| 37 |
1
1. process : negated conditional → KILLED |
if (!seen.add(option.getClass())) { |
| 38 | throw new IllegalArgumentException("each option can be used at most once, and you configured '%s' multiple times".formatted(toHumanReadableString(option))); | |
| 39 | } | |
| 40 | ||
| 41 | switch (option) { | |
| 42 | case Options.Batched __ -> batching = Optional.of(true); | |
| 43 | case Options.Parallelism parallelismOption -> | |
| 44 | parallelism = OptionalInt.of(parallelismOption.parallelism()); | |
| 45 | case Options.ThreadPool threadPoolOption -> executor = Optional.ofNullable(threadPoolOption.executor()); | |
| 46 | case Options.Ordered __ -> ordered = Optional.of(true); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 |
1
1. process : replaced return value with null for com/pivovarit/collectors/ConfigProcessor::process → KILLED |
return new Configuration(ordered, batching, parallelism, executor); |
| 51 | } | |
| 52 | ||
| 53 | private static String toHumanReadableString(Options.CollectingOption option) { | |
| 54 |
1
1. toHumanReadableString : replaced return value with "" for com/pivovarit/collectors/ConfigProcessor::toHumanReadableString → KILLED |
return switch (option) { |
| 55 | case Options.Batched __ -> "batching"; | |
| 56 | case Options.Parallelism __ -> "parallelism"; | |
| 57 | case Options.ThreadPool __ -> "executor"; | |
| 58 | case Options.Ordered __ -> "ordered"; | |
| 59 | }; | |
| 60 | } | |
| 61 | } | |
Mutations | ||
| 37 |
1.1 |
|
| 50 |
1.1 |
|
| 54 |
1.1 |