|
9 | 9 | import static java.util.stream.Collectors.joining; |
10 | 10 |
|
11 | 11 | import java.util.Collection; |
| 12 | +import java.util.Collections; |
12 | 13 | import java.util.LinkedHashSet; |
13 | 14 | import java.util.Set; |
14 | 15 | import java.util.StringJoiner; |
|
29 | 30 | public final class IncludeExcludePredicate implements Predicate<String> { |
30 | 31 |
|
31 | 32 | private final boolean globMatchingEnabled; |
32 | | - @Nullable private final Set<String> included; |
33 | | - @Nullable private final Set<String> excluded; |
| 33 | + private final Set<String> included; |
| 34 | + private final Set<String> excluded; |
34 | 35 | private final Predicate<String> predicate; |
35 | 36 |
|
36 | 37 | private IncludeExcludePredicate( |
37 | 38 | @Nullable Collection<String> included, |
38 | 39 | @Nullable Collection<String> excluded, |
39 | 40 | boolean globMatchingEnabled) { |
40 | 41 | this.globMatchingEnabled = globMatchingEnabled; |
41 | | - this.included = included == null ? null : new LinkedHashSet<>(included); |
42 | | - this.excluded = excluded == null ? null : new LinkedHashSet<>(excluded); |
43 | | - if (this.included != null && this.excluded != null) { |
44 | | - this.predicate = |
45 | | - includedPredicate(this.included, globMatchingEnabled) |
46 | | - .and(excludedPredicate(this.excluded, globMatchingEnabled)); |
47 | | - } else if (this.included == null && this.excluded != null) { |
| 42 | + this.included = included == null ? Collections.emptySet() : new LinkedHashSet<>(included); |
| 43 | + this.excluded = excluded == null ? Collections.emptySet() : new LinkedHashSet<>(excluded); |
| 44 | + if (this.included.isEmpty() && this.excluded.isEmpty()) { |
| 45 | + throw new IllegalArgumentException( |
| 46 | + "At least one of include or exclude patterns must not be null or empty"); |
| 47 | + } |
| 48 | + if (this.included.isEmpty()) { |
48 | 49 | this.predicate = excludedPredicate(this.excluded, globMatchingEnabled); |
49 | | - } else if (this.excluded == null && this.included != null) { |
| 50 | + } else if (this.excluded.isEmpty()) { |
50 | 51 | this.predicate = includedPredicate(this.included, globMatchingEnabled); |
51 | 52 | } else { |
52 | | - throw new IllegalArgumentException( |
53 | | - "At least one of includedPatterns or excludedPatterns must not be null"); |
| 53 | + this.predicate = |
| 54 | + includedPredicate(this.included, globMatchingEnabled) |
| 55 | + .and(excludedPredicate(this.excluded, globMatchingEnabled)); |
54 | 56 | } |
55 | 57 | } |
56 | 58 |
|
@@ -85,10 +87,10 @@ public boolean test(String s) { |
85 | 87 | public String toString() { |
86 | 88 | StringJoiner joiner = new StringJoiner(", ", "IncludeExcludePredicate{", "}"); |
87 | 89 | joiner.add("globMatchingEnabled=" + globMatchingEnabled); |
88 | | - if (included != null) { |
| 90 | + if (!included.isEmpty()) { |
89 | 91 | joiner.add("included=" + included.stream().collect(joining(", ", "[", "]"))); |
90 | 92 | } |
91 | | - if (excluded != null) { |
| 93 | + if (!excluded.isEmpty()) { |
92 | 94 | joiner.add("excluded=" + excluded.stream().collect(joining(", ", "[", "]"))); |
93 | 95 | } |
94 | 96 | return joiner.toString(); |
|
0 commit comments