diff options
| author | abranson <andrew.branson@cern.ch> | 2011-08-04 00:42:34 +0200 |
|---|---|---|
| committer | abranson <andrew.branson@cern.ch> | 2011-08-04 00:42:34 +0200 |
| commit | 0ec8481c10cd8277d84c7c1a785483a0a739e5a0 (patch) | |
| tree | 5f6e5d9ae75193e67e6f3b3dfa488960c5cde1d5 /source/com/c2kernel/utils/SoftCache.java | |
| parent | 036cbdba66f804743c4c838ed598d6972c4b3e17 (diff) | |
More code cleanup:
Refactored Entity Proxy Subscription to handle generics better
Rewrote RemoteMap to use TreeMap instead of the internal array for
order. It now sorts its keys by number if they parse, else as strings.
Removed a no-longer-in-progress outcome form class
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);
}
}
-
+
}
|
