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 | sealed interface Option { | |
12 | ||
13 | record Configuration(Optional<Boolean> batching, OptionalInt parallelism, Optional<Executor> executor) { | |
14 | public Configuration { | |
15 | requireNonNull(batching, "batching can't be null"); | |
16 | requireNonNull(parallelism, "parallelism can't be null"); | |
17 | requireNonNull(executor, "executor can't be null"); | |
18 | } | |
19 | } | |
20 | ||
21 | static Configuration process(Option... options) { | |
22 | requireNonNull(options, "options can't be null"); | |
23 | ||
24 | Set<Class<? extends Option>> seen = new HashSet<>(); | |
25 | ||
26 | Optional<Boolean> batching = Optional.empty(); | |
27 | OptionalInt parallelism = OptionalInt.empty(); | |
28 | Optional<Executor> executor = Optional.empty(); | |
29 | ||
30 | for (var option : options) { | |
31 |
1
1. process : negated conditional → KILLED |
if (!seen.add(option.getClass())) { |
32 | throw new IllegalArgumentException("each option can be used at most once, and you configured '%s' multiple times".formatted(switch (option) { | |
33 | case Option.Batching __ -> "batching"; | |
34 | case Option.Parallelism __ -> "parallelism"; | |
35 | case Option.ThreadPool __ -> "executor"; | |
36 | })); | |
37 | } | |
38 | ||
39 | switch (option) { | |
40 | case Batching batchingOption -> batching = Optional.of(batchingOption.batched()); | |
41 | case Parallelism parallelismOption -> parallelism = OptionalInt.of(parallelismOption.parallelism()); | |
42 | case ThreadPool threadPoolOption -> executor = Optional.ofNullable(threadPoolOption.executor()); | |
43 | } | |
44 | } | |
45 | ||
46 |
1
1. process : replaced return value with null for com/pivovarit/collectors/Option::process → KILLED |
return new Configuration(batching, parallelism, executor); |
47 | } | |
48 | ||
49 | record Batching(boolean batched) implements Option { | |
50 | } | |
51 | ||
52 | record ThreadPool(Executor executor) implements Option { | |
53 | public ThreadPool { | |
54 | Preconditions.requireValidExecutor(executor); | |
55 | } | |
56 | } | |
57 | ||
58 | record Parallelism(int parallelism) implements Option { | |
59 | public Parallelism { | |
60 | Preconditions.requireValidParallelism(parallelism); | |
61 | } | |
62 | } | |
63 | ||
64 | static Option executor(Executor executor) { | |
65 |
1
1. executor : replaced return value with null for com/pivovarit/collectors/Option::executor → KILLED |
return new ThreadPool(executor); |
66 | } | |
67 | ||
68 | static Option batched() { | |
69 |
1
1. batched : replaced return value with null for com/pivovarit/collectors/Option::batched → KILLED |
return new Batching(true); |
70 | } | |
71 | ||
72 | static Option parallelism(int parallelism) { | |
73 |
1
1. parallelism : replaced return value with null for com/pivovarit/collectors/Option::parallelism → KILLED |
return new Parallelism(parallelism); |
74 | } | |
75 | } | |
Mutations | ||
31 |
1.1 |
|
46 |
1.1 |
|
65 |
1.1 |
|
69 |
1.1 |
|
73 |
1.1 |