|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.google.inject.internal.util.MapMaker
public final class MapMaker
A ConcurrentMap
builder, providing any combination of these
features: soft or weak keys, soft or weak values, timed expiration, and on-demand
computation of values. Usage example:
ConcurrentMap<Key, Graph> graphs = new MapMaker()
.concurrencyLevel(32)
.softKeys()
.weakValues()
.expiration(30, TimeUnit.MINUTES)
.makeComputingMap(
new Function<Key, Graph>() {
public Graph apply(Key key) {
return createExpensiveGraph(key);
}
});
These features are all optional; new MapMaker().makeMap()
returns a valid concurrent map that behaves exactly like a
ConcurrentHashMap
.
The returned map is implemented as a hash table with similar performance
characteristics to ConcurrentHashMap
. It supports all optional
operations of the ConcurrentMap
interface. It does not permit
null keys or values. It is serializable; however, serializing a map that
uses soft or weak references can give unpredictable results.
Note: by default, the returned map uses equality comparisons
(the equals
method) to determine equality
for keys or values. However, if weakKeys()
or softKeys()
was specified, the map uses identity (==
)
comparisons instead for keys. Likewise, if weakValues()
or
softValues()
was specified, the map uses identity comparisons
for values.
The returned map has weakly consistent iteration: an iterator over one of the map's view collections may reflect some, all or none of the changes made to the map after the iterator was created.
An entry whose key or value is reclaimed by the garbage collector
immediately disappears from the map. (If the default settings of strong
keys and strong values are used, this will never happen.) The client can
never observe a partially-reclaimed entry. Any Map.Entry
instance retrieved from the map's entry set
is snapshot of that entry's state at the time of retrieval.
new MapMaker().weakKeys().makeMap()
can almost always be
used as a drop-in replacement for WeakHashMap
, adding
concurrency, asynchronous cleanup, identity-based equality for keys, and
great flexibility.
Constructor Summary | |
---|---|
MapMaker()
Constructs a new MapMaker instance with default settings,
including strong keys, strong values, and no automatic expiration. |
Method Summary | ||
---|---|---|
MapMaker |
concurrencyLevel(int concurrencyLevel)
Guides the allowed concurrency among update operations. |
|
MapMaker |
expiration(long duration,
TimeUnit unit)
Specifies that each entry should be automatically removed from the map once a fixed duration has passed since the entry's creation. |
|
MapMaker |
initialCapacity(int initialCapacity)
Sets a custom initial capacity (defaults to 16). |
|
MapMaker |
loadFactor(float loadFactor)
Sets a custom load factor (defaults to 0.75). |
|
|
makeComputingMap(Function<? super K,? extends V> computer)
Builds a map that supports atomic, on-demand computation of values. |
|
|
makeMap()
Builds the final map, without on-demand computation of values. |
|
MapMaker |
softKeys()
Specifies that each key (not value) stored in the map should be wrapped in a SoftReference (by default, strong references
are used). |
|
MapMaker |
softValues()
Specifies that each value (not key) stored in the map should be wrapped in a SoftReference (by default, strong references
are used). |
|
MapMaker |
weakKeys()
Specifies that each key (not value) stored in the map should be wrapped in a WeakReference (by default, strong references
are used). |
|
MapMaker |
weakValues()
Specifies that each value (not key) stored in the map should be wrapped in a WeakReference (by default, strong references
are used). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public MapMaker()
MapMaker
instance with default settings,
including strong keys, strong values, and no automatic expiration.
Method Detail |
---|
public MapMaker initialCapacity(int initialCapacity)
IllegalArgumentException
- if initialCapacity
is
negative
IllegalStateException
- if an initial capacity was already set
(TODO: make that true)public MapMaker loadFactor(float loadFactor)
IllegalArgumentException
- if loadFactor
is
nonpositive
IllegalStateException
- if a load factor was already set
(TODO: make that true)public MapMaker concurrencyLevel(int concurrencyLevel)
IllegalArgumentException
- if concurrencyLevel
is
nonpositive
IllegalStateException
- if a concurrency level was already set
(TODO: make that true)public MapMaker weakKeys()
WeakReference
(by default, strong references
are used).
IllegalStateException
- if the key strength was already setpublic MapMaker softKeys()
SoftReference
(by default, strong references
are used).
IllegalStateException
- if the key strength was already setpublic MapMaker weakValues()
WeakReference
(by default, strong references
are used).
IllegalStateException
- if the key strength was already setpublic MapMaker softValues()
SoftReference
(by default, strong references
are used).
IllegalStateException
- if the value strength was already setpublic MapMaker expiration(long duration, TimeUnit unit)
duration
- the length of time after an entry is created that it
should be automatically removedunit
- the unit that duration
is expressed in
IllegalArgumentException
- if duration
is not positive
IllegalStateException
- if the expiration time was already setpublic <K,V> ConcurrentMap<K,V> makeMap()
K
- the type of keys to be stored in the returned mapV
- the type of values to be stored in the returned map
public <K,V> ConcurrentMap<K,V> makeComputingMap(Function<? super K,? extends V> computer)
Map.get(java.lang.Object)
returns the value corresponding to the given key, atomically
computes it using the computer function passed to this builder, or waits
for another thread to compute the value if necessary. Only one value will
be computed for each key at a given time.
If an entry's value has not finished computing yet, query methods
besides Map.get(java.lang.Object)
return immediately as if an entry doesn't
exist. In other words, an entry isn't externally visible until the value's
computation completes.
Map.get(java.lang.Object)
in the returned map implementation throws:
NullPointerException
if the key is null or the computer returns
nullComputationException
wrapping an exception thrown by the
computationNote: Callers of get()
must ensure that the key
argument is of type K
. Map.get()
takes Object
, so
the key type is not checked at compile time. Passing an object of a type
other than K
can result in that object being unsafely passed to the
computer function as type K
not to mention the unsafe key being
stored in the map.
If Map.put(K, V)
is called before a computation completes,
other threads waiting on the computation will wake up and return the put
value up until the computation completes, at which point the computation
result will overwrite the value from the put
in the map.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |