|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
|
|
package sun.misc; |
|
|
|
import java.lang.ref.SoftReference; |
|
import java.lang.ref.ReferenceQueue; |
|
|
|
import java.util.Iterator; |
|
import java.util.Map; |
|
import java.util.AbstractMap; |
|
import java.util.HashMap; |
|
import java.util.Set; |
|
import java.util.AbstractSet; |
|
import java.util.NoSuchElementException; |
|
|
|
|
|
/** |
|
* A memory-sensitive implementation of the <code>Map</code> interface. |
|
* |
|
* <p> A <code>SoftCache</code> object uses {@link java.lang.ref.SoftReference |
|
* 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 |
|
* <code>SoftCache</code> entry is no longer strongly reachable, then it may |
|
* remove that entry in order to release the memory occupied by the value |
|
* object. All <code>SoftCache</code> objects are guaranteed to be completely |
|
* cleared before the virtual machine will throw an |
|
* <code>OutOfMemoryError</code>. Because of this automatic clearing feature, |
|
* the behavior of this class is somewhat different from that of other |
|
* <code>Map</code> implementations. |
|
* |
|
* <p> Both null values and the null key are supported. This class has the |
|
* same performance characteristics as the <code>HashMap</code> class, and has |
|
* the same efficiency parameters of <em>initial capacity</em> and <em>load |
|
* factor</em>. |
|
* |
|
* <p> Like most collection classes, this class is not synchronized. A |
|
* synchronized <code>SoftCache</code> may be constructed using the |
|
* <code>Collections.synchronizedMap</code> method. |
|
* |
|
* <p> In typical usage this class will be subclassed and the <code>fill</code> |
|
* method will be overridden. When the <code>get</code> method is invoked on a |
|
* key for which there is no mapping in the cache, it will in turn invoke the |
|
* <code>fill</code> method on that key in an attempt to construct a |
|
* corresponding value. If the <code>fill</code> 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: |
|
* |
|
* <pre> |
|
* public class URLCache extends SoftCache { |
|
* protected Object fill(Object key) { |
|
* return ((URL)key).getContent(); |
|
* } |
|
* } |
|
* </pre> |
|
* |
|
* <p> The behavior of the <code>SoftCache</code> class depends in part upon |
|
* the actions of the garbage collector, so several familiar (though not |
|
* required) <code>Map</code> invariants do not hold for this class. <p> |
|
* Because entries are removed from a <code>SoftCache</code> in response to |
|
* dynamic advice from the garbage collector, a <code>SoftCache</code> may |
|
* behave as though an unknown thread is silently removing entries. In |
|
* particular, even if you synchronize on a <code>SoftCache</code> instance and |
|
* invoke none of its mutator methods, it is possible for the <code>size</code> |
|
* method to return smaller values over time, for the <code>isEmpty</code> |
|
* method to return <code>false</code> and then <code>true</code>, for the |
|
* <code>containsKey</code> method to return <code>true</code> and later |
|
* <code>false</code> for a given key, for the <code>get</code> method to |
|
* return a value for a given key but later return <code>null</code>, for the |
|
* <code>put</code> method to return <code>null</code> and the |
|
* <code>remove</code> method to return <code>false</code> 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. |
|
* |
|
* @author Mark Reinhold |
|
* @since 1.2 |
|
* @see java.util.HashMap |
|
* @see java.lang.ref.SoftReference |
|
*/ |
|
|
|
|
|
public class SoftCache extends AbstractMap implements Map { |
|
|
|
/* The basic idea of this implementation is to maintain an internal HashMap |
|
that maps keys to soft references whose referents are the keys' values; |
|
the various accessor methods dereference these soft references before |
|
returning values. Because we don't have access to the innards of the |
|
HashMap, each soft reference must contain the key that maps to it so |
|
that the processQueue method can remove keys whose values have been |
|
discarded. Thus the HashMap actually maps keys to instances of the |
|
ValueCell class, which is a simple extension of the SoftReference class. |
|
*/ |
|
|
|
|
|
static private class ValueCell extends SoftReference { |
|
static private Object INVALID_KEY = new Object(); |
|
static private int dropped = 0; |
|
private Object key; |
|
|
|
private ValueCell(Object key, Object value, ReferenceQueue queue) { |
|
super(value, queue); |
|
this.key = key; |
|
} |
|
|
|
private static ValueCell create(Object key, Object value, |
|
ReferenceQueue queue) |
|
{ |
|
if (value == null) return null; |
|
return new ValueCell(key, value, queue); |
|
} |
|
|
|
private static Object strip(Object val, boolean drop) { |
|
if (val == null) return null; |
|
ValueCell vc = (ValueCell)val; |
|
Object o = vc.get(); |
|
if (drop) vc.drop(); |
|
return o; |
|
} |
|
|
|
private boolean isValid() { |
|
return (key != INVALID_KEY); |
|
} |
|
|
|
private void drop() { |
|
super.clear(); |
|
key = INVALID_KEY; |
|
dropped++; |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private Map hash; |
|
|
|
|
|
private ReferenceQueue queue = new ReferenceQueue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private void processQueue() { |
|
ValueCell vc; |
|
while ((vc = (ValueCell)queue.poll()) != null) { |
|
if (vc.isValid()) hash.remove(vc.key); |
|
else ValueCell.dropped--; |
|
} |
|
} |
|
|
|
|
|
/* -- Constructors -- */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SoftCache(int initialCapacity, float loadFactor) { |
|
hash = new HashMap(initialCapacity, loadFactor); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public SoftCache(int initialCapacity) { |
|
hash = new HashMap(initialCapacity); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
public SoftCache() { |
|
hash = new HashMap(); |
|
} |
|
|
|
|
|
/* -- Simple queries -- */ |
|
|
|
|
|
|
|
|
|
*/ |
|
public int size() { |
|
return entrySet().size(); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public boolean isEmpty() { |
|
return entrySet().isEmpty(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean containsKey(Object key) { |
|
return ValueCell.strip(hash.get(key), false) != null; |
|
} |
|
|
|
|
|
/* -- Lookup and modification operations -- */ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected Object fill(Object key) { |
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object get(Object key) { |
|
processQueue(); |
|
Object v = hash.get(key); |
|
if (v == null) { |
|
v = fill(key); |
|
if (v != null) { |
|
hash.put(key, ValueCell.create(key, v, queue)); |
|
return v; |
|
} |
|
} |
|
return ValueCell.strip(v, false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object put(Object key, Object value) { |
|
processQueue(); |
|
ValueCell vc = ValueCell.create(key, value, queue); |
|
return ValueCell.strip(hash.put(key, vc), true); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Object remove(Object key) { |
|
processQueue(); |
|
return ValueCell.strip(hash.remove(key), true); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void clear() { |
|
processQueue(); |
|
hash.clear(); |
|
} |
|
|
|
|
|
/* -- Views -- */ |
|
|
|
private static boolean valEquals(Object o1, Object o2) { |
|
return (o1 == null) ? (o2 == null) : o1.equals(o2); |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private class Entry implements Map.Entry { |
|
private Map.Entry ent; |
|
private Object value; /* Strong reference to value, to prevent the GC |
|
from flushing the value while this Entry |
|
exists */ |
|
|
|
Entry(Map.Entry ent, Object value) { |
|
this.ent = ent; |
|
this.value = value; |
|
} |
|
|
|
public Object getKey() { |
|
return ent.getKey(); |
|
} |
|
|
|
public Object getValue() { |
|
return value; |
|
} |
|
|
|
public Object setValue(Object value) { |
|
return ent.setValue(ValueCell.create(ent.getKey(), value, queue)); |
|
} |
|
|
|
public boolean equals(Object o) { |
|
if (! (o instanceof Map.Entry)) return false; |
|
Map.Entry e = (Map.Entry)o; |
|
return (valEquals(ent.getKey(), e.getKey()) |
|
&& valEquals(value, e.getValue())); |
|
} |
|
|
|
public int hashCode() { |
|
Object k; |
|
return ((((k = getKey()) == null) ? 0 : k.hashCode()) |
|
^ ((value == null) ? 0 : value.hashCode())); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private class EntrySet extends AbstractSet { |
|
Set hashEntries = hash.entrySet(); |
|
|
|
public Iterator iterator() { |
|
|
|
return new Iterator() { |
|
Iterator hashIterator = hashEntries.iterator(); |
|
Entry next = null; |
|
|
|
public boolean hasNext() { |
|
while (hashIterator.hasNext()) { |
|
Map.Entry ent = (Map.Entry)hashIterator.next(); |
|
ValueCell vc = (ValueCell)ent.getValue(); |
|
Object v = null; |
|
if ((vc != null) && ((v = vc.get()) == null)) { |
|
|
|
continue; |
|
} |
|
next = new Entry(ent, v); |
|
return true; |
|
} |
|
return false; |
|
} |
|
|
|
public Object next() { |
|
if ((next == null) && !hasNext()) |
|
throw new NoSuchElementException(); |
|
Entry e = next; |
|
next = null; |
|
return e; |
|
} |
|
|
|
public void remove() { |
|
hashIterator.remove(); |
|
} |
|
|
|
}; |
|
} |
|
|
|
public boolean isEmpty() { |
|
return !(iterator().hasNext()); |
|
} |
|
|
|
public int size() { |
|
int j = 0; |
|
for (Iterator i = iterator(); i.hasNext(); i.next()) j++; |
|
return j; |
|
} |
|
|
|
public boolean remove(Object o) { |
|
processQueue(); |
|
if (o instanceof Entry) return hashEntries.remove(((Entry)o).ent); |
|
else return false; |
|
} |
|
|
|
} |
|
|
|
|
|
private Set entrySet = null; |
|
|
|
|
|
|
|
*/ |
|
public Set entrySet() { |
|
if (entrySet == null) entrySet = new EntrySet(); |
|
return entrySet; |
|
} |
|
|
|
} |