Allow a value in an LRU hash map to be notified when it gets the boot.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@1685 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -31,6 +31,17 @@ public class LRUHashMap implements Map
|
|||||||
public int computeSize (Object item);
|
public int computeSize (Object item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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).
|
||||||
|
*/
|
||||||
|
public static interface LRUItem
|
||||||
|
{
|
||||||
|
/** Informs this item that it was removed from the map. */
|
||||||
|
public void removedFromMap (LRUHashMap map);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a LRUHashMap with the specified maximum size. All items
|
* Construct a LRUHashMap with the specified maximum size. All items
|
||||||
* in the cache will be considered to have a size of one.
|
* in the cache will be considered to have a size of one.
|
||||||
@@ -170,10 +181,13 @@ public class LRUHashMap implements Map
|
|||||||
_seenKeys.add(key);
|
_seenKeys.add(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// updated our computed "size"
|
// avoid fruitless NOOPs
|
||||||
_size += _sizer.computeSize(value);
|
if (result != value) {
|
||||||
entryRemoved(result);
|
// updated our computed "size"
|
||||||
|
_size += _sizer.computeSize(value);
|
||||||
|
entryRemoved(result);
|
||||||
// System.out.println("Added " + value + ": " + _size);
|
// System.out.println("Added " + value + ": " + _size);
|
||||||
|
}
|
||||||
|
|
||||||
// flush if needed
|
// flush if needed
|
||||||
flush();
|
flush();
|
||||||
@@ -199,7 +213,7 @@ public class LRUHashMap implements Map
|
|||||||
Iterator iter = _delegate.entrySet().iterator();
|
Iterator iter = _delegate.entrySet().iterator();
|
||||||
// don't remove the last entry, even if it's too big, because
|
// don't remove the last entry, even if it's too big, because
|
||||||
// a cache with nothing in it sucks
|
// a cache with nothing in it sucks
|
||||||
for (int ii=size(); (ii > 1) && (_size > _maxSize); ii--) {
|
for (int ii = size(); (ii > 1) && (_size > _maxSize); ii--) {
|
||||||
Map.Entry entry = (Map.Entry) iter.next();
|
Map.Entry entry = (Map.Entry) iter.next();
|
||||||
entryRemoved(entry.getValue());
|
entryRemoved(entry.getValue());
|
||||||
iter.remove();
|
iter.remove();
|
||||||
@@ -214,6 +228,9 @@ public class LRUHashMap implements Map
|
|||||||
{
|
{
|
||||||
if (entry != null) {
|
if (entry != null) {
|
||||||
_size -= _sizer.computeSize(entry);
|
_size -= _sizer.computeSize(entry);
|
||||||
|
if (entry instanceof LRUItem) {
|
||||||
|
((LRUItem)entry).removedFromMap(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user