From ae63a42c2e95a04ddd20fe5022da652b076118fb Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 21 Feb 2019 12:37:23 +0000 Subject: [PATCH] Slightly optimize regex and wildcard processors --- .../calculator/processor/RegexProcessor.java | 16 +++++++++------- .../calculator/processor/WildcardProcessor.java | 15 ++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/calculator/processor/RegexProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculator/processor/RegexProcessor.java index b1ec9cbae..961902da3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculator/processor/RegexProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculator/processor/RegexProcessor.java @@ -25,7 +25,8 @@ package me.lucko.luckperms.common.calculator.processor; -import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Maps; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.nodetype.types.RegexType; @@ -33,28 +34,28 @@ import me.lucko.luckperms.common.calculator.result.TristateResult; import me.lucko.luckperms.common.node.model.NodeTypes; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.regex.Pattern; public class RegexProcessor extends AbstractPermissionProcessor implements PermissionProcessor { private static final TristateResult.Factory RESULT_FACTORY = new TristateResult.Factory(RegexProcessor.class); - private Map regexPermissions = Collections.emptyMap(); + private List> regexPermissions = Collections.emptyList(); @Override public TristateResult hasPermission(String permission) { - for (Map.Entry e : this.regexPermissions.entrySet()) { + for (Map.Entry e : this.regexPermissions) { if (e.getKey().matcher(permission).matches()) { - return RESULT_FACTORY.result(Tristate.fromBoolean(e.getValue()), "pattern: " + e.getKey().pattern()); + return e.getValue(); } } - return TristateResult.UNDEFINED; } @Override public void refresh() { - ImmutableMap.Builder builder = ImmutableMap.builder(); + ImmutableList.Builder> builder = ImmutableList.builder(); for (Map.Entry e : this.sourceMap.entrySet()) { RegexType regexType = NodeTypes.parseRegexType(e.getKey()); if (regexType == null) { @@ -66,7 +67,8 @@ public class RegexProcessor extends AbstractPermissionProcessor implements Permi continue; } - builder.put(pattern, e.getValue()); + TristateResult value = RESULT_FACTORY.result(Tristate.fromBoolean(e.getValue()), "pattern: " + pattern.pattern()); + builder.add(Maps.immutableEntry(pattern, value)); } this.regexPermissions = builder.build(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculator/processor/WildcardProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculator/processor/WildcardProcessor.java index c3288478d..a7c5c77e9 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculator/processor/WildcardProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculator/processor/WildcardProcessor.java @@ -41,7 +41,7 @@ public class WildcardProcessor extends AbstractPermissionProcessor implements Pe private static final String ROOT_WILDCARD = "*"; private static final String ROOT_WILDCARD_WITH_QUOTES = "'*'"; - private Map wildcardPermissions = Collections.emptyMap(); + private Map wildcardPermissions = Collections.emptyMap(); private TristateResult rootWildcardState = TristateResult.UNDEFINED; @Override @@ -56,9 +56,9 @@ public class WildcardProcessor extends AbstractPermissionProcessor implements Pe node = node.substring(0, endIndex); if (!node.isEmpty()) { - Tristate t = Tristate.fromNullableBoolean(this.wildcardPermissions.get(node)); - if (t != Tristate.UNDEFINED) { - return RESULT_FACTORY.result(t, "match: " + node); + TristateResult match = this.wildcardPermissions.get(node); + if (match != null && match.result() != Tristate.UNDEFINED) { + return match; } } } @@ -68,14 +68,16 @@ public class WildcardProcessor extends AbstractPermissionProcessor implements Pe @Override public void refresh() { - ImmutableMap.Builder builder = ImmutableMap.builder(); + ImmutableMap.Builder builder = ImmutableMap.builder(); for (Map.Entry e : this.sourceMap.entrySet()) { String key = e.getKey(); if (!key.endsWith(WILDCARD_SUFFIX) || key.length() <= 2) { continue; } + key = key.substring(0, key.length() - 2); - builder.put(key.substring(0, key.length() - 2), e.getValue()); + TristateResult value = RESULT_FACTORY.result(Tristate.fromBoolean(e.getValue()), "match: " + key); + builder.put(key, value); } this.wildcardPermissions = builder.build(); @@ -83,7 +85,6 @@ public class WildcardProcessor extends AbstractPermissionProcessor implements Pe if (state == Tristate.UNDEFINED) { state = Tristate.fromNullableBoolean(this.sourceMap.get(ROOT_WILDCARD_WITH_QUOTES)); } - this.rootWildcardState = RESULT_FACTORY.result(state, "root"); } }