diff options
Diffstat (limited to 'guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/MapMaker.java')
-rw-r--r-- | guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/MapMaker.java | 109 |
1 files changed, 6 insertions, 103 deletions
diff --git a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/MapMaker.java b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/MapMaker.java index fa1afc587..b2fd7ae78 100644 --- a/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/MapMaker.java +++ b/guava-gwt/src-super/com/google/common/collect/super/com/google/common/collect/MapMaker.java @@ -16,108 +16,18 @@ package com.google.common.collect; -import com.google.common.base.Function; -import java.util.LinkedHashMap; +import static com.google.common.base.Preconditions.checkArgument; + import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** - * MapMaker emulation. Since Javascript is single-threaded and have no references, this reduces to - * the creation of expiring and computing maps. + * MapMaker emulation. * * @author Charles Fry */ public final class MapMaker { - - // TODO(fry,kak): ConcurrentHashMap never throws a CME when mutating the map during iteration, but - // this implementation (based on a LHM) does. This will all be replaced soon anyways, so leaving - // it as is for now. - private static class ComputingMap<K, V> extends LinkedHashMap<K, V> - implements ConcurrentMap<K, V> { - private final Function<? super K, ? extends V> computer; - - ComputingMap(int initialCapacity) { - this(null, initialCapacity); - } - - ComputingMap(Function<? super K, ? extends V> computer, int initialCapacity) { - super(initialCapacity, /* ignored loadFactor */ 0.75f, true); - this.computer = computer; - } - - @Override - public V putIfAbsent(K key, V value) { - if (!containsKey(key)) { - return put(key, value); - } else { - return get(key); - } - } - - @Override - public boolean remove(Object key, Object value) { - if (containsKey(key) && get(key).equals(value)) { - remove(key); - return true; - } - return false; - } - - @Override - public boolean replace(K key, V oldValue, V newValue) { - if (containsKey(key) && get(key).equals(oldValue)) { - put(key, newValue); - return true; - } - return false; - } - - @Override - public V replace(K key, V value) { - return containsKey(key) ? put(key, value) : null; - } - - @Override - public V get(Object k) { - // from CustomConcurrentHashMap - V result = super.get(k); - if (result == null && computer != null) { - /* - * This cast isn't safe, but we can rely on the fact that K is almost always passed to - * Map.get(), and tools like IDEs and Findbugs can catch situations where this isn't the - * case. - * - * The alternative is to add an overloaded method, but the chances of a user calling get() - * instead of the new API and the risks inherent in adding a new API outweigh this little - * hole. - */ - @SuppressWarnings("unchecked") - K key = (K) k; - result = compute(key); - } - return result; - } - - private V compute(K key) { - // from MapMaker - V value; - try { - value = computer.apply(key); - } catch (Throwable t) { - throw new ComputationException(t); - } - - if (value == null) { - String message = computer + " returned null for key " + key + "."; - throw new NullPointerException(message); - } - put(key, value); - return value; - } - } - private int initialCapacity = 16; - private boolean useCustomMap; public MapMaker() {} @@ -130,21 +40,14 @@ public final class MapMaker { } public MapMaker concurrencyLevel(int concurrencyLevel) { - if (concurrencyLevel < 1) { - throw new IllegalArgumentException("GWT only supports a concurrency level of 1"); - } + checkArgument( + concurrencyLevel >= 1, "concurrency level (%s) must be at least 1", concurrencyLevel); // GWT technically only supports concurrencyLevel == 1, but we silently // ignore other positive values. return this; } public <K, V> ConcurrentMap<K, V> makeMap() { - return useCustomMap - ? new ComputingMap<K, V>(null, initialCapacity) - : new ConcurrentHashMap<K, V>(initialCapacity); - } - - public <K, V> ConcurrentMap<K, V> makeComputingMap(Function<? super K, ? extends V> computer) { - return new ComputingMap<K, V>(computer, initialCapacity); + return new ConcurrentHashMap<K, V>(initialCapacity); } } |