Implement LoadingMap using Guava's ForwardingMap utility

This commit is contained in:
Luck 2018-11-28 12:35:38 +00:00
parent 1544487e92
commit 7b5917660c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -25,49 +25,32 @@
package me.lucko.luckperms.common.util; package me.lucko.luckperms.common.util;
import java.util.Collection; import com.google.common.collect.ForwardingMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function; import java.util.function.Function;
public interface LoadingMap<K, V> extends Map<K, V> { public class LoadingMap<K, V> extends ForwardingMap<K, V> implements Map<K, V> {
static <K, V> LoadingMap<K, V> of(Map<K, V> map, Function<K, V> function) { public static <K, V> LoadingMap<K, V> of(Map<K, V> map, Function<K, V> function) {
return new LoadingMapImpl<>(map, function); return new LoadingMap<>(map, function);
} }
static <K, V> LoadingMap<K, V> of(Function<K, V> function) { public static <K, V> LoadingMap<K, V> of(Function<K, V> function) {
return of(new ConcurrentHashMap<>(), function); return of(new ConcurrentHashMap<>(), function);
} }
}
final class LoadingMapImpl<K, V> implements LoadingMap<K, V> {
private final Map<K, V> map; private final Map<K, V> map;
private final Function<K, V> function; private final Function<K, V> function;
LoadingMapImpl(Map<K, V> map, Function<K, V> function) { private LoadingMap(Map<K, V> map, Function<K, V> function) {
this.map = map; this.map = map;
this.function = function; this.function = function;
} }
@Override @Override
public int size() { protected Map<K, V> delegate() {
return this.map.size(); return this.map;
}
@Override
public boolean isEmpty() {
return this.map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return this.map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.map.containsValue(value);
} }
@Override @Override
@ -78,39 +61,4 @@ final class LoadingMapImpl<K, V> implements LoadingMap<K, V> {
} }
return this.map.computeIfAbsent((K) key, this.function); return this.map.computeIfAbsent((K) key, this.function);
} }
@Override
public V put(K key, V value) {
return this.map.put(key, value);
}
@Override
public V remove(Object key) {
return this.map.remove(key);
}
@Override
public void putAll(Map<? extends K, ? extends V> that) {
this.map.putAll(that);
}
@Override
public void clear() {
this.map.clear();
}
@Override
public Set<K> keySet() {
return this.map.keySet();
}
@Override
public Collection<V> values() {
return this.map.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
return this.map.entrySet();
}
} }