From fef6ebf7930c06add3a17896eee54c7f9070c059 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 26 Apr 2018 19:51:05 +0100 Subject: [PATCH] Context set values should be lowercase'd too --- .../api/context/AbstractContextSet.java | 23 +------ .../luckperms/api/context/ContextSet.java | 69 ++----------------- .../api/context/MutableContextSet.java | 18 +---- .../luckperms/common/node/ImmutableNode.java | 2 +- .../storage/dao/file/FileActionLogger.java | 11 +-- 5 files changed, 15 insertions(+), 108 deletions(-) diff --git a/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java index 723a9511..9e667031 100644 --- a/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java +++ b/api/src/main/java/me/lucko/luckperms/api/context/AbstractContextSet.java @@ -68,27 +68,6 @@ abstract class AbstractContextSet implements ContextSet { return backing().containsEntry(sanitizeKey(key), sanitizeValue(value)); } - @Override - public boolean hasIgnoreCase(@Nonnull String key, @Nonnull String value) { - String v = sanitizeValue(value); - - Collection values = backing().asMap().get(sanitizeKey(key)); - if (values == null || values.isEmpty()) { - return false; - } - - if (values.contains(v)) { - return true; - } - - for (String val : values) { - if (val.equalsIgnoreCase(v)) { - return true; - } - } - return false; - } - @Override public boolean isEmpty() { return backing().isEmpty(); @@ -134,7 +113,7 @@ abstract class AbstractContextSet implements ContextSet { if (stringIsEmpty(value)) { throw new IllegalArgumentException("value is (effectively) empty"); } - return value; + return value.toLowerCase(); } private static boolean stringIsEmpty(String s) { diff --git a/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java index e4f15fe7..e50dcbca 100644 --- a/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java +++ b/api/src/main/java/me/lucko/luckperms/api/context/ContextSet.java @@ -47,9 +47,8 @@ import javax.annotation.Nonnull; *

Contexts can be combined with each other to form so called * "context sets" - simply a collection of context pairs.

* - *

Context keys are case-insensitive, and will be converted to - * {@link String#toLowerCase() lowercase} by all implementations. - * Values however are case-sensitive.

+ *

Context keys and values are case-insensitive, and will be converted to + * {@link String#toLowerCase() lowercase} by all implementations.

* *

Context keys and values may not be null or empty. A key/value will be * deemed empty if it's length is zero, or if it consists of only space @@ -272,33 +271,16 @@ public interface ContextSet { /** * Returns if the {@link ContextSet} contains a given context pairing. * - *

This lookup is case-sensitive on the value.

- * * @param key the key to look for - * @param value the value to look for (case sensitive) + * @param value the value to look for * @return true if the set contains the context pair * @throws NullPointerException if the key or value is null */ boolean has(@Nonnull String key, @Nonnull String value); - /** - * Returns if the {@link ContextSet} contains a given context pairing, - * ignoring the case of values. - * - *

This lookup is case-insensitive on the value.

- * - * @param key the key to look for - * @param value the value to look for - * @return true if the set contains the context pair - * @throws NullPointerException if the key or value is null - */ - boolean hasIgnoreCase(@Nonnull String key, @Nonnull String value); - /** * Returns if the {@link ContextSet} contains a given context pairing. * - *

This lookup is case-sensitive on the value.

- * * @param entry the entry to look for * @return true if the set contains the context pair * @throws NullPointerException if the key or value is null @@ -308,21 +290,6 @@ public interface ContextSet { return has(entry.getKey(), entry.getValue()); } - /** - * Returns if the {@link ContextSet} contains a given context pairing, - * ignoring the case of values. - * - *

This lookup is case-insensitive on the value.

- * - * @param entry the entry to look for - * @return true if the set contains the context pair - * @throws NullPointerException if the key or value is null - */ - default boolean hasIgnoreCase(@Nonnull Map.Entry entry) { - Objects.requireNonNull(entry, "entry"); - return hasIgnoreCase(entry.getKey(), entry.getValue()); - } - /** * Returns if this {@link ContextSet} is fully "satisfied" by another set. * @@ -331,31 +298,11 @@ public interface ContextSet { * *

Mathematically, this method returns true if this set is a subset of the other.

* - *

This check is case-sensitive. For a case-insensitive check, - * use {@link #isSatisfiedBy(ContextSet, boolean)}.

- * * @param other the other set to check * @return true if all entries in this set are also in the other set * @since 3.1 */ default boolean isSatisfiedBy(@Nonnull ContextSet other) { - return isSatisfiedBy(other, true); - } - - /** - * Returns if this {@link ContextSet} is fully "satisfied" by another set. - * - *

For a context set to "satisfy" another, it must itself contain all of - * the context pairings in the other set.

- * - *

Mathematically, this method returns true if this set is a subset of the other.

- * - * @param other the other set to check - * @param caseSensitive if the check should be case sensitive - * @return true if all entries in this set are also in the other set - * @since 3.4 - */ - default boolean isSatisfiedBy(@Nonnull ContextSet other, boolean caseSensitive) { if (this == other) { return true; } @@ -373,14 +320,8 @@ public interface ContextSet { } else { // neither are empty, we need to compare the individual entries for (Map.Entry context : toSet()) { - if (caseSensitive) { - if (!other.has(context)) { - return false; - } - } else { - if (!other.hasIgnoreCase(context)) { - return false; - } + if (!other.has(context)) { + return false; } } return true; diff --git a/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java index 5e2bd610..3b3aa2e8 100644 --- a/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java +++ b/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java @@ -32,7 +32,6 @@ import com.google.common.collect.Multimap; import com.google.common.collect.Multimaps; import com.google.common.collect.SetMultimap; -import java.util.Collection; import java.util.Map; import java.util.Objects; import java.util.Set; @@ -282,28 +281,13 @@ public final class MutableContextSet extends AbstractContextSet implements Conte * Removes a context from this set. * * @param key the key to remove - * @param value the value to remove (case sensitive) + * @param value the value to remove * @throws NullPointerException if the key or value is null */ public void remove(@Nonnull String key, @Nonnull String value) { this.map.remove(sanitizeKey(key), sanitizeValue(value)); } - /** - * Removes a context from this set. (case-insensitive) - * - * @param key the key to remove - * @param value the value to remove - * @throws NullPointerException if the key or value is null - */ - public void removeIgnoreCase(@Nonnull String key, @Nonnull String value) { - String v = sanitizeValue(value); - Collection strings = this.map.asMap().get(sanitizeKey(key)); - if (strings != null) { - strings.removeIf(e -> e.equalsIgnoreCase(v)); - } - } - /** * Removes all contexts from this set with the given key. * diff --git a/common/src/main/java/me/lucko/luckperms/common/node/ImmutableNode.java b/common/src/main/java/me/lucko/luckperms/common/node/ImmutableNode.java index 59288cad..cc8a1660 100644 --- a/common/src/main/java/me/lucko/luckperms/common/node/ImmutableNode.java +++ b/common/src/main/java/me/lucko/luckperms/common/node/ImmutableNode.java @@ -321,7 +321,7 @@ public final class ImmutableNode implements Node { @Override public boolean shouldApplyWithContext(@Nonnull ContextSet contextSet) { - return getFullContexts().isSatisfiedBy(contextSet, false); + return getFullContexts().isSatisfiedBy(contextSet); } @Nonnull diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/FileActionLogger.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/FileActionLogger.java index a818b34f..e4f70f4d 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/FileActionLogger.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/file/FileActionLogger.java @@ -25,10 +25,11 @@ package me.lucko.luckperms.common.storage.dao.file; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.internal.Streams; +import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; @@ -49,6 +50,8 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.locks.ReentrantLock; public class FileActionLogger { + private static final JsonParser JSON_PARSER = new JsonParser(); + private static final Gson GSON = new Gson(); /** * The path to save logger content to @@ -94,7 +97,7 @@ public class FileActionLogger { if (Files.exists(this.contentFile)) { try (JsonReader reader = new JsonReader(Files.newBufferedReader(this.contentFile, StandardCharsets.UTF_8))) { - array = Streams.parse(reader).getAsJsonArray(); + array = JSON_PARSER.parse(reader).getAsJsonArray(); } catch (IOException e) { e.printStackTrace(); array = new JsonArray(); @@ -123,7 +126,7 @@ public class FileActionLogger { // write the full content back to the file try (JsonWriter writer = new JsonWriter(Files.newBufferedWriter(this.contentFile, StandardCharsets.UTF_8))) { writer.setIndent(" "); - Streams.write(array, writer); + GSON.toJson(array, writer); } } catch (IOException e) { e.printStackTrace(); @@ -136,7 +139,7 @@ public class FileActionLogger { public Log getLog() throws IOException { Log.Builder log = Log.builder(); try (JsonReader reader = new JsonReader(Files.newBufferedReader(this.contentFile, StandardCharsets.UTF_8))) { - JsonArray array = Streams.parse(reader).getAsJsonArray(); + JsonArray array = JSON_PARSER.parse(reader).getAsJsonArray(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject();