diff options
Diffstat (limited to 'source/com/c2kernel/utils/SoftCache.java')
| -rw-r--r-- | source/com/c2kernel/utils/SoftCache.java | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/source/com/c2kernel/utils/SoftCache.java b/source/com/c2kernel/utils/SoftCache.java index 6680f30..9c95f2e 100644 --- a/source/com/c2kernel/utils/SoftCache.java +++ b/source/com/c2kernel/utils/SoftCache.java @@ -2,12 +2,16 @@ package com.c2kernel.utils; import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
-import java.util.*;
+import java.util.AbstractMap;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Set;
/*******************************************************************************
* SoftReferences are reaped if no strong references are left and the vm is
* running out of memory. Most caches in the kernel use this.
- *
+ *
* $Revision: 1.5 $ $Date: 2004/10/29 13:29:09 $
******************************************************************************/
public class SoftCache<K, V> extends AbstractMap<K, V> {
@@ -43,7 +47,8 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { return result;
}
- public V put(K key, V value) {
+ @Override
+ public V put(K key, V value) {
processQueue();
if (minSize > 0) {
hardCache.addFirst(value);
@@ -54,33 +59,38 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { return value;
}
- public V remove(Object key) {
+ @Override
+ public V remove(Object key) {
processQueue();
return hash.remove(key).get();
}
- public void clear() {
+ @Override
+ public void clear() {
hardCache.clear();
while(queue.poll()!=null);
hash.clear();
}
- public int size() {
+ @Override
+ public int size() {
processQueue();
return hash.size();
}
-
- public Set<K> keySet() {
+
+ @Override
+ public Set<K> keySet() {
processQueue();
return hash.keySet();
}
- public Set<Map.Entry<K, V>> entrySet() {
+ @Override
+ 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<V> extends SoftReference<V> {
private final Object key;
private SoftValue(Object key, V value, ReferenceQueue<V> q) {
@@ -88,15 +98,15 @@ public class SoftCache<K, V> extends AbstractMap<K, V> { this.key = key;
}
}
-
+
/**
* Look for values that have been reaped, and remove their keys from the cache
*/
private void processQueue() {
- SoftValue sv;
- while ((sv = (SoftValue) queue.poll()) != null) {
+ SoftValue<?> sv;
+ while ((sv = (SoftValue<?>) queue.poll()) != null) {
hash.remove(sv.key);
}
}
-
+
}
|
