From fa839862507b8ba3755d8ebcb96b37d0c0343695 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 23 Sep 2018 19:53:16 +0100 Subject: [PATCH] Refactor Constraint/Comparison impls for bulkupdate --- .../bulkupdate/comparisons/Comparison.java | 25 +++++++--- .../bulkupdate/comparisons/Constraint.java | 22 +++------ .../comparisons/StandardComparison.java | 47 +++++++++---------- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Comparison.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Comparison.java index de005b2bd..23bf7f735 100644 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Comparison.java +++ b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Comparison.java @@ -40,18 +40,31 @@ public interface Comparison { String getSymbol(); /** - * Compares two strings according to this comparisons - * behaviour, and returns if they match. + * Creates a {@link CompiledExpression} for the given expression * - * @param str the string - * @param expr the expression - * @return if the string matches the expression + * @param expression the expression + * @return the compiled expression */ - boolean matches(String str, String expr); + CompiledExpression compile(String expression); /** * Returns the comparison operator in SQL form */ void appendSql(PreparedStatementBuilder builder); + /** + * An instance of {@link Comparison} which is bound to an expression. + */ + interface CompiledExpression { + + /** + * Tests the expression against a given string, according to the + * rules of the parent {@link Comparison}. + * + * @param string the string + * @return if there was a match + */ + boolean test(String string); + } + } diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Constraint.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Constraint.java index 4937a6fa1..0ae3f265c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Constraint.java +++ b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/Constraint.java @@ -33,15 +33,15 @@ public class Constraint { return new Constraint(comparison, expression); } - // the comparison type being used in this query private final Comparison comparison; + private final String expressionValue; + private final Comparison.CompiledExpression compiledExpression; - // the expression being compared against - private final String expression; private Constraint(Comparison comparison, String expression) { this.comparison = comparison; - this.expression = expression; + this.expressionValue = expression; + this.compiledExpression = this.comparison.compile(this.expressionValue); } /** @@ -51,7 +51,7 @@ public class Constraint { * @return true if satisfied */ public boolean eval(String value) { - return this.comparison.matches(value, this.expression); + return this.compiledExpression.test(value); } public void appendSql(PreparedStatementBuilder builder, String field) { @@ -59,19 +59,11 @@ public class Constraint { builder.append(field + " "); this.comparison.appendSql(builder); builder.append(" ?"); - builder.variable(this.expression); - } - - public Comparison getComparison() { - return this.comparison; - } - - public String getExpression() { - return this.expression; + builder.variable(this.expressionValue); } @Override public String toString() { - return this.comparison + " " + this.expression; + return this.comparison + " " + this.expressionValue; } } diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/StandardComparison.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/StandardComparison.java index 47d5aadd6..75883341b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/StandardComparison.java +++ b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/StandardComparison.java @@ -27,6 +27,8 @@ package me.lucko.luckperms.common.bulkupdate.comparisons; import me.lucko.luckperms.common.bulkupdate.PreparedStatementBuilder; +import java.util.regex.Pattern; + /** * An enumeration of standard {@link Comparison}s. */ @@ -34,45 +36,31 @@ public enum StandardComparison implements Comparison { EQUAL("==", "=") { @Override - public boolean matches(String str, String expr) { - return str.equalsIgnoreCase(expr); + public CompiledExpression compile(String expression) { + return expression::equalsIgnoreCase; } }, NOT_EQUAL("!=", "!=") { @Override - public boolean matches(String str, String expr) { - return !str.equalsIgnoreCase(expr); + public CompiledExpression compile(String expression) { + return string -> !expression.equalsIgnoreCase(string); } }, SIMILAR("~~", "LIKE") { @Override - public boolean matches(String str, String expr) { - // form expression - expr = expr.toLowerCase(); - expr = expr.replace(".", "\\."); - - // convert from SQL LIKE syntax to regex - expr = expr.replace("_", "."); - expr = expr.replace("%", ".*"); - - return str.toLowerCase().matches(expr); + public CompiledExpression compile(String expression) { + Pattern pattern = StandardComparison.compilePatternForLikeSyntax(expression); + return string -> pattern.matcher(string).matches(); } }, NOT_SIMILAR("!~", "NOT LIKE") { @Override - public boolean matches(String str, String expr) { - // form expression - expr = expr.toLowerCase(); - expr = expr.replace(".", "\\."); - - // convert from SQL LIKE syntax to regex - expr = expr.replace("_", "."); - expr = expr.replace("%", ".*"); - - return !str.toLowerCase().matches(expr); + public CompiledExpression compile(String expression) { + Pattern pattern = StandardComparison.compilePatternForLikeSyntax(expression); + return string -> !pattern.matcher(string).matches(); } }; @@ -108,4 +96,15 @@ public enum StandardComparison implements Comparison { return null; } + static Pattern compilePatternForLikeSyntax(String expression) { + expression = expression.toLowerCase(); + expression = expression.replace(".", "\\."); + + // convert from SQL LIKE syntax to regex + expression = expression.replace("_", "."); + expression = expression.replace("%", ".*"); + + return Pattern.compile(expression); + } + }