diff options
Diffstat (limited to 'source/com/c2kernel/utils/SoftCache.java')
| -rw-r--r-- | source/com/c2kernel/utils/SoftCache.java | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/source/com/c2kernel/utils/SoftCache.java b/source/com/c2kernel/utils/SoftCache.java index f13c87d..6680f30 100644 --- a/source/com/c2kernel/utils/SoftCache.java +++ b/source/com/c2kernel/utils/SoftCache.java @@ -10,12 +10,12 @@ import java.util.*; *
* $Revision: 1.5 $ $Date: 2004/10/29 13:29:09 $
******************************************************************************/
-public class SoftCache extends AbstractMap {
+public class SoftCache<K, V> extends AbstractMap<K, V> {
- private final Map hash = new HashMap();
+ private final Map<K, SoftValue<V>> hash = new HashMap<K, SoftValue<V>>();
private final int minSize;
- private final LinkedList hardCache = new LinkedList();
- private final ReferenceQueue queue = new ReferenceQueue();
+ private final LinkedList<V> hardCache = new LinkedList<V>();
+ private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
public SoftCache() {
this(0);
@@ -25,9 +25,10 @@ public class SoftCache extends AbstractMap { this.minSize = minSize;
}
- public Object get(Object key) {
- Object result = null;
- SoftReference soft_ref = (SoftReference) hash.get(key);
+ @Override
+ public V get(Object key) {
+ V result = null;
+ SoftValue<V> soft_ref = hash.get(key);
if (soft_ref != null) {
result = soft_ref.get();
if (result == null)
@@ -42,19 +43,20 @@ public class SoftCache extends AbstractMap { return result;
}
- public Object put(Object key, Object value) {
+ public V put(K key, V value) {
processQueue();
if (minSize > 0) {
hardCache.addFirst(value);
if (hardCache.size() > minSize)
hardCache.removeLast();
}
- return hash.put(key, new SoftValue(value, key, queue));
+ hash.put(key, new SoftValue<V>(key, value, queue));
+ return value;
}
- public Object remove(Object key) {
+ public V remove(Object key) {
processQueue();
- return hash.remove(key);
+ return hash.remove(key).get();
}
public void clear() {
@@ -68,21 +70,21 @@ public class SoftCache extends AbstractMap { return hash.size();
}
- public Set keySet() {
+ public Set<K> keySet() {
processQueue();
return hash.keySet();
}
- public Set entrySet() {
+ public Set<Map.Entry<K, V>> entrySet() {
// Would have to create another Map to do this - too expensive
// Throwing runtime expensive is dangerous, but better than nulls
throw new UnsupportedOperationException();
}
- private static class SoftValue extends SoftReference {
+ private static class SoftValue<V> extends SoftReference<V> {
private final Object key;
- private SoftValue(Object k, Object key, ReferenceQueue q) {
- super(k, q);
+ private SoftValue(Object key, V value, ReferenceQueue<V> q) {
+ super(value, q);
this.key = key;
}
}
|
