From 50fd95459924e00eb9d9a7a6f28dc16f089e570c Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 19 Sep 2016 21:13:45 +0100 Subject: [PATCH] Use a BiMap in UuidCache --- .../me/lucko/luckperms/core/UuidCache.java | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/core/UuidCache.java b/common/src/main/java/me/lucko/luckperms/core/UuidCache.java index af6c7dd83..51eab71d0 100644 --- a/common/src/main/java/me/lucko/luckperms/core/UuidCache.java +++ b/common/src/main/java/me/lucko/luckperms/core/UuidCache.java @@ -22,12 +22,12 @@ package me.lucko.luckperms.core; +import com.google.common.collect.BiMap; +import com.google.common.collect.HashBiMap; +import com.google.common.collect.Maps; import lombok.Getter; -import java.util.Map; -import java.util.Optional; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; /** * @see me.lucko.luckperms.api.UuidCache for docs @@ -35,7 +35,7 @@ import java.util.concurrent.ConcurrentHashMap; public class UuidCache { // External UUID --> Internal UUID - private Map cache; + private BiMap cache; @Getter private final boolean onlineMode; @@ -44,23 +44,16 @@ public class UuidCache { this.onlineMode = onlineMode; if (!onlineMode) { - cache = new ConcurrentHashMap<>(); + cache = Maps.synchronizedBiMap(HashBiMap.create()); } } public UUID getUUID(UUID external) { - return onlineMode ? external : (cache.containsKey(external) ? cache.get(external) : external); + return onlineMode ? external : cache.getOrDefault(external, external); } public UUID getExternalUUID(UUID internal) { - if (onlineMode) return internal; - - Optional external = cache.entrySet().stream() - .filter(e -> e.getValue().equals(internal)) - .map(Map.Entry::getKey) - .findFirst(); - - return external.isPresent() ? external.get() : internal; + return onlineMode ? internal : cache.inverse().getOrDefault(internal, internal); } public void addToCache(UUID external, UUID internal) {