public static class Maps.CacheMap<K,V> extends Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>
Modifier and Type | Class and Description |
---|---|
static class |
Maps.CacheMap.Canonicity
The canonicity of a mapping.
|
static class |
Maps.CacheMap.Strength
The types of references to values supported.
|
Modifier and Type | Field and Description |
---|---|
static Maps.CacheMap.Canonicity |
CANONICAL |
static Maps.CacheMap.Strength |
HARD |
static Maps.CacheMap.Canonicity |
NOT_CANONICAL |
static Maps.CacheMap.Strength |
SOFT |
static Maps.CacheMap.Strength |
STRONG |
static Maps.CacheMap.Strength |
WEAK |
DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR, MAXIMUM_CAPACITY
Constructor and Description |
---|
Maps.CacheMap()
Constructs an unbounded, non-canonical cache map with strongly-
referenced values.
|
Maps.CacheMap(int bound)
Constructs a bounded LRU (least recently used), canonical cache map with
strongly-referenced values.
|
Maps.CacheMap(int bound,
Maps.CacheMap.Canonicity canonicity)
Constructs a bounded LRU (least recently used) cache map with
strongly-referenced values.
|
Maps.CacheMap(int bound,
Maps.CacheMap.Strength strength)
Constructs a bounded LRU (least recently used), canonical cache map.
|
Maps.CacheMap(Maps.CacheMap.Strength strength)
Constructs an unbounded, non-canonical cache map.
|
Maps.CacheMap(Maps.CacheStrategy<K,V> strategy)
Constructs a cache map with specified
Maps.CacheStrategy . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all mappings from this map.
|
protected Maps.LinkedEntry<K,V> |
createEntry(int hashCode,
K key,
V value,
Maps.LinkedEntry<K,V> next)
Creates an entry for this map.
|
protected Maps.CacheStrategy<K,V> |
decoratedStrategy(Maps.CacheStrategy<K,V> strategy)
Invoked by the
Maps.CacheMap constructor to allow a subclass to
replace the strategy passed to the constructor. |
protected void |
entryAccessed(Maps.LinkedEntry<K,V> entry)
Invoked when the value of an existing entry is accessed by
Maps.AbstractHashMap.get(java.lang.Object) . |
protected void |
entryAdded(Maps.LinkedEntry<K,V> entry)
Invoked when a new entry is added to this map.
|
protected void |
entryChanged(Maps.LinkedEntry<K,V> oldEntry,
V oldValue,
Maps.LinkedEntry<K,V> newEntry)
Invoked when the value of an existing entry is changed.
|
protected void |
entryEvicted(Maps.LinkedEntry<K,V> entry)
Invoked when an existing entry is evicted by
Maps.AbstractLinkedHashMap.evictEntry(E) ,
e.g., because Maps.AbstractLinkedHashMap.removeEldestEntry(E) returned true. |
protected void |
entryPurged(Maps.LinkedEntry<K,V> entry)
Invoked when an existing entry is purged by
Maps.AbstractHashMap.purgeEntry(E)
after its value has been garbage collected. |
protected void |
entryRemoved(Maps.LinkedEntry<K,V> entry)
Invoked when an existing entry is removed by a remove
method of this map, of a view collection of this map, or of an iterator
of a view collection of this map
|
protected boolean |
evictEntry(Maps.LinkedEntry<K,V> entry)
Evicts an existing entry from this map.
|
protected Maps.LinkedEntry<K,V> |
getEldestEntry()
Gets the eldest entry in this map, or null if the map is empty.
|
protected void |
mapChanged()
Invoked when the map has changed.
|
protected boolean |
removeEldestEntry(Maps.LinkedEntry<K,V> eldest)
Gets whether to evict the eldest entry from this map.
|
createEntryIterator, createEntrySetIterator, createKeySetIterator, createValuesIterator, evict, transfer
addEntry, capacity, change, clone, containsKey, containsValue, entrySet, equals, get, getChangeCount, getEntry, getQueue, hashCode, isEmpty, keyEquals, keyHashCode, keySet, loadFactor, mapConstructed, mapInitialized, purge, purgeEntry, put, putAll, remove, removeExistingEntry, removeKey, size, toString, valueEquals, valueHashCode, values
public static final Maps.CacheMap.Strength STRONG
public static final Maps.CacheMap.Strength HARD
public static final Maps.CacheMap.Strength SOFT
public static final Maps.CacheMap.Strength WEAK
public static final Maps.CacheMap.Canonicity CANONICAL
public static final Maps.CacheMap.Canonicity NOT_CANONICAL
public Maps.CacheMap()
Maps.AbstractHashMap.DEFAULT_INITIAL_CAPACITY
and Maps.AbstractHashMap.DEFAULT_LOAD_FACTOR
,
respectively.public Maps.CacheMap(Maps.CacheMap.Strength strength)
Maps.AbstractHashMap.DEFAULT_INITIAL_CAPACITY
and
Maps.AbstractHashMap.DEFAULT_LOAD_FACTOR
, respectively.public Maps.CacheMap(int bound)
default values
.bound
- The maximum number of entries allowed in the cache.public Maps.CacheMap(int bound, Maps.CacheMap.Strength strength)
default values
.bound
- The maximum number of entries allowed in the cache.strength
- The strength of value references.public Maps.CacheMap(int bound, Maps.CacheMap.Canonicity canonicity)
default values
.bound
- The maximum number of entries allowed in the cache.canonicity
- Canonicity of this cache.public Maps.CacheMap(Maps.CacheStrategy<K,V> strategy)
Maps.CacheStrategy
.protected Maps.CacheStrategy<K,V> decoratedStrategy(Maps.CacheStrategy<K,V> strategy)
Maps.CacheMap
constructor to allow a subclass to
replace the strategy passed to the constructor. The default Maps.CacheMap
implementation returns the strategy passed to it.
This hook is provided to allow a subclass to decorate a strategy given to its constructor without running afoul of superclass-not- initialized issues.
strategy
- The strategy passed to the CacheMap constructor.protected final Maps.LinkedEntry<K,V> createEntry(int hashCode, K key, V value, Maps.LinkedEntry<K,V> next)
The Maps.CacheMap
implementation delegates to Maps.CacheStrategy.createEntry(int, K, V, oracle.javatools.util.Maps.LinkedEntry<K, V>, java.lang.ref.ReferenceQueue<V>)
.
createEntry
in class Maps.AbstractHashMap<K,V,Maps.LinkedEntry<K,V>>
hashCode
- The hashCode of the key for this entry.key
- The key for this entry.value
- The value for this entry.next
- The entry to follow this entry in the entry chain.protected final void mapChanged()
Maps.AbstractLinkedHashMap
Maps.AbstractHashMap.mapInitialized()
), it only changes when the map is cleared or
when entries are added, removed, or purged. In the latter cases, this
method is invoked before the corresponding Maps.AbstractHashMap.entryAdded(E)
,
Maps.AbstractHashMap.entryRemoved(E)
or Maps.AbstractHashMap.entryPurged(E)
method.
The Maps.AbstractHashMap
implementation does nothing.
For subclasses of Maps.AbstractLinkedHashMap
, this method is also
invoked when iteration order is by access and an entry is
accessed. This method is invoked before the associated
Maps.AbstractHashMap.entryAccessed(E)
method is invoked.
Maps.AbstractLinkedHashMap
implementation does nothing.mapChanged
in class Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final void entryAdded(Maps.LinkedEntry<K,V> entry)
Maps.AbstractHashMap
Maps.AbstractHashMap.mapInitialized()
), entries are only added
by Maps.AbstractHashMap.put(K, V)
.
The Maps.AbstractHashMap
implementation does nothing.entryAdded
in class Maps.AbstractHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final void entryChanged(Maps.LinkedEntry<K,V> oldEntry, V oldValue, Maps.LinkedEntry<K,V> newEntry)
Maps.AbstractHashMap
Maps.AbstractHashMap.mapInitialized()
), entries are only changed
by Maps.AbstractHashMap.put(K, V)
.
If the entry to change was mutable, its value is changed with Map.Entry#setValue
and oldEntry and newEntry are the
same object. If the entry to change was not mutable, a new entry
must be created and oldEntry and newEntry are
different objects.
When a client directly invokes the Map.Entry#setValue
method of
any of the Map.Entry
implementations provided in this class,
this method is not invoked (while this could be addressed, it would
require each entry instance to hold a reference to its map).
The Maps.AbstractHashMap
implementation does nothing.entryChanged
in class Maps.AbstractHashMap<K,V,Maps.LinkedEntry<K,V>>
oldEntry
- The existing entry that changed.oldValue
- The old value of the entry.newEntry
- The new entry, which may be the same as the old.protected final void entryAccessed(Maps.LinkedEntry<K,V> entry)
Maps.AbstractHashMap
Maps.AbstractHashMap.get(java.lang.Object)
.
Access to an entry by iterating a view collection of this map does
not cause this method to be invoked.
The Maps.AbstractHashMap
implementation does nothing.entryAccessed
in class Maps.AbstractHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final void entryRemoved(Maps.LinkedEntry<K,V> entry)
Maps.AbstractHashMap
The Maps.AbstractHashMap
implementation never invokes Maps.AbstractHashMap.entryRemoved(E)
when entries are removed by the Maps.AbstractHashMap.clear()
method of
this map, or of a view collection of this map.
The Maps.AbstractHashMap
implementation does nothing.
entryRemoved
in class Maps.AbstractHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final void entryPurged(Maps.LinkedEntry<K,V> entry)
Maps.AbstractHashMap
Maps.AbstractHashMap.purgeEntry(E)
after its value has been garbage collected.
The Maps.AbstractHashMap
implementation only ever invokes
Maps.AbstractHashMap.entryPurged(E)
for entries which extend SoftReference
or
WeakReference
and which register themselves with the
ReferenceQueue
returned by Maps.AbstractHashMap.getQueue()
. Maps
provides four such classes: Maps.SoftEntry
, Maps.WeakEntry
, Maps.SoftLinkedEntry
, and Maps.WeakLinkedEntry
.
The Maps.AbstractHashMap
implementation does nothing.
entryPurged
in class Maps.AbstractHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final void entryEvicted(Maps.LinkedEntry<K,V> entry)
Maps.AbstractLinkedHashMap.evictEntry(E)
,
e.g., because Maps.AbstractLinkedHashMap.removeEldestEntry(E)
returned true.
The Maps.AbstractLinkedHashMap
implementation does nothing.
If this map is canonical, eviction means the entry was converted to a
weak reference and mapChanged()
was not invoked. If this
map is not canonical, eviction means the entry was removed and
mapChanged()
was invoked.
entryEvicted
in class Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final boolean removeEldestEntry(Maps.LinkedEntry<K,V> eldest)
Maps.AbstractHashMap.entryAdded(E)
is invoked. If
Maps.AbstractHashMap.entryAdded(E)
modifies the map, however, this method is not
invoked. The removal of the entry, if requested, causes the Maps.AbstractLinkedHashMap.entryEvicted(E)
method to be invoked, rather than the Maps.AbstractHashMap.entryRemoved(E)
method.
The Maps.AbstractLinkedHashMap
implementation returns false.
In an insertion-ordered map, the eldest entry is the least recently inserted one; in an access-ordered map, it is the least recently accessed entry.
This method is provided for compatibility with LinkedHashMap.removeEldestEntry(java.util.Map.Entry<K, V>)
.
The Maps.CacheMap
implementation returns false, like its superclass.
It is overridden here only to make it final; use a Maps.CacheStrategy
to determine the eviction strategy.
removeEldestEntry
in class Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>
eldest
- The eldest entry in the map.protected Maps.LinkedEntry<K,V> getEldestEntry()
Maps.AbstractLinkedHashMap
In an insertion-ordered map, the eldest entry is the least recently inserted one; in an access-ordered map, it is the least recently accessed entry.
TheMaps.AbstractLinkedHashMap
implementation does nothing.getEldestEntry
in class Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>
protected final boolean evictEntry(Maps.LinkedEntry<K,V> entry)
Maps.CacheMap
implementation ensures that, if this map is
canonical, the mapping represented by the evicted entry is retained for
as long as an external reference to the value exists.evictEntry
in class Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>
entry
- The entry to evict.public void clear()
Maps.AbstractLinkedHashMap
clear
in interface java.util.Map<K,V>
clear
in class Maps.AbstractLinkedHashMap<K,V,Maps.LinkedEntry<K,V>>