Refactor Constraint/Comparison impls for bulkupdate

This commit is contained in:
Luck 2018-09-23 19:53:16 +01:00
parent e1b16465f8
commit fa83986250
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 49 additions and 45 deletions

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}