Class SoftHashMap<K,V>
- java.lang.Object
-
- de.gsi.dataset.utils.SoftHashMap<K,V>
-
- Type Parameters:
K- the type of keys maintained by this mapV- the type of mapped values
- All Implemented Interfaces:
java.util.Map<K,V>
public class SoftHashMap<K,V> extends java.lang.Object implements java.util.Map<K,V>A memory-constrainedSoftHashMapthat stores its values inSoftReferences.N.B. JDK's
WeakHashMap, which usesWeakReferences for its keys rather than for its values. SeeSoftKeyHashMapfor aWeakHashMap-similar implementation using SoftReference-ed keys.Having the values wrapped by soft references allows the cache to automatically reduce its size based on memory limitations and garbage collection. This ensures that the cache will not cause memory leaks by holding strong references to all of its values.
This implementation is thread-safe and usable in concurrent environments.
- Author:
- Heinz Kabutz, original author public version, rstein, adapted to use within de.gsi.dataset
- See Also:
SoftKeyHashMap
-
-
Constructor Summary
Constructors Constructor Description SoftHashMap()Creates a new SoftHashMap with a default retention size size ofDEFAULT_RETENTION_SIZE(100 entries).SoftHashMap(int retentionSize)Creates a new SoftHashMap with the specified retention size.SoftHashMap(java.util.Map<K,V> source)Creates aSoftHashMapbacked by the specifiedsource, with a default retention size ofDEFAULT_RETENTION_SIZE(100 entries).SoftHashMap(java.util.Map<K,V> source, int retentionSize)Creates aSoftHashMapbacked by the specifiedsource, with the specified retention size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)java.util.Set<java.util.Map.Entry<K,V>>entrySet()Vget(java.lang.Object key)booleanisEmpty()java.util.Set<K>keySet()Vput(K key, V value)Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection.voidputAll(java.util.Map<? extends K,? extends V> m)Vremove(java.lang.Object key)intsize()java.util.Collection<V>values()
-
-
-
Constructor Detail
-
SoftHashMap
public SoftHashMap()
Creates a new SoftHashMap with a default retention size size ofDEFAULT_RETENTION_SIZE(100 entries).- See Also:
SoftHashMap(int)
-
SoftHashMap
public SoftHashMap(int retentionSize)
Creates a new SoftHashMap with the specified retention size.The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (ie 'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references.
Note that in a highly concurrent environments the exact total number of strong references may differ slightly than the actual
retentionSizevalue. This number is intended to be a best-effort retention low water mark.- Parameters:
retentionSize- the total number of most recent entries in the map that will be strongly referenced (retained), preventing them from being eagerly garbage collected by the JVM.
-
SoftHashMap
public SoftHashMap(java.util.Map<K,V> source)
Creates aSoftHashMapbacked by the specifiedsource, with a default retention size ofDEFAULT_RETENTION_SIZE(100 entries).- Parameters:
source- the backing map to populate thisSoftHashMap- See Also:
SoftHashMap(Map,int)
-
SoftHashMap
public SoftHashMap(java.util.Map<K,V> source, int retentionSize)
Creates aSoftHashMapbacked by the specifiedsource, with the specified retention size.The retention size (n) is the total number of most recent entries in the map that will be strongly referenced (ie 'retained') to prevent them from being eagerly garbage collected. That is, the point of a SoftHashMap is to allow the garbage collector to remove as many entries from this map as it desires, but there will always be (n) elements retained after a GC due to the strong references.
Note that in a highly concurrent environments the exact total number of strong references may differ slightly than the actual
retentionSizevalue. This number is intended to be a best-effort retention low water mark.- Parameters:
source- the backing map to populate thisSoftHashMapretentionSize- the total number of most recent entries in the map that will be strongly referenced (retained), preventing them from being eagerly garbage collected by the JVM.
-
-
Method Detail
-
containsKey
public boolean containsKey(java.lang.Object key)
-
containsValue
public boolean containsValue(java.lang.Object value)
-
put
public V put(K key, V value)
Creates a new entry, but wraps the value in a SoftValue instance to enable auto garbage collection.- Specified by:
putin interfacejava.util.Map<K,V>- Parameters:
key- key with which the specified value is to be associatedvalue- value to be associated with the specified key- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. (Anullreturn can also indicate that the map previously associatednullwithkey, if the implementation supportsnullvalues.)
-
-