[
https://issues.apache.org/jira/browse/COLLECTIONS-467?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13651869#comment-13651869
]
l0co commented on COLLECTIONS-467:
----------------------------------
Suggested implementation with example usage:
{code}
public class LRUMap extends org.apache.commons.collections.map.LRUMap {
public LRUMap() {
super();
}
public LRUMap(int maxSize) {
super(maxSize);
}
public LRUMap(int maxSize, boolean scanUntilRemovable) {
super(maxSize, scanUntilRemovable);
}
public LRUMap(int maxSize, float loadFactor) {
super(maxSize, loadFactor);
}
public LRUMap(int maxSize, float loadFactor, boolean
scanUntilRemovable) {
super(maxSize, loadFactor, scanUntilRemovable);
}
public LRUMap(Map map) {
super(map);
}
public LRUMap(Map map, boolean scanUntilRemovable) {
super(map, scanUntilRemovable);
}
@Override
protected void removeEntry(HashEntry entry, int hashIndex, HashEntry
previous) {
onRemove(entry.getKey(), entry.getValue());
super.removeEntry(entry, hashIndex, previous);
}
@Override
protected void addEntry(HashEntry entry, int hashIndex) {
onAdd(entry.getKey(), entry.getValue());
super.addEntry(entry, hashIndex);
}
@Override
public void clear() {
for (MapIterator i=mapIterator(); i.hasNext(); ) {
i.next();
onRemove(i.getKey(), i.getValue());
}
super.clear();
}
@Override
public Object get(Object key) {
return super.get(key);
}
protected void onAdd(Object key, Object value) {
// to override
}
protected void onRemove(Object key, Object value) {
// to override
}
// test
public static void main(String [] args) {
LRUMap map = new LRUMap(5) {
@Override
protected void onRemove(Object key, Object value) {
System.out.println("remove: "+key);
}
@Override
protected void onAdd(Object key, Object value) {
System.out.println("add: "+key);
}
};
for (int i=1; i<=10; i++)
map.put(i, i);
map.clear();
}
}
{code}
> LRUMap remove callback
> ----------------------
>
> Key: COLLECTIONS-467
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-467
> Project: Commons Collections
> Issue Type: New Feature
> Affects Versions: 3.2
> Reporter: l0co
> Priority: Minor
>
> If you use LRUMap with objects that require doing some disposal when they are
> dropped (eg. close()) and you hold these objects only in LRUMap, you cannot
> do it with current implementation. I propose to add onRemove() and onAdd()
> methods to the implementation, so that you can create anonymous inherited
> class and be able to react to these events.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira