From bdff84885a89de9a51cd6012f4dc989f0546e83a Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 18 Mar 2017 20:18:18 +0000 Subject: [PATCH] Enforce that ContextSets use the HashMultimap variant - closes #208 --- .../luckperms/api/context/ImmutableContextSet.java | 13 +++++++------ .../luckperms/api/context/MutableContextSet.java | 7 ++++--- .../storage/backing/utils/NodeDataHolder.java | 11 +++++------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java index 8f17528d..c58e968c 100644 --- a/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java +++ b/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java @@ -23,9 +23,10 @@ package me.lucko.luckperms.api.context; import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.Multimap; +import com.google.common.collect.SetMultimap; import java.util.Collection; import java.util.Map; @@ -37,7 +38,7 @@ import java.util.Set; * @since 2.16 */ public final class ImmutableContextSet implements ContextSet { - private static final ImmutableContextSet EMPTY = new ImmutableContextSet(ImmutableMultimap.of()); + private static final ImmutableContextSet EMPTY = new ImmutableContextSet(ImmutableSetMultimap.of()); /** * Make a singleton ImmutableContextSet from a context pair @@ -55,7 +56,7 @@ public final class ImmutableContextSet implements ContextSet { throw new NullPointerException("value"); } - return new ImmutableContextSet(ImmutableMultimap.of(key.toLowerCase(), value)); + return new ImmutableContextSet(ImmutableSetMultimap.of(key.toLowerCase(), value)); } /** @@ -70,7 +71,7 @@ public final class ImmutableContextSet implements ContextSet { throw new NullPointerException("map"); } - ImmutableMultimap.Builder b = ImmutableMultimap.builder(); + ImmutableSetMultimap.Builder b = ImmutableSetMultimap.builder(); for (Map.Entry e : map.entrySet()) { b.put(e.getKey().toLowerCase(), e.getValue()); } @@ -129,10 +130,10 @@ public final class ImmutableContextSet implements ContextSet { return EMPTY; } - private final Multimap map; + private final SetMultimap map; ImmutableContextSet(Multimap contexts) { - this.map = ImmutableMultimap.copyOf(contexts); + this.map = ImmutableSetMultimap.copyOf(contexts); } @Override 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 1e259f33..15cae9d3 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 @@ -27,6 +27,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; 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; @@ -136,14 +137,14 @@ public final class MutableContextSet implements ContextSet { return new MutableContextSet(); } - private final Multimap map; + private final SetMultimap map; public MutableContextSet() { - this.map = Multimaps.synchronizedMultimap(HashMultimap.create()); + this.map = Multimaps.synchronizedSetMultimap(HashMultimap.create()); } private MutableContextSet(Multimap contexts) { - this.map = Multimaps.synchronizedMultimap(HashMultimap.create(contexts)); + this.map = Multimaps.synchronizedSetMultimap(HashMultimap.create(contexts)); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java index 24f93d14..fd041c61 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/utils/NodeDataHolder.java @@ -27,8 +27,7 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; +import com.google.common.collect.ImmutableSetMultimap; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; @@ -54,18 +53,18 @@ public class NodeDataHolder { node.getServer().orElse("global"), node.getWorld().orElse("global"), node.isTemporary() ? node.getExpiryUnixTime() : 0L, - node.getContexts().toMultimap() + ImmutableSetMultimap.copyOf(node.getContexts().toMultimap()) ); } public static NodeDataHolder of(String permission, boolean value, String server, String world, long expiry, String contexts) { Map> deserializedContexts = GSON.fromJson(contexts, CONTEXT_TYPE); - Multimap map = HashMultimap.create(); + ImmutableSetMultimap.Builder map = ImmutableSetMultimap.builder(); for (Map.Entry> e : deserializedContexts.entrySet()) { map.putAll(e.getKey(), e.getValue()); } - return new NodeDataHolder(permission, value, server, world, expiry, map); + return new NodeDataHolder(permission, value, server, world, expiry, map.build()); } private final String permission; @@ -73,7 +72,7 @@ public class NodeDataHolder { private final String server; private final String world; private final long expiry; - private final Multimap contexts; + private final ImmutableSetMultimap contexts; public String serialiseContext() { return GSON.toJson(getContexts().asMap());