Slightly optimize regex and wildcard processors

This commit is contained in:
Luck 2019-02-21 12:37:23 +00:00
parent 8773ec07ab
commit ae63a42c2e
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 17 additions and 14 deletions

View File

@ -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<Pattern, Boolean> regexPermissions = Collections.emptyMap();
private List<Map.Entry<Pattern, TristateResult>> regexPermissions = Collections.emptyList();
@Override
public TristateResult hasPermission(String permission) {
for (Map.Entry<Pattern, Boolean> e : this.regexPermissions.entrySet()) {
for (Map.Entry<Pattern, TristateResult> 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<Pattern, Boolean> builder = ImmutableMap.builder();
ImmutableList.Builder<Map.Entry<Pattern, TristateResult>> builder = ImmutableList.builder();
for (Map.Entry<String, Boolean> 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();
}

View File

@ -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<String, Boolean> wildcardPermissions = Collections.emptyMap();
private Map<String, TristateResult> 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<String, Boolean> builder = ImmutableMap.builder();
ImmutableMap.Builder<String, TristateResult> builder = ImmutableMap.builder();
for (Map.Entry<String, Boolean> 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");
}
}