Switched to different structure for reporting item removal per Ray's

suggestion. This allows proper handling of value classes that cannot be
made to implement an interface or extended or otherwise fiddled with.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@1687 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2005-08-05 20:23:37 +00:00
parent 809df481c1
commit aa74625b0a
@@ -32,14 +32,14 @@ public class LRUHashMap implements Map
}
/**
* Values in the LRU hash map that implement this interface will be
* notified when they are removed from the table (either explicitly or
* by being replaced with another value or due to being flushed).
* An observer may be registered with a LRU hash map to be notified
* when items are removed from the table (either explicitly or by
* being replaced with another value or due to being flushed).
*/
public static interface LRUItem
public static interface RemovalObserver
{
/** Informs this item that it was removed from the map. */
public void removedFromMap (LRUHashMap map);
/** Informs the observer that this item was removed from the map. */
public void removedFromMap (LRUHashMap map, Object item);
}
/**
@@ -85,6 +85,14 @@ public class LRUHashMap implements Map
return _maxSize;
}
/**
* Configures this hash map with a removal observer.
*/
public void setRemovalObserver (RemovalObserver obs)
{
_remobs = obs;
}
/**
* Used to temporarily disable flushing elements from the
* cache. Generally this is only used to avoid undesired garbage
@@ -228,8 +236,8 @@ public class LRUHashMap implements Map
{
if (entry != null) {
_size -= _sizer.computeSize(entry);
if (entry instanceof LRUItem) {
((LRUItem)entry).removedFromMap(this);
if (_remobs != null) {
_remobs.removedFromMap(this, entry);
}
}
}
@@ -254,11 +262,11 @@ public class LRUHashMap implements Map
// documentation inherited from interface
public void clear ()
{
// notify all values of their removal
for (Iterator iter = _delegate.values().iterator(); iter.hasNext(); ) {
Object value = iter.next();
if (value instanceof LRUItem) {
((LRUItem)value).removedFromMap(this);
// notify our removal observer if we have one
if (_remobs != null) {
for (Iterator iter = _delegate.values().iterator();
iter.hasNext(); ) {
_remobs.removedFromMap(this, iter.next());
}
}
@@ -316,8 +324,12 @@ public class LRUHashMap implements Map
/** Used to temporarily disable flushing. */
protected boolean _canFlush = true;
/** Notified when items are removed from the map, if non-null. */
protected RemovalObserver _remobs;
/** Used to compute the size of items in this cache. */
protected ItemSizer _sizer;
/** Tracking info. */
protected boolean _tracking;
protected HashSet _seenKeys;