From 794455d728bdcc4732b07f1fb6b53a92eb260191 Mon Sep 17 00:00:00 2001 From: Luck Date: Fri, 16 Feb 2018 22:21:56 +0000 Subject: [PATCH] Refactor bulkupdate comparisons --- .../bulkupdate/comparisons/Comparison.java | 7 ++ .../comparisons/ComparisonType.java | 68 ------------ .../comparisons/StandardComparison.java | 104 ++++++++++++++++++ .../comparisons/impl/ComparisonEqual.java | 42 ------- .../comparisons/impl/ComparisonNotEqual.java | 42 ------- .../impl/ComparisonNotSimilar.java | 50 --------- .../comparisons/impl/ComparisonSimilar.java | 50 --------- .../bulkupdate/constraint/Constraint.java | 35 ++---- .../commands/impl/misc/BulkUpdateCommand.java | 5 +- .../me/lucko/luckperms/common/model/User.java | 15 +-- 10 files changed, 130 insertions(+), 288 deletions(-) delete mode 100644 common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/ComparisonType.java create mode 100644 common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/StandardComparison.java delete mode 100644 common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonEqual.java delete mode 100644 common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotEqual.java delete mode 100644 common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotSimilar.java delete mode 100644 common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonSimilar.java 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 20ad5661..25097deb 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 @@ -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(); + } diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/ComparisonType.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/ComparisonType.java deleted file mode 100644 index 775a0606..00000000 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/ComparisonType.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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; - } - -} 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 new file mode 100644 index 00000000..072eeecb --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/StandardComparison.java @@ -0,0 +1,104 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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; + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonEqual.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonEqual.java deleted file mode 100644 index c8a2128f..00000000 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonEqual.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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); - } - -} diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotEqual.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotEqual.java deleted file mode 100644 index 10d96834..00000000 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotEqual.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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); - } - -} diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotSimilar.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotSimilar.java deleted file mode 100644 index a92410a1..00000000 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonNotSimilar.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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); - } - -} diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonSimilar.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonSimilar.java deleted file mode 100644 index 8afccff2..00000000 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/comparisons/impl/ComparisonSimilar.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * This file is part of LuckPerms, licensed under the MIT License. - * - * Copyright (c) lucko (Luck) - * 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); - } - -} diff --git a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/constraint/Constraint.java b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/constraint/Constraint.java index e5158af4..8427f6b6 100644 --- a/common/src/main/java/me/lucko/luckperms/common/bulkupdate/constraint/Constraint.java +++ b/common/src/main/java/me/lucko/luckperms/common/bulkupdate/constraint/Constraint.java @@ -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() { diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/BulkUpdateCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/BulkUpdateCommand.java index d5ab01f0..70eafa87 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/BulkUpdateCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/misc/BulkUpdateCommand.java @@ -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; diff --git a/common/src/main/java/me/lucko/luckperms/common/model/User.java b/common/src/main/java/me/lucko/luckperms/common/model/User.java index aad3cdfc..8f6fff2e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/User.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/User.java @@ -64,17 +64,6 @@ public class User extends PermissionHolder implements Identifiable 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