supply toString to Factory maps

GitOrigin-RevId: 438cdd07036461c51fbc0cbfaef10948b45bac5d
This commit is contained in:
Alexey Kudravtsev
2019-12-19 12:33:08 +00:00
committed by intellij-monorepo-bot
parent 94c21e9824
commit c2373ffb3c
2 changed files with 16 additions and 6 deletions
@@ -14,10 +14,10 @@ import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
/**
* a ConcurrentMap which computes the value associated with the key (via {@link #create(Object)} method) on first {@link #get(Object)} access.
* THREAD SAFE.
* ConcurrentMap which computes the value associated with the key (via {@link #create(Object)} method) on first {@link #get(Object)} access.
* This map is THREAD SAFE.
* It's guaranteed that two {@link #get(Object)} method calls with the same key will return the same value (i.e. the created value stored atomically).
* For not thread-safe (but possible faster and more memory-efficient) alternative please use {@link FactoryMap}
* For the not thread-safe (but possibly faster and more memory-efficient) alternative please use {@link FactoryMap} instead.
*/
public abstract class ConcurrentFactoryMap<K,V> implements ConcurrentMap<K,V> {
private final ConcurrentMap<K, V> myMap = createMap();
@@ -167,6 +167,11 @@ public abstract class ConcurrentFactoryMap<K,V> implements ConcurrentMap<K,V> {
return nullize(myMap.replace(notNull(key), notNull(value)));
}
@Override
public String toString() {
return myMap.toString();
}
@NotNull
public static <T, V> ConcurrentMap<T, V> createMap(@NotNull final Function<? super T, ? extends V> computeValue) {
return new ConcurrentFactoryMap<T, V>() {
@@ -29,9 +29,9 @@ import java.util.*;
import java.util.function.Supplier;
/**
* a Map which computes the value associated with the key (via {@link #create(Object)} method) on first {@link #get(Object)} access.
* NOT THREAD SAFE.
* For thread-safe alternative please use {@link ConcurrentFactoryMap}
* Map which computes the value associated with the key (via {@link #create(Object)} method) on first {@link #get(Object)} access.
* This map is NOT THREAD SAFE.
* For the thread-safe alternative please use {@link ConcurrentFactoryMap} instead.
*/
public abstract class FactoryMap<K,V> implements Map<K, V> {
private Map<K, V> myMap;
@@ -175,6 +175,11 @@ public abstract class FactoryMap<K,V> implements Map<K, V> {
entry -> new AbstractMap.SimpleEntry<>(nullize(entry.getKey()), nullize(entry.getValue())));
}
@Override
public String toString() {
return String.valueOf(myMap);
}
@NotNull
public static <K, V> Map<K, V> create(@NotNull final Function<? super K, ? extends V> computeValue) {
return new FactoryMap<K, V>(true) {