Allow subclasses to customize the reference.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2077 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -23,8 +23,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements a {@link SoftReference} cache wherein the values in the hashmap
|
* Implements a {@link SoftReference} cache wherein the values in the hashmap are not prevented
|
||||||
* are not prevented from being garbage collected.
|
* from being garbage collected.
|
||||||
*/
|
*/
|
||||||
public class SoftCache<K,V>
|
public class SoftCache<K,V>
|
||||||
{
|
{
|
||||||
@@ -44,8 +44,7 @@ public class SoftCache<K,V>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the supplied key exists in this map and is mapped to an
|
* Returns true if the supplied key exists in this map and is mapped to an active value.
|
||||||
* active value.
|
|
||||||
*/
|
*/
|
||||||
public boolean containsKey (K key)
|
public boolean containsKey (K key)
|
||||||
{
|
{
|
||||||
@@ -73,13 +72,13 @@ public class SoftCache<K,V>
|
|||||||
*/
|
*/
|
||||||
public V put (K key, V value)
|
public V put (K key, V value)
|
||||||
{
|
{
|
||||||
SoftReference<V> old = _map.put(key, new SoftReference<V>(value));
|
SoftReference<V> old = _map.put(key, createReference(value));
|
||||||
return (old == null) ? null : old.get();
|
return (old == null) ? null : old.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes the specified key from the map. Returns the value to which the
|
* Removes the specified key from the map. Returns the value to which the key was previously
|
||||||
* key was previously mapped or null.
|
* mapped or null.
|
||||||
*/
|
*/
|
||||||
public V remove (K key)
|
public V remove (K key)
|
||||||
{
|
{
|
||||||
@@ -95,5 +94,14 @@ public class SoftCache<K,V>
|
|||||||
_map.clear();
|
_map.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and returns a {@link SoftReference} to the supplied value. Subclasses can override
|
||||||
|
* to return custom subclasses.
|
||||||
|
*/
|
||||||
|
protected SoftReference<V> createReference (V value)
|
||||||
|
{
|
||||||
|
return new SoftReference<V>(value);
|
||||||
|
}
|
||||||
|
|
||||||
protected HashMap<K,SoftReference<V>> _map;
|
protected HashMap<K,SoftReference<V>> _map;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user