Skip to content

Commit 800709c

Browse files
committed
Properly annotate parameters for matcher classes
1 parent 935cbad commit 800709c

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

src/main/java/org/mvplugins/multiverse/core/utils/matcher/ExactStringMatcher.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.mvplugins.multiverse.core.utils.matcher;
22

33
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
46

57
import java.util.Collection;
68
import java.util.HashSet;
@@ -34,7 +36,7 @@ public ExactStringMatcher() {
3436
* @since 5.2
3537
*/
3638
@ApiStatus.AvailableSince("5.2")
37-
public ExactStringMatcher(String exactMatch) {
39+
public ExactStringMatcher(@NotNull String exactMatch) {
3840
this.exactMatches = new HashSet<>();
3941
this.exactMatches.add(exactMatch);
4042
}
@@ -47,7 +49,7 @@ public ExactStringMatcher(String exactMatch) {
4749
* @since 5.2
4850
*/
4951
@ApiStatus.AvailableSince("5.2")
50-
public ExactStringMatcher(Collection<String> exactMatches) {
52+
public ExactStringMatcher(@NotNull Collection<String> exactMatches) {
5153
this.exactMatches = new HashSet<>(exactMatches);
5254
}
5355

@@ -59,15 +61,15 @@ public ExactStringMatcher(Collection<String> exactMatches) {
5961
* @since 5.2
6062
*/
6163
@ApiStatus.AvailableSince("5.2")
62-
public void addExactMatch(String value) {
64+
public void addExactMatch(@NotNull String value) {
6365
this.exactMatches.add(value);
6466
}
6567

6668
/**
6769
* {@inheritDoc}
6870
*/
6971
@Override
70-
public boolean matches(String value) {
72+
public boolean matches(@Nullable String value) {
7173
return exactMatches.contains(value);
7274
}
7375
}

src/main/java/org/mvplugins/multiverse/core/utils/matcher/MatcherGroup.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.dumptruckman.minecraft.util.Logging;
44
import org.jetbrains.annotations.ApiStatus;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
57

68
import java.util.ArrayList;
79
import java.util.Collection;
@@ -38,7 +40,7 @@ public MatcherGroup() {
3840
* @since 5.2
3941
*/
4042
@ApiStatus.AvailableSince("5.2")
41-
public MatcherGroup(Collection<String> matchStrings) {
43+
public MatcherGroup(@NotNull Collection<String> matchStrings) {
4244
this();
4345
for (String matchString : matchStrings) {
4446
addMatcher(matchString);
@@ -53,7 +55,10 @@ public MatcherGroup(Collection<String> matchStrings) {
5355
* @since 5.2
5456
*/
5557
@ApiStatus.AvailableSince("5.2")
56-
public void addMatcher(String matchString) {
58+
public void addMatcher(@Nullable String matchString) {
59+
if (matchString == null || matchString.isEmpty()) {
60+
return;
61+
}
5762
if (isExact(matchString)) {
5863
Logging.warning("Exact: " + matchString);
5964
exactMatcher.addExactMatch(matchString);
@@ -62,7 +67,7 @@ public void addMatcher(String matchString) {
6267
}
6368
}
6469

65-
private boolean isExact(String matcherString) {
70+
private boolean isExact(@NotNull String matcherString) {
6671
return !matcherString.contains("*") && !matcherString.startsWith("r=");
6772
}
6873

@@ -74,15 +79,15 @@ private boolean isExact(String matcherString) {
7479
* @since 5.2
7580
*/
7681
@ApiStatus.AvailableSince("5.2")
77-
public void addMatcher(StringMatcher matcher) {
82+
public void addMatcher(@NotNull StringMatcher matcher) {
7883
stringMatchers.add(matcher);
7984
}
8085

8186
/**
8287
* {@inheritDoc}
8388
*/
8489
@Override
85-
public boolean matches(String value) {
90+
public boolean matches(@Nullable String value) {
8691
if (exactMatcher.matches(value)) {
8792
return true;
8893
}

src/main/java/org/mvplugins/multiverse/core/utils/matcher/RegexStringMatcher.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ private Pattern compileRegex(String regexString) {
4848
* {@inheritDoc}
4949
*/
5050
@Override
51-
public boolean matches(String value) {
52-
if (regexPattern == null) {
53-
return false; // If the regex pattern could not be compiled, we cannot match
51+
public boolean matches(@Nullable String value) {
52+
if (regexPattern == null || value == null) {
53+
return false;
5454
}
5555
return regexPattern.matcher(value).matches();
5656
}

src/main/java/org/mvplugins/multiverse/core/utils/matcher/StringMatcher.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.mvplugins.multiverse.core.utils.matcher;
22

33
import org.jetbrains.annotations.ApiStatus;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
46

57
import java.util.List;
68
import java.util.stream.Collectors;
@@ -26,7 +28,7 @@ public interface StringMatcher {
2628
* @since 5.2
2729
*/
2830
@ApiStatus.AvailableSince("5.2")
29-
static StringMatcher fromString(String matcherString) {
31+
static @NotNull StringMatcher fromString(@NotNull String matcherString) {
3032
if (matcherString.startsWith("r=")) {
3133
return new RegexStringMatcher(matcherString);
3234
} else if (matcherString.contains("*")) {
@@ -45,7 +47,7 @@ static StringMatcher fromString(String matcherString) {
4547
* @since 5.2
4648
*/
4749
@ApiStatus.AvailableSince("5.2")
48-
boolean matches(String value);
50+
boolean matches(@Nullable String value);
4951

5052
/**
5153
* Filters a list of strings, returning only those that match the pattern defined by this StringMatcher.
@@ -57,7 +59,7 @@ static StringMatcher fromString(String matcherString) {
5759
* @since 5.2
5860
*/
5961
@ApiStatus.AvailableSince("5.2")
60-
default List<String> filter(List<String> values) {
62+
default @NotNull List<String> filter(@NotNull List<String> values) {
6163
return values.stream()
6264
.filter(this::matches)
6365
.collect(Collectors.toList());

src/main/java/org/mvplugins/multiverse/core/utils/matcher/WildcardStringMatcher.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.dumptruckman.minecraft.util.Logging;
44
import io.vavr.control.Try;
55
import org.jetbrains.annotations.ApiStatus;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
68

79
import java.util.regex.Pattern;
810

@@ -27,7 +29,7 @@ public class WildcardStringMatcher implements StringMatcher {
2729
* @param wildcard the wildcard string to match against.
2830
*/
2931
@ApiStatus.AvailableSince("5.2")
30-
public WildcardStringMatcher(String wildcard) {
32+
public WildcardStringMatcher(@NotNull String wildcard) {
3133
this.wildcard = wildcard;
3234
this.pattern = Try.of(() -> Pattern.compile(("\\Q" + wildcard + "\\E").replace("*", "\\E.*\\Q")))
3335
.onFailure(ex -> Logging.warning("Failed to compile wildcard '%s': %s",
@@ -39,7 +41,10 @@ public WildcardStringMatcher(String wildcard) {
3941
* {@inheritDoc}
4042
*/
4143
@Override
42-
public boolean matches(String value) {
44+
public boolean matches(@Nullable String value) {
45+
if (pattern == null || value == null) {
46+
return false;
47+
}
4348
return pattern.matcher(value).matches();
4449
}
4550
}

0 commit comments

Comments
 (0)