Use a BiMap in UuidCache
This commit is contained in:
parent
b1e34b55f8
commit
50fd954599
@ -22,12 +22,12 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.core;
|
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 lombok.Getter;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see me.lucko.luckperms.api.UuidCache for docs
|
* @see me.lucko.luckperms.api.UuidCache for docs
|
||||||
@ -35,7 +35,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
public class UuidCache {
|
public class UuidCache {
|
||||||
|
|
||||||
// External UUID --> Internal UUID
|
// External UUID --> Internal UUID
|
||||||
private Map<UUID, UUID> cache;
|
private BiMap<UUID, UUID> cache;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private final boolean onlineMode;
|
private final boolean onlineMode;
|
||||||
@ -44,23 +44,16 @@ public class UuidCache {
|
|||||||
this.onlineMode = onlineMode;
|
this.onlineMode = onlineMode;
|
||||||
|
|
||||||
if (!onlineMode) {
|
if (!onlineMode) {
|
||||||
cache = new ConcurrentHashMap<>();
|
cache = Maps.synchronizedBiMap(HashBiMap.create());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UUID getUUID(UUID external) {
|
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) {
|
public UUID getExternalUUID(UUID internal) {
|
||||||
if (onlineMode) return internal;
|
return onlineMode ? internal : cache.inverse().getOrDefault(internal, internal);
|
||||||
|
|
||||||
Optional<UUID> external = cache.entrySet().stream()
|
|
||||||
.filter(e -> e.getValue().equals(internal))
|
|
||||||
.map(Map.Entry::getKey)
|
|
||||||
.findFirst();
|
|
||||||
|
|
||||||
return external.isPresent() ? external.get() : internal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addToCache(UUID external, UUID internal) {
|
public void addToCache(UUID external, UUID internal) {
|
||||||
|
Loading…
Reference in New Issue
Block a user