From 0ec8481c10cd8277d84c7c1a785483a0a739e5a0 Mon Sep 17 00:00:00 2001 From: abranson Date: Thu, 4 Aug 2011 00:42:34 +0200 Subject: 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 --- source/com/c2kernel/utils/SoftCache.java | 40 ++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'source/com/c2kernel/utils/SoftCache.java') 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 extends AbstractMap { @@ -43,7 +47,8 @@ public class SoftCache extends AbstractMap { 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 extends AbstractMap { 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 keySet() { + + @Override + public Set keySet() { processQueue(); return hash.keySet(); } - public Set> entrySet() { + @Override + public Set> 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 final Object key; private SoftValue(Object key, V value, ReferenceQueue q) { @@ -88,15 +98,15 @@ public class SoftCache extends AbstractMap { 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); } } - + } -- cgit v1.2.3