|
1 | 1 | package org.sinytra.adapter.transform.patch; |
2 | 2 |
|
3 | | -import com.google.common.collect.ImmutableMap; |
| 3 | +import com.google.common.collect.ImmutableList; |
4 | 4 | import org.sinytra.adapter.patch.config.PropertyContainer; |
5 | 5 | import org.sinytra.adapter.patch.config.PropertyKey; |
6 | 6 |
|
7 | | -import java.util.HashMap; |
8 | | -import java.util.Map; |
| 7 | +import java.util.*; |
| 8 | +import java.util.function.Consumer; |
9 | 9 | import java.util.function.Predicate; |
10 | 10 |
|
11 | 11 | public class ConfigurationMatcher { |
12 | | - private final Map<PropertyKey<?>, Predicate<?>> matchers; |
| 12 | + private final List<SubMatcher> matchers; |
13 | 13 |
|
14 | | - private ConfigurationMatcher(Map<PropertyKey<?>, Predicate<?>> matchers) { |
15 | | - this.matchers = ImmutableMap.copyOf(matchers); |
| 14 | + private ConfigurationMatcher(List<SubMatcher> matchers) { |
| 15 | + this.matchers = ImmutableList.copyOf(matchers); |
16 | 16 | } |
17 | 17 |
|
18 | | - @SuppressWarnings({"rawtypes", "unchecked"}) |
19 | 18 | public boolean match(PropertyContainer container) { |
20 | | - for (Map.Entry<PropertyKey<?>, Predicate<?>> entry : this.matchers.entrySet()) { |
21 | | - Object value = container.getProperty(entry.getKey()).orElse(null); |
22 | | - if (value == null || !((Predicate) entry.getValue()).test(value)) { |
| 19 | + for (SubMatcher subMatcher : this.matchers) { |
| 20 | + if (!subMatcher.match(container)) { |
23 | 21 | return false; |
24 | 22 | } |
25 | 23 | } |
26 | 24 | return true; |
27 | 25 | } |
28 | 26 |
|
| 27 | + public interface SubMatcher { |
| 28 | + boolean match(PropertyContainer container); |
| 29 | + } |
| 30 | + |
| 31 | + private record SingleMatcher<T>(PropertyKey<T> key, Predicate<T> predicate) implements SubMatcher { |
| 32 | + private SingleMatcher(PropertyKey<T> key, Predicate<T> predicate) { |
| 33 | + this.key = Objects.requireNonNull(key); |
| 34 | + this.predicate = Objects.requireNonNull(predicate); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public boolean match(PropertyContainer container) { |
| 39 | + T value = container.getProperty(this.key).orElse(null); |
| 40 | + return value != null && this.predicate.test(value); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private record OrMatcher(List<SubMatcher> matchers) implements SubMatcher { |
| 45 | + @Override |
| 46 | + public boolean match(PropertyContainer container) { |
| 47 | + for (SubMatcher matcher : this.matchers) { |
| 48 | + if (matcher.match(container)) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + } |
| 52 | + return false; |
| 53 | + } |
| 54 | + } |
| 55 | + |
29 | 56 | public static Builder builder() { |
30 | 57 | return new Builder(); |
31 | 58 | } |
32 | 59 |
|
33 | 60 | public static class Builder { |
34 | | - private final Map<PropertyKey<?>, Predicate<?>> matchers = new HashMap<>(); |
| 61 | + private final Map<PropertyKey<?>, Predicate<?>> singleMatchers = new HashMap<>(); |
| 62 | + private final List<SubMatcher> matchers = new ArrayList<>(); |
35 | 63 |
|
36 | 64 | @SuppressWarnings({"rawtypes", "unchecked"}) |
37 | 65 | public <T> Builder match(PropertyKey<T> prop, Predicate<T> predicate) { |
38 | | - this.matchers.compute(prop, (k, v) -> v == null ? predicate : v.or((Predicate) predicate)); |
| 66 | + this.singleMatchers.compute(prop, (k, v) -> v == null ? predicate : v.or((Predicate) predicate)); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + public Builder match(SubMatcher subMatcher) { |
| 71 | + this.matchers.add(subMatcher); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + public Builder or(Consumer<Builder> subBuilder) { |
| 76 | + Builder sub = new Builder(); |
| 77 | + subBuilder.accept(sub); |
| 78 | + match(new OrMatcher(sub.buildMatchers())); |
39 | 79 | return this; |
40 | 80 | } |
41 | 81 |
|
| 82 | + @SuppressWarnings({"rawtypes", "unchecked"}) |
| 83 | + public List<SubMatcher> buildMatchers() { |
| 84 | + List<SubMatcher> all = new ArrayList<>(); |
| 85 | + this.singleMatchers.forEach((key, pred) -> { |
| 86 | + SingleMatcher matcher = new SingleMatcher(key, pred); |
| 87 | + all.addFirst(matcher); |
| 88 | + }); |
| 89 | + all.addAll(this.matchers); |
| 90 | + return all; |
| 91 | + } |
| 92 | + |
42 | 93 | public ConfigurationMatcher build() { |
43 | | - return new ConfigurationMatcher(this.matchers); |
| 94 | + return new ConfigurationMatcher(buildMatchers()); |
44 | 95 | } |
45 | 96 | } |
46 | 97 | } |
0 commit comments