Refactor bulkupdate comparisons

This commit is contained in:
Luck 2018-02-16 22:21:56 +00:00
parent 0adf85746d
commit 794455d728
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 130 additions and 288 deletions

View File

@ -47,4 +47,11 @@ public interface Comparison {
*/
boolean matches(String str, String expr);
/**
* Returns the comparison operator in SQL form
*
* @return a sql form of this comparison
*/
String getAsSql();
}

View File

@ -1,68 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.bulkupdate.comparisons;
import me.lucko.luckperms.common.bulkupdate.comparisons.impl.ComparisonEqual;
import me.lucko.luckperms.common.bulkupdate.comparisons.impl.ComparisonNotEqual;
import me.lucko.luckperms.common.bulkupdate.comparisons.impl.ComparisonNotSimilar;
import me.lucko.luckperms.common.bulkupdate.comparisons.impl.ComparisonSimilar;
/**
* An enumeration of all {@link Comparison}s.
*/
public enum ComparisonType {
EQUAL("==", new ComparisonEqual()),
NOT_EQUAL("!=", new ComparisonNotEqual()),
SIMILAR("~~", new ComparisonSimilar()),
NOT_SIMILAR("!~", new ComparisonNotSimilar());
private final String symbol;
private final Comparison instance;
ComparisonType(String symbol, Comparison instance) {
this.symbol = symbol;
this.instance = instance;
}
public String getSymbol() {
return this.symbol;
}
public Comparison getComparison() {
return this.instance;
}
public static ComparisonType parseComparison(String s) {
for (ComparisonType t : values()) {
if (t.getSymbol().equals(s)) {
return t;
}
}
return null;
}
}

View File

@ -0,0 +1,104 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.bulkupdate.comparisons;
/**
* An enumeration of standard {@link Comparison}s.
*/
public enum StandardComparison implements Comparison {
EQUAL("==", "=") {
@Override
public boolean matches(String str, String expr) {
return str.equalsIgnoreCase(expr);
}
},
NOT_EQUAL("!=", "!=") {
@Override
public boolean matches(String str, String expr) {
return !str.equalsIgnoreCase(expr);
}
},
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);
}
},
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);
}
};
private final String symbol;
private final String asSql;
StandardComparison(String symbol, String asSql) {
this.symbol = symbol;
this.asSql = asSql;
}
@Override
public String getSymbol() {
return this.symbol;
}
@Override
public String getAsSql() {
return this.asSql;
}
public static StandardComparison parseComparison(String s) {
for (StandardComparison t : values()) {
if (t.getSymbol().equals(s)) {
return t;
}
}
return null;
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.bulkupdate.comparisons.impl;
import me.lucko.luckperms.common.bulkupdate.comparisons.Comparison;
public class ComparisonEqual implements Comparison {
@Override
public String getSymbol() {
return "==";
}
@Override
public boolean matches(String str, String expr) {
return str.equalsIgnoreCase(expr);
}
}

View File

@ -1,42 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.bulkupdate.comparisons.impl;
import me.lucko.luckperms.common.bulkupdate.comparisons.Comparison;
public class ComparisonNotEqual implements Comparison {
@Override
public String getSymbol() {
return "!=";
}
@Override
public boolean matches(String str, String expr) {
return !str.equalsIgnoreCase(expr);
}
}

View File

@ -1,50 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.bulkupdate.comparisons.impl;
import me.lucko.luckperms.common.bulkupdate.comparisons.Comparison;
public class ComparisonNotSimilar implements Comparison {
@Override
public String getSymbol() {
return "!~";
}
@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);
}
}

View File

@ -1,50 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.bulkupdate.comparisons.impl;
import me.lucko.luckperms.common.bulkupdate.comparisons.Comparison;
public class ComparisonSimilar implements Comparison {
@Override
public String getSymbol() {
return "~~";
}
@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);
}
}

View File

@ -26,7 +26,7 @@
package me.lucko.luckperms.common.bulkupdate.constraint;
import me.lucko.luckperms.common.bulkupdate.BulkUpdate;
import me.lucko.luckperms.common.bulkupdate.comparisons.ComparisonType;
import me.lucko.luckperms.common.bulkupdate.comparisons.Comparison;
import me.lucko.luckperms.common.node.NodeModel;
/**
@ -34,22 +34,22 @@ import me.lucko.luckperms.common.node.NodeModel;
*/
public class Constraint {
public static Constraint of(QueryField field, ComparisonType comparisonType, String value) {
return new Constraint(field, comparisonType, value);
public static Constraint of(QueryField field, Comparison comparison, String value) {
return new Constraint(field, comparison, value);
}
// the field this constraint is comparing against
private final QueryField field;
// the comparison type being used in this constraint
private final ComparisonType comparisonType;
private final Comparison comparison;
// the expression being compared against
private final String value;
private Constraint(QueryField field, ComparisonType comparisonType, String value) {
private Constraint(QueryField field, Comparison comparison, String value) {
this.field = field;
this.comparisonType = comparisonType;
this.comparison = comparison;
this.value = value;
}
@ -62,37 +62,26 @@ public class Constraint {
public boolean isSatisfiedBy(NodeModel node) {
switch (this.field) {
case PERMISSION:
return this.comparisonType.getComparison().matches(node.getPermission(), this.value);
return this.comparison.matches(node.getPermission(), this.value);
case SERVER:
return this.comparisonType.getComparison().matches(node.getServer(), this.value);
return this.comparison.matches(node.getServer(), this.value);
case WORLD:
return this.comparisonType.getComparison().matches(node.getWorld(), this.value);
return this.comparison.matches(node.getWorld(), this.value);
default:
throw new RuntimeException();
}
}
public String getAsSql() {
switch (this.comparisonType) {
case EQUAL:
return this.field.getSqlName() + " = " + BulkUpdate.escapeStringForSql(this.value);
case NOT_EQUAL:
return this.field.getSqlName() + " != " + BulkUpdate.escapeStringForSql(this.value);
case SIMILAR:
return this.field.getSqlName() + " LIKE " + BulkUpdate.escapeStringForSql(this.value);
case NOT_SIMILAR:
return this.field.getSqlName() + " NOT LIKE " + BulkUpdate.escapeStringForSql(this.value);
default:
throw new RuntimeException();
}
return this.field.getSqlName() + " " + this.comparison.getAsSql() + " " + BulkUpdate.escapeStringForSql(this.value);
}
public QueryField getField() {
return this.field;
}
public ComparisonType getComparisonType() {
return this.comparisonType;
public Comparison getComparison() {
return this.comparison;
}
public String getValue() {

View File

@ -33,7 +33,8 @@ import me.lucko.luckperms.common.bulkupdate.BulkUpdateBuilder;
import me.lucko.luckperms.common.bulkupdate.DataType;
import me.lucko.luckperms.common.bulkupdate.action.DeleteAction;
import me.lucko.luckperms.common.bulkupdate.action.UpdateAction;
import me.lucko.luckperms.common.bulkupdate.comparisons.ComparisonType;
import me.lucko.luckperms.common.bulkupdate.comparisons.Comparison;
import me.lucko.luckperms.common.bulkupdate.comparisons.StandardComparison;
import me.lucko.luckperms.common.bulkupdate.constraint.Constraint;
import me.lucko.luckperms.common.bulkupdate.constraint.QueryField;
import me.lucko.luckperms.common.commands.CommandException;
@ -133,7 +134,7 @@ public class BulkUpdateCommand extends SingleCommand {
return CommandResult.INVALID_ARGS;
}
ComparisonType comparison = ComparisonType.parseComparison(parts[1]);
Comparison comparison = StandardComparison.parseComparison(parts[1]);
if (comparison == null) {
Message.BULK_UPDATE_INVALID_COMPARISON.send(sender, parts[1]);
return CommandResult.INVALID_ARGS;

View File

@ -64,17 +64,6 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
private final BufferedRequest<Void> refreshBuffer;
public User(UUID uuid, LuckPermsPlugin plugin) {
super(uuid.toString(), plugin);
this.uuid = uuid;
this.refreshBuffer = new UserRefreshBuffer(plugin, this);
this.primaryGroup = plugin.getConfiguration().get(ConfigKeys.PRIMARY_GROUP_CALCULATION).apply(this);
this.cachedData = new UserCachedData(this);
getPlugin().getEventFactory().handleUserCacheLoad(this, this.cachedData);
}
public User(UUID uuid, String name, LuckPermsPlugin plugin) {
super(uuid.toString(), plugin);
this.uuid = uuid;
@ -87,6 +76,10 @@ public class User extends PermissionHolder implements Identifiable<UserIdentifie
getPlugin().getEventFactory().handleUserCacheLoad(this, this.cachedData);
}
public User(UUID uuid, LuckPermsPlugin plugin) {
this(uuid, null, plugin);
}
public UUID getUuid() {
return this.uuid;
}