Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.mvplugins.multiverse.core.utils.matcher;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashSet;
Expand Down Expand Up @@ -34,7 +36,7 @@ public ExactStringMatcher() {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public ExactStringMatcher(String exactMatch) {
public ExactStringMatcher(@NotNull String exactMatch) {
this.exactMatches = new HashSet<>();
this.exactMatches.add(exactMatch);
}
Expand All @@ -47,7 +49,7 @@ public ExactStringMatcher(String exactMatch) {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public ExactStringMatcher(Collection<String> exactMatches) {
public ExactStringMatcher(@NotNull Collection<String> exactMatches) {
this.exactMatches = new HashSet<>(exactMatches);
}

Expand All @@ -59,15 +61,15 @@ public ExactStringMatcher(Collection<String> exactMatches) {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public void addExactMatch(String value) {
public void addExactMatch(@NotNull String value) {
this.exactMatches.add(value);
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(String value) {
public boolean matches(@Nullable String value) {
return exactMatches.contains(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.dumptruckman.minecraft.util.Logging;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -38,7 +40,7 @@ public MatcherGroup() {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public MatcherGroup(Collection<String> matchStrings) {
public MatcherGroup(@NotNull Collection<String> matchStrings) {
this();
for (String matchString : matchStrings) {
addMatcher(matchString);
Expand All @@ -53,7 +55,10 @@ public MatcherGroup(Collection<String> matchStrings) {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public void addMatcher(String matchString) {
public void addMatcher(@Nullable String matchString) {
if (matchString == null || matchString.isEmpty()) {
return;
}
if (isExact(matchString)) {
Logging.warning("Exact: " + matchString);
exactMatcher.addExactMatch(matchString);
Expand All @@ -62,7 +67,7 @@ public void addMatcher(String matchString) {
}
}

private boolean isExact(String matcherString) {
private boolean isExact(@NotNull String matcherString) {
return !matcherString.contains("*") && !matcherString.startsWith("r=");
}

Expand All @@ -74,15 +79,15 @@ private boolean isExact(String matcherString) {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public void addMatcher(StringMatcher matcher) {
public void addMatcher(@NotNull StringMatcher matcher) {
stringMatchers.add(matcher);
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(String value) {
public boolean matches(@Nullable String value) {
if (exactMatcher.matches(value)) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.mvplugins.multiverse.core.utils.matcher;

import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Try;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.regex.Pattern;

Expand All @@ -12,8 +16,8 @@
*/
@ApiStatus.AvailableSince("5.2")
public class RegexStringMatcher implements StringMatcher {
private final String regexString;
private final Pattern regexPattern;
private final @NotNull String regexString;
private final @Nullable Pattern regexPattern;

/**
* Creates a new RegexStringMatcher with a regex string. 'r=' prefix will be stripped if present.
Expand All @@ -23,7 +27,7 @@ public class RegexStringMatcher implements StringMatcher {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
public RegexStringMatcher(String regexString) {
public RegexStringMatcher(@NotNull String regexString) {
this.regexString = regexString;
this.regexPattern = compileRegex(regexString);
}
Expand All @@ -32,14 +36,22 @@ private Pattern compileRegex(String regexString) {
if (regexString.startsWith("r=")) {
regexString = regexString.substring(2);
}
return Pattern.compile(regexString);

String finalRegexString = regexString;
return Try.of(() -> Pattern.compile(finalRegexString))
.onFailure(ex -> Logging.warning("Failed to compile regex '%s': %s",
finalRegexString, ex.getMessage()))
.getOrNull();
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(String value) {
public boolean matches(@Nullable String value) {
if (regexPattern == null || value == null) {
return false;
}
return regexPattern.matcher(value).matches();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.mvplugins.multiverse.core.utils.matcher;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -26,7 +28,7 @@ public interface StringMatcher {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
static StringMatcher fromString(String matcherString) {
static @NotNull StringMatcher fromString(@NotNull String matcherString) {
if (matcherString.startsWith("r=")) {
return new RegexStringMatcher(matcherString);
} else if (matcherString.contains("*")) {
Expand All @@ -45,7 +47,7 @@ static StringMatcher fromString(String matcherString) {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
boolean matches(String value);
boolean matches(@Nullable String value);

/**
* Filters a list of strings, returning only those that match the pattern defined by this StringMatcher.
Expand All @@ -57,7 +59,7 @@ static StringMatcher fromString(String matcherString) {
* @since 5.2
*/
@ApiStatus.AvailableSince("5.2")
default List<String> filter(List<String> values) {
default @NotNull List<String> filter(@NotNull List<String> values) {
return values.stream()
.filter(this::matches)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.mvplugins.multiverse.core.utils.matcher;

import com.dumptruckman.minecraft.util.Logging;
import io.vavr.control.Try;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.regex.Pattern;

Expand All @@ -25,16 +29,22 @@ public class WildcardStringMatcher implements StringMatcher {
* @param wildcard the wildcard string to match against.
*/
@ApiStatus.AvailableSince("5.2")
public WildcardStringMatcher(String wildcard) {
public WildcardStringMatcher(@NotNull String wildcard) {
this.wildcard = wildcard;
this.pattern = Pattern.compile(("\\Q" + wildcard + "\\E").replace("*", "\\E.*\\Q"));
this.pattern = Try.of(() -> Pattern.compile(("\\Q" + wildcard + "\\E").replace("*", "\\E.*\\Q")))
.onFailure(ex -> Logging.warning("Failed to compile wildcard '%s': %s",
wildcard, ex.getMessage()))
.getOrNull();
}

/**
* {@inheritDoc}
*/
@Override
public boolean matches(String value) {
public boolean matches(@Nullable String value) {
if (pattern == null || value == null) {
return false;
}
return pattern.matcher(value).matches();
}
}
Loading