Class MapMaker
- java.lang.Object
-
- org.glassfish.jersey.internal.guava.MapMaker
-
public final class MapMaker extends Object
A builder of
ConcurrentMapinstances having any combination of the following features:- keys or values automatically wrapped in weak or soft references
- notification of evicted (or otherwise removed) entries
- on-demand computation of values for keys not already present
Usage example:
<p> ConcurrentMap<Request, Stopwatch> timers = new MapMaker() .concurrencyLevel(4) .weakKeys() .makeMap();These features are all optional;
new MapMaker().makeMap()returns a valid concurrent map that behaves similarly to aConcurrentHashMap.The returned map is implemented as a hash table with similar performance characteristics to
ConcurrentHashMap. It supports all optional operations of theConcurrentMapinterface. It does not permit null keys or values.Note: by default, the returned map uses equality comparisons (the
equalsmethod) to determine equality for keys or values. However, if#weakKeyswas specified, the map uses identity (==) comparisons instead for keys. Likewise, if#weakValuesor#softValueswas specified, the map uses identity comparisons for values.The view collections of the returned map have weakly consistent iterators. This means that they are safe for concurrent use, but if other threads modify the map after the iterator is created, it is undefined which of these changes, if any, are reflected in that iterator. These iterators never throw
ConcurrentModificationException.If
#weakKeys,#weakValues, or#softValuesare requested, it is possible for a key or value present in the map to be reclaimed by the garbage collector. Entries with reclaimed keys or values may be removed from the map on each map modification or on occasional map accesses; such entries may be counted byMap.size(), but will never be visible to read or write operations. A partially-reclaimed entry is never exposed to the user. AnyMap.Entryinstance retrieved from the map's entry set is a snapshot of that entry's state at the time of retrieval; such entries do, however, supportMap.Entry.setValue(V), which simply callsMap.put(K, V)on the entry's key.The maps produced by
MapMakerare serializable, and the deserialized maps retain all the configuration properties of the original map. During deserialization, if the original map had used soft or weak references, the entries are reconstructed as they were, but it's not unlikely they'll be quickly garbage-collected before they are ever accessed.new MapMaker().weakKeys().makeMap()is a recommended replacement forWeakHashMap, but note that it compares keys using object identity whereasWeakHashMapusesObject.equals(java.lang.Object).- Since:
- 2.0 (imported from Google Collections Library)
- Author:
- Bob Lee, Charles Fry, Kevin Bourrillion
-
-
Constructor Summary
Constructors Constructor Description MapMaker()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringtoString()Returns a string representation for this MapMaker instance.
-