|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.AbstractMap
sun.misc.SoftCache
public class SoftCache
A memory-sensitive implementation of the Map interface.
A SoftCache object uses soft references to implement a memory-sensitive hash map. If the garbage
collector determines at a certain point in time that a value object in a
SoftCache entry is no longer strongly reachable, then it may
remove that entry in order to release the memory occupied by the value
object. All SoftCache objects are guaranteed to be completely
cleared before the virtual machine will throw an
OutOfMemoryError. Because of this automatic clearing feature,
the behavior of this class is somewhat different from that of other
Map implementations.
Both null values and the null key are supported. This class has the
same performance characteristics as the HashMap class, and has
the same efficiency parameters of initial capacity and load
factor.
Like most collection classes, this class is not synchronized. A
synchronized SoftCache may be constructed using the
Collections.synchronizedMap method.
In typical usage this class will be subclassed and the fill
method will be overridden. When the get method is invoked on a
key for which there is no mapping in the cache, it will in turn invoke the
fill method on that key in an attempt to construct a
corresponding value. If the fill method returns such a value
then the cache will be updated and the new value will be returned. Thus,
for example, a simple URL-content cache can be constructed as follows:
public class URLCache extends SoftCache {
protected Object fill(Object key) {
return ((URL)key).getContent();
}
}
The behavior of the SoftCache class depends in part upon
the actions of the garbage collector, so several familiar (though not
required) Map invariants do not hold for this class.
Because entries are removed from a SoftCache in response to
dynamic advice from the garbage collector, a SoftCache may
behave as though an unknown thread is silently removing entries. In
particular, even if you synchronize on a SoftCache instance and
invoke none of its mutator methods, it is possible for the size
method to return smaller values over time, for the isEmpty
method to return false and then true, for the
containsKey method to return true and later
false for a given key, for the get method to
return a value for a given key but later return null, for the
put method to return null and the
remove method to return false for a key that
previously appeared to be in the map, and for successive examinations of the
key set, the value set, and the entry set to yield successively smaller
numbers of elements.
HashMap,
SoftReference| Nested Class Summary |
|---|
| Nested classes/interfaces inherited from class java.util.AbstractMap |
|---|
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V> |
| Constructor Summary | |
|---|---|
SoftCache()
Construct a new, empty SoftCache with the default
capacity and the default load factor. |
|
SoftCache(int initialCapacity)
Construct a new, empty SoftCache with the given
initial capacity and the default load factor. |
|
SoftCache(int initialCapacity,
float loadFactor)
Construct a new, empty SoftCache with the given
initial capacity and the given load factor. |
|
| Method Summary | |
|---|---|
void |
clear()
Remove all mappings from this cache. |
boolean |
containsKey(Object key)
Return true if this cache contains a mapping for the
specified key. |
Set |
entrySet()
Return a Set view of the mappings in this cache. |
protected Object |
fill(Object key)
Create a value object for the given key. |
Object |
get(Object key)
Return the value to which this cache maps the specified key. |
boolean |
isEmpty()
Return true if this cache contains no key-value mappings. |
Object |
put(Object key,
Object value)
Update this cache so that the given key maps to the given
value. |
Object |
remove(Object key)
Remove the mapping for the given key from this cache, if
present. |
int |
size()
Return the number of key-value mappings in this cache. |
| Methods inherited from class java.util.AbstractMap |
|---|
clone, containsValue, equals, hashCode, keySet, putAll, toString, values |
| Methods inherited from class java.lang.Object |
|---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface java.util.Map |
|---|
containsValue, equals, hashCode, keySet, putAll, values |
| Constructor Detail |
|---|
public SoftCache(int initialCapacity,
float loadFactor)
SoftCache with the given
initial capacity and the given load factor.
initialCapacity - The initial capacity of the cacheloadFactor - A number between 0.0 and 1.0
IllegalArgumentException - If the initial capacity is less than
or equal to zero, or if the load
factor is less than zeropublic SoftCache(int initialCapacity)
SoftCache with the given
initial capacity and the default load factor.
initialCapacity - The initial capacity of the cache
IllegalArgumentException - If the initial capacity is less than
or equal to zeropublic SoftCache()
SoftCache with the default
capacity and the default load factor.
| Method Detail |
|---|
public int size()
size in interface Mapsize in class AbstractMapSet.size()public boolean isEmpty()
true if this cache contains no key-value mappings.
isEmpty in interface MapisEmpty in class AbstractMapAbstractMap.size()public boolean containsKey(Object key)
true if this cache contains a mapping for the
specified key. If there is no mapping for the key, this method will not
attempt to construct one by invoking the fill method.
containsKey in interface MapcontainsKey in class AbstractMapkey - The key whose presence in the cache is to be tested
AbstractMap.containsValue(Object)protected Object fill(Object key)
key. This method is
invoked by the get method when there is no entry for
key. If this method returns a non-null value,
then the cache will be updated to map key to that value,
and that value will be returned by the get method.
The default implementation of this method simply returns
null for every key value. A subclass may
override this method to provide more useful behavior.
key - The key for which a value is to be computed
key, or null if one
could not be computedget(java.lang.Object)public Object get(Object key)
key. If the cache does not presently contain a value for
this key, then invoke the fill method in an attempt to
compute such a value. If that method returns a non-null
value, then update the cache and return the new value. Otherwise,
return null.
Note that because this method may update the cache, it is considered
a mutator and may cause ConcurrentModificationExceptions to
be thrown if invoked while an iterator is in use.
get in interface Mapget in class AbstractMapkey - The key whose associated value, if any, is to be returned
fill(java.lang.Object)
public Object put(Object key,
Object value)
key maps to the given
value. If the cache previously contained a mapping for
key then that mapping is replaced and the old value is
returned.
put in interface Mapput in class AbstractMapkey - The key that is to be mapped to the given
valuevalue - The value to which the given key is to be
mapped
null if if there was no mapping for the keyAbstractMap.containsKey(Object)public Object remove(Object key)
key from this cache, if
present.
remove in interface Mapremove in class AbstractMapkey - The key whose mapping is to be removed
null if
there was no mapping for the keyIterator.remove()public void clear()
clear in interface Mapclear in class AbstractMapSet.clear()public Set entrySet()
Set view of the mappings in this cache.
entrySet in interface MapentrySet in class AbstractMapMap.Entry
|
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||