From cedc5108fc95e2529f6bcb851a00cced9117ee9e Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 7 May 2017 13:56:42 +0100 Subject: [PATCH] Refactor Patterns to PatternCache --- .../lucko/luckperms/bukkit/BukkitCommand.java | 3 +- .../lucko/luckperms/bungee/BungeeCommand.java | 3 +- .../processors/RegexProcessor.java | 21 +++++------ .../common/commands/CommandManager.java | 2 + .../impl/log/subcommands/LogRecent.java | 4 +- .../impl/log/subcommands/LogUserHistory.java | 4 +- .../commands/impl/user/UserMainCommand.java | 4 +- .../luckperms/common/commands/utils/Util.java | 5 ++- .../luckperms/common/core/NodeBuilder.java | 11 ++++-- .../luckperms/common/core/NodeFactory.java | 26 ++++++------- .../common/core/model/ImmutableNode.java | 12 +++--- .../lucko/luckperms/common/data/Importer.java | 3 +- .../Patterns.java => utils/PatternCache.java} | 37 +++++++++++-------- .../lucko/luckperms/sponge/SpongeCommand.java | 3 +- 14 files changed, 72 insertions(+), 66 deletions(-) rename common/src/main/java/me/lucko/luckperms/common/{constants/Patterns.java => utils/PatternCache.java} (71%) diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCommand.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCommand.java index a10e6786..9ebdb976 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCommand.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCommand.java @@ -30,7 +30,6 @@ import com.google.common.base.Splitter; import me.lucko.luckperms.common.commands.CommandManager; import me.lucko.luckperms.common.commands.utils.Util; -import me.lucko.luckperms.common.constants.Patterns; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -53,7 +52,7 @@ public class BukkitCommand extends CommandManager implements CommandExecutor, Ta onCommand( plugin.getSenderFactory().wrap(sender), label, - Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(Joiner.on(' ').join(args))) + Util.stripQuotes(Splitter.on(COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(Joiner.on(' ').join(args))) ); return true; } diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCommand.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCommand.java index 87878a94..4f5d9aaf 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCommand.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCommand.java @@ -30,7 +30,6 @@ import com.google.common.base.Splitter; import me.lucko.luckperms.common.commands.CommandManager; import me.lucko.luckperms.common.commands.utils.Util; -import me.lucko.luckperms.common.constants.Patterns; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.plugin.Command; @@ -53,7 +52,7 @@ class BungeeCommand extends Command implements TabExecutor { manager.onCommand( plugin.getSenderFactory().wrap(sender), "lpb", - Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(Joiner.on(' ').join(args))) + Util.stripQuotes(Splitter.on(CommandManager.COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(Joiner.on(' ').join(args))) ); } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/processors/RegexProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculators/processors/RegexProcessor.java index 318b677f..cef13411 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/processors/RegexProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/processors/RegexProcessor.java @@ -27,24 +27,19 @@ package me.lucko.luckperms.common.calculators.processors; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.calculators.PermissionProcessor; -import me.lucko.luckperms.common.constants.Patterns; +import me.lucko.luckperms.common.utils.PatternCache; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; public class RegexProcessor implements PermissionProcessor { - private Map regexPermissions = new ConcurrentHashMap<>(); + private Map regexPermissions = new ConcurrentHashMap<>(); @Override public Tristate hasPermission(String permission) { - for (Map.Entry e : regexPermissions.entrySet()) { - Pattern p = Patterns.compile(e.getKey()); - if (p == null) { - continue; - } - - if (p.matcher(permission).matches()) { + for (Map.Entry e : regexPermissions.entrySet()) { + if (e.getKey().matcher(permission).matches()) { return Tristate.fromBoolean(e.getValue()); } } @@ -61,9 +56,13 @@ public class RegexProcessor implements PermissionProcessor { } String pattern = e.getKey().substring(2); - Patterns.compile(pattern); // Cache the lookup for later. + Pattern p = PatternCache.compile(pattern); - regexPermissions.put(pattern, e.getValue()); + if (p == null) { + continue; + } + + regexPermissions.put(p, e.getValue()); } } } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java b/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java index a3c43526..18c6e005 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/CommandManager.java @@ -69,9 +69,11 @@ import java.util.Optional; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; +import java.util.regex.Pattern; import java.util.stream.Collectors; public class CommandManager { + public static final Pattern COMMAND_SEPARATOR_PATTERN = Pattern.compile(" (?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)"); @Getter private final LuckPermsPlugin plugin; diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogRecent.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogRecent.java index 8a3a3488..1bf232b4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogRecent.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogRecent.java @@ -32,8 +32,8 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.DataConstraints; import me.lucko.luckperms.common.constants.Message; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -105,7 +105,7 @@ public class LogRecent extends SubCommand { u = Util.parseUuid(s); if (u == null) { if (s.length() <= 16) { - if (Patterns.NON_USERNAME.matcher(s).find()) { + if (!DataConstraints.PLAYER_USERNAME_TEST.test(s)) { Message.USER_INVALID_ENTRY.send(sender, s); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogUserHistory.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogUserHistory.java index 06f7a09e..78d86e5a 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogUserHistory.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/log/subcommands/LogUserHistory.java @@ -32,8 +32,8 @@ import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.abstraction.SubCommand; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.DataConstraints; import me.lucko.luckperms.common.constants.Message; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.data.Log; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -103,7 +103,7 @@ public class LogUserHistory extends SubCommand { } if (user.length() <= 16) { - if (Patterns.NON_USERNAME.matcher(user).find()) { + if (!DataConstraints.PLAYER_USERNAME_TEST.test(user)) { Message.USER_INVALID_ENTRY.send(sender, user); return CommandResult.INVALID_ARGS; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java index f0b6551a..979ffccf 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/user/UserMainCommand.java @@ -36,8 +36,8 @@ import me.lucko.luckperms.common.commands.impl.generic.parent.CommandParent; import me.lucko.luckperms.common.commands.impl.generic.permission.CommandPermission; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.Util; +import me.lucko.luckperms.common.constants.DataConstraints; import me.lucko.luckperms.common.constants.Message; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -65,7 +65,7 @@ public class UserMainCommand extends MainCommand { UUID u = Util.parseUuid(target); if (u == null) { if (target.length() <= 16) { - if (Patterns.NON_USERNAME.matcher(target).find()) { + if (!DataConstraints.PLAYER_USERNAME_TEST.test(target)) { Message.USER_INVALID_ENTRY.send(sender, target); return null; } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java b/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java index 6fa0c32a..9738a6da 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/utils/Util.java @@ -34,7 +34,6 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Message; -import me.lucko.luckperms.common.constants.Patterns; import io.github.mkremins.fanciful.FancyMessage; @@ -45,9 +44,11 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.UUID; +import java.util.regex.Pattern; @UtilityClass public class Util { + private static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf('§') + "[0-9A-FK-OR]"); /** * Sends a message to the sender, formatted with the plugin prefix and color scheme @@ -111,7 +112,7 @@ public class Util { * @return the message without color */ public static String stripColor(String s) { - return s == null ? null : Patterns.STRIP_COLOR_PATTERN.matcher(s).replaceAll(""); + return s == null ? null : STRIP_COLOR_PATTERN.matcher(s).replaceAll(""); } public static List> divideList(Iterable source, int size) { diff --git a/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java b/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java index f682f16a..1580f43b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/NodeBuilder.java @@ -34,18 +34,21 @@ import com.google.common.base.Splitter; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.MutableContextSet; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.core.model.ImmutableNode; +import me.lucko.luckperms.common.utils.PatternCache; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.regex.Pattern; /** * Builds Nodes */ @RequiredArgsConstructor(access = AccessLevel.PACKAGE) class NodeBuilder implements Node.Builder { + private static final Pattern NODE_CONTEXTS_PATTERN = Pattern.compile("\\(.+\\).*"); + private final String permission; private final MutableContextSet extraContexts = MutableContextSet.create(); private Boolean value = true; @@ -58,15 +61,15 @@ class NodeBuilder implements Node.Builder { if (!shouldConvertContexts) { this.permission = permission; } else { - if (!Patterns.NODE_CONTEXTS.matcher(permission).matches()) { + if (!NODE_CONTEXTS_PATTERN.matcher(permission).matches()) { this.permission = permission; } else { - List contextParts = Splitter.on(Patterns.compileDelimitedMatcher(")", "\\")).limit(2).splitToList(permission.substring(1)); + List contextParts = Splitter.on(PatternCache.compileDelimitedMatcher(")", "\\")).limit(2).splitToList(permission.substring(1)); // 0 = context, 1 = node this.permission = contextParts.get(1); try { - Map map = Splitter.on(Patterns.compileDelimitedMatcher(",", "\\")).withKeyValueSeparator(Splitter.on(Patterns.compileDelimitedMatcher("=", "\\"))).split(contextParts.get(0)); + Map map = Splitter.on(PatternCache.compileDelimitedMatcher(",", "\\")).withKeyValueSeparator(Splitter.on(PatternCache.compileDelimitedMatcher("=", "\\"))).split(contextParts.get(0)); for (Map.Entry e : map.entrySet()) { this.withExtraContext(NodeFactory.unescapeDelimiters(e.getKey(), "=", "(", ")", ","), NodeFactory.unescapeDelimiters(e.getValue(), "=", "(", ")", ",")); } diff --git a/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java b/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java index 352bd426..7ebb014d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/NodeFactory.java @@ -34,8 +34,8 @@ import com.google.common.base.Splitter; import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.context.ContextSet; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.core.model.Group; +import me.lucko.luckperms.common.utils.PatternCache; import java.util.List; import java.util.Map; @@ -65,18 +65,18 @@ public class NodeFactory { public static Node.Builder builderFromSerializedNode(String s, Boolean b) { // if contains / - if (Patterns.compileDelimitedMatcher("/", "\\").matcher(s).find()) { - List parts = Splitter.on(Patterns.compileDelimitedMatcher("/", "\\")).limit(2).splitToList(s); + if (PatternCache.compileDelimitedMatcher("/", "\\").matcher(s).find()) { + List parts = Splitter.on(PatternCache.compileDelimitedMatcher("/", "\\")).limit(2).splitToList(s); // 0=server(+world) 1=node // WORLD SPECIFIC // if parts[0] contains - - if (Patterns.compileDelimitedMatcher("-", "\\").matcher(parts.get(0)).find()) { - List serverParts = Splitter.on(Patterns.compileDelimitedMatcher("-", "\\")).limit(2).splitToList(parts.get(0)); + if (PatternCache.compileDelimitedMatcher("-", "\\").matcher(parts.get(0)).find()) { + List serverParts = Splitter.on(PatternCache.compileDelimitedMatcher("-", "\\")).limit(2).splitToList(parts.get(0)); // 0=server 1=world // if parts[1] contains $ - if (Patterns.compileDelimitedMatcher("$", "\\").matcher(parts.get(1)).find()) { + if (PatternCache.compileDelimitedMatcher("$", "\\").matcher(parts.get(1)).find()) { List tempParts = Splitter.on('$').limit(2).splitToList(parts.get(1)); return new NodeBuilder(tempParts.get(0), true).setServer(serverParts.get(0)).setWorld(serverParts.get(1)) .setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); @@ -87,8 +87,8 @@ public class NodeFactory { // SERVER BUT NOT WORLD SPECIFIC // if parts[1] contains $ - if (Patterns.compileDelimitedMatcher("$", "\\").matcher(parts.get(1)).find()) { - List tempParts = Splitter.on(Patterns.compileDelimitedMatcher("$", "\\")).limit(2).splitToList(parts.get(1)); + if (PatternCache.compileDelimitedMatcher("$", "\\").matcher(parts.get(1)).find()) { + List tempParts = Splitter.on(PatternCache.compileDelimitedMatcher("$", "\\")).limit(2).splitToList(parts.get(1)); return new NodeBuilder(tempParts.get(0), true).setServer(parts.get(0)).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); } else { return new NodeBuilder(parts.get(1), true).setServer(parts.get(0)).setValue(b); @@ -98,8 +98,8 @@ public class NodeFactory { // NOT SERVER SPECIFIC // if s contains $ - if (Patterns.compileDelimitedMatcher("$", "\\").matcher(s).find()) { - List tempParts = Splitter.on(Patterns.compileDelimitedMatcher("$", "\\")).limit(2).splitToList(s); + if (PatternCache.compileDelimitedMatcher("$", "\\").matcher(s).find()) { + List tempParts = Splitter.on(PatternCache.compileDelimitedMatcher("$", "\\")).limit(2).splitToList(s); return new NodeBuilder(tempParts.get(0), true).setExpiry(Long.parseLong(tempParts.get(1))).setValue(b); } else { return new NodeBuilder(s, true).setValue(b); @@ -214,7 +214,7 @@ public class NodeFactory { return false; } String parts = s.substring("meta.".length()); - return Patterns.compileDelimitedMatcher(".", "\\").matcher(parts).find(); + return PatternCache.compileDelimitedMatcher(".", "\\").matcher(parts).find(); } private static boolean isChatMetaNode(String type, String s) { @@ -223,11 +223,11 @@ public class NodeFactory { } String parts = s.substring((type + ".").length()); - if (!Patterns.compileDelimitedMatcher(".", "\\").matcher(parts).find()) { + if (!PatternCache.compileDelimitedMatcher(".", "\\").matcher(parts).find()) { return false; } - List metaParts = Splitter.on(Patterns.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(parts); + List metaParts = Splitter.on(PatternCache.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(parts); String priority = metaParts.get(0); try { Integer.parseInt(priority); diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java b/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java index 786c9579..bf5d2ece 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java @@ -40,8 +40,8 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ImmutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.utils.PatternCache; import me.lucko.luckperms.common.utils.ShorthandParser; import java.util.Collections; @@ -70,7 +70,7 @@ public final class ImmutableNode implements Node { Set expandedThisStr = ShorthandParser.parseShorthand(thisStr, false); if (str.toLowerCase().startsWith("r=") && applyRegex) { - Pattern p = Patterns.compile(str.substring(2)); + Pattern p = PatternCache.compile(str.substring(2)); if (p == null) { return false; } @@ -84,7 +84,7 @@ public final class ImmutableNode implements Node { } if (thisStr.toLowerCase().startsWith("r=") && applyRegex) { - Pattern p = Patterns.compile(thisStr.substring(2)); + Pattern p = PatternCache.compile(thisStr.substring(2)); if (p == null) { return false; } @@ -231,20 +231,20 @@ public final class ImmutableNode implements Node { isMeta = NodeFactory.isMetaNode(this.permission); if (isMeta) { - List metaPart = Splitter.on(Patterns.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(getPermission().substring("meta.".length())); + List metaPart = Splitter.on(PatternCache.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(getPermission().substring("meta.".length())); meta = Maps.immutableEntry(MetaUtils.unescapeCharacters(metaPart.get(0)), MetaUtils.unescapeCharacters(metaPart.get(1))); } isPrefix = NodeFactory.isPrefixNode(this.permission); if (isPrefix) { - List prefixPart = Splitter.on(Patterns.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(getPermission().substring("prefix.".length())); + List prefixPart = Splitter.on(PatternCache.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(getPermission().substring("prefix.".length())); Integer i = Integer.parseInt(prefixPart.get(0)); prefix = Maps.immutableEntry(i, MetaUtils.unescapeCharacters(prefixPart.get(1))); } isSuffix = NodeFactory.isSuffixNode(this.permission); if (isSuffix) { - List suffixPart = Splitter.on(Patterns.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(getPermission().substring("suffix.".length())); + List suffixPart = Splitter.on(PatternCache.compileDelimitedMatcher(".", "\\")).limit(2).splitToList(getPermission().substring("suffix.".length())); Integer i = Integer.parseInt(suffixPart.get(0)); suffix = Maps.immutableEntry(i, MetaUtils.unescapeCharacters(suffixPart.get(1))); } diff --git a/common/src/main/java/me/lucko/luckperms/common/data/Importer.java b/common/src/main/java/me/lucko/luckperms/common/data/Importer.java index 307d2ff1..5f0d9f95 100644 --- a/common/src/main/java/me/lucko/luckperms/common/data/Importer.java +++ b/common/src/main/java/me/lucko/luckperms/common/data/Importer.java @@ -38,7 +38,6 @@ import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.Util; import me.lucko.luckperms.common.constants.Constants; import me.lucko.luckperms.common.constants.Message; -import me.lucko.luckperms.common.constants.Patterns; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -109,7 +108,7 @@ public class Importer implements Runnable { CommandResult result = commandManager.onCommand( fake, "lp", - Util.stripQuotes(Splitter.on(Patterns.COMMAND_SEPARATOR).omitEmptyStrings().splitToList(command)) + Util.stripQuotes(Splitter.on(CommandManager.COMMAND_SEPARATOR_PATTERN).omitEmptyStrings().splitToList(command)) ).get(); getResult(index, command).setResult(result); diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java b/common/src/main/java/me/lucko/luckperms/common/utils/PatternCache.java similarity index 71% rename from common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java rename to common/src/main/java/me/lucko/luckperms/common/utils/PatternCache.java index 3038143c..264c4138 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java +++ b/common/src/main/java/me/lucko/luckperms/common/utils/PatternCache.java @@ -23,8 +23,9 @@ * SOFTWARE. */ -package me.lucko.luckperms.common.constants; +package me.lucko.luckperms.common.utils; +import lombok.AllArgsConstructor; import lombok.experimental.UtilityClass; import com.github.benmanes.caffeine.cache.Caffeine; @@ -33,30 +34,29 @@ import com.google.common.collect.Maps; import java.util.Map; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; @UtilityClass -public class Patterns { - private static final LoadingCache CACHE = Caffeine.newBuilder().build(Pattern::compile); +public class PatternCache { + + private static final NullablePattern NULL_PATTERN = new NullablePattern(null); + + private static final LoadingCache CACHE = Caffeine.newBuilder().build(s -> { + try { + return new NullablePattern(Pattern.compile(s)); + } catch (PatternSyntaxException e) { + return NULL_PATTERN; + } + }); + private static final LoadingCache, String> DELIMITER_CACHE = Caffeine.newBuilder() .build(e -> { // note the reversed order return "(?