Enforce that ContextSets use the HashMultimap variant - closes #208

This commit is contained in:
Luck 2017-03-18 20:18:18 +00:00
parent f1a9c3722c
commit bdff84885a
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 16 additions and 15 deletions

View File

@ -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<String, String> b = ImmutableMultimap.builder();
ImmutableSetMultimap.Builder<String, String> b = ImmutableSetMultimap.builder();
for (Map.Entry<String, String> 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<String, String> map;
private final SetMultimap<String, String> map;
ImmutableContextSet(Multimap<String, String> contexts) {
this.map = ImmutableMultimap.copyOf(contexts);
this.map = ImmutableSetMultimap.copyOf(contexts);
}
@Override

View File

@ -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<String, String> map;
private final SetMultimap<String, String> map;
public MutableContextSet() {
this.map = Multimaps.synchronizedMultimap(HashMultimap.create());
this.map = Multimaps.synchronizedSetMultimap(HashMultimap.create());
}
private MutableContextSet(Multimap<String, String> contexts) {
this.map = Multimaps.synchronizedMultimap(HashMultimap.create(contexts));
this.map = Multimaps.synchronizedSetMultimap(HashMultimap.create(contexts));
}
@Override

View File

@ -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<String, Collection<String>> deserializedContexts = GSON.fromJson(contexts, CONTEXT_TYPE);
Multimap<String, String> map = HashMultimap.create();
ImmutableSetMultimap.Builder<String, String> map = ImmutableSetMultimap.builder();
for (Map.Entry<String, Collection<String>> 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<String, String> contexts;
private final ImmutableSetMultimap<String, String> contexts;
public String serialiseContext() {
return GSON.toJson(getContexts().asMap());