Deprecated
No direct replacement; java.
addresses a related by different use-case.
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.
java.util.HashMap , java.lang.ref.SoftReference
| Modifier and Type | Class and Description |
|---|---|
| private class | |
| private class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private Set | |
| private Map | |
| private ReferenceQueue |
| Access | Constructor and Description |
|---|---|
| public | SoftCache(int
The initial capacity of the cache initialCapacity, float A number between 0.0 and 1.0 loadFactor)Construct a new, empty |
| public | SoftCache(int
The initial capacity of the cache initialCapacity)Construct a new, empty |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Overrides java. Implements java. Remove all mappings from this cache. |
| public boolean | containsKey(Object
The key whose presence in the cache is to be tested key)Overrides java. Implements java. Return |
| public Set | entrySet()
Implements abstract java. Implements java. Return a |
| protected Object | |
| public Object | get(Object
The key whose associated value, if any, is to be returned key)Overrides java. Implements java. Return the value to which this cache maps the specified
|
| public boolean | isEmpty()
Overrides java. Implements java. Return |
| private void | |
| public Object | Returns: The previous value to which this key was mapped, ornull if there was no mapping for the keyThe key that is to be mapped to the given
key, Object valueThe value to which the given value)key is to be
mappedOverrides java. Implements java. Update this cache so that the given |
| public Object | Returns: The value to which this key was mapped, ornull if
there was no mapping for the keyThe key whose mapping is to be removed key)Overrides java. Implements java. Remove the mapping for the given |
| public int | size()
Overrides java. Implements java. Return the number of key-value mappings in this cache. |
| private static boolean |
| entrySet | back to summary |
|---|---|
| private Set<Map. | |
| hash | back to summary |
|---|---|
| private Map<Object, Object> hash | |
| queue | back to summary |
|---|---|
| private ReferenceQueue<Object> queue | |
| SoftCache | back to summary |
|---|---|
| public SoftCache(int initialCapacity, float loadFactor) Construct a new, empty
| |
| SoftCache | back to summary |
|---|---|
| public SoftCache(int initialCapacity) Construct a new, empty
| |
| SoftCache | back to summary |
|---|---|
| public SoftCache() Construct a new, empty | |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Remove all mappings from this cache. | |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(Object key) Overrides java. Implements java. Return
| |
| entrySet | back to summary |
|---|---|
| public Set Implements abstract java. Implements java. Return a | |
| fill | back to summary |
|---|---|
| protected Object fill(Object key) Create a value object for the given The default implementation of this method simply returns
| |
| get | back to summary |
|---|---|
| public Object get(Object key) Overrides java. Implements java. Return the value to which this cache maps the specified
Note that because this method may update the cache, it is considered
a mutator and may cause | |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides java. Implements java. Return | |
| processQueue | back to summary |
|---|---|
| private void processQueue() | |
| put | back to summary |
|---|---|
| public Object put(Object key, Object value) Overrides java. Implements java. Update this cache so that the given | |
| remove | back to summary |
|---|---|
| public Object remove(Object key) Overrides java. Implements java. Remove the mapping for the given | |
| size | back to summary |
|---|---|
| public int size() Overrides java. Implements java. Return the number of key-value mappings in this cache. The time required by this operation is linear in the size of the map. | |
| valEquals | back to summary |
|---|---|
| private static boolean valEquals(Object o1, Object o2) | |
| Modifier and Type | Field and Description |
|---|---|
| private Map. | |
| private Object |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object o)
Overrides java. Implements java. Compares the specified object with this entry for equality. |
| public Object | |
| public Object | |
| public int | hashCode()
Overrides java. Implements java. Returns the hash code value for this map entry. |
| public Object | setValue(Object value)
Implements java. Replaces the value corresponding to this entry with the specified value (optional operation). |
| ent | back to summary |
|---|---|
| private Map. | |
| value | back to summary |
|---|---|
| private Object value | |
| Entry | back to summary |
|---|---|
| pack-priv Entry(Map. | |
| equals | back to summary |
|---|---|
| public boolean equals(Object o) Overrides java. Implements java. Doc from java. Compares the specified object with this entry for equality.
Returns
(e1.getKey()==null ?
e2.getKey()==null : e1.getKey().equals(e2.getKey())) &&
(e1.getValue()==null ?
e2.getValue()==null : e1.getValue().equals(e2.getValue()))
This ensures that the equals method works properly across
different implementations of the Map.Entry interface.
| |
| getKey | back to summary |
|---|---|
| public Object getKey() Implements java. Doc from java. Returns the key corresponding to this entry. | |
| getValue | back to summary |
|---|---|
| public Object getValue() Implements java. Doc from java. Returns the value corresponding to this entry. If the mapping
has been removed from the backing map (by the iterator's
| |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Implements java. Doc from java. Returns the hash code value for this map entry. The hash code
of a map entry
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^
(e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that
e1.hashCode()==e2.hashCode() for any two Entries
e1 and e2, as required by the general
contract of Object.hashCode.
| |
| setValue | back to summary |
|---|---|
| public Object setValue(Object value) Implements java. Doc from java. Replaces the value corresponding to this entry with the specified
value (optional operation). (Writes through to the map.) The
behavior of this call is undefined if the mapping has already been
removed from the map (by the iterator's | |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isEmpty()
Overrides java. Implements java. Returns |
| public Iterator | iterator()
Implements abstract java. Implements java. Returns an iterator over the elements in this set. |
| public boolean | remove(Object o)
Overrides java. Implements java. Removes the specified element from this set if it is present (optional operation). |
| public int | size()
Implements abstract java. Implements java. Returns the number of elements in this set (its cardinality). |
| EntrySet | back to summary |
|---|---|
| private EntrySet() | |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides java. Implements java. Doc from java. Returns | |
| iterator | back to summary |
|---|---|
| public Iterator Implements abstract java. Implements java. Doc from java. Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee). | |
| remove | back to summary |
|---|---|
| public boolean remove(Object o) Overrides java. Implements java. Doc from java. Removes the specified element from this set if it is present
(optional operation). More formally, removes an element | |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this set (its cardinality). If this
set contains more than | |
| Modifier and Type | Field and Description |
|---|---|
| private static int | |
| private static Object | |
| private Object |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private static SoftCache. | |
| private void | |
| private boolean | |
| private static Object |
| dropped | back to summary |
|---|---|
| private static int dropped | |
| INVALID_KEY | back to summary |
|---|---|
| private static Object INVALID_KEY | |
| key | back to summary |
|---|---|
| private Object key | |
| ValueCell | back to summary |
|---|---|
| private ValueCell(Object key, Object value, ReferenceQueue<Object> queue) | |
| create | back to summary |
|---|---|
| private static SoftCache. | |
| drop | back to summary |
|---|---|
| private void drop() | |
| isValid | back to summary |
|---|---|
| private boolean isValid() | |
| strip | back to summary |
|---|---|
| private static Object strip(Object val, boolean drop) | |