From 845a2177dbabb1fce23183c9d6fffa2c44456758 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Fri, 16 Apr 2004 23:53:59 +0000 Subject: [PATCH] LRUMap - The removeLRU() method was passed the wrong LinkEntry [28433] git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131669 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE-NOTES.html | 1 + .../commons/collections/map/LRUMap.java | 4 +- .../commons/collections/map/TestLRUMap.java | 59 ++++++++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html index f704a826f..2ef072c1c 100644 --- a/RELEASE-NOTES.html +++ b/RELEASE-NOTES.html @@ -65,6 +65,7 @@ No interface changes, or deprecations have occurred.
  • AbstractHashedMap subclasses failed to clone() correctly [27159]
  • ExtendedProperties - Close input stream in constructor [27737]
  • Flat3Map - Handle infinite loops in toString
  • +
  • LRUMap - The removeLRU() method was passed the wrong LinkEntry [28433]
  • JAVADOC

    diff --git a/src/java/org/apache/commons/collections/map/LRUMap.java b/src/java/org/apache/commons/collections/map/LRUMap.java index 254a56303..a5f169dcd 100644 --- a/src/java/org/apache/commons/collections/map/LRUMap.java +++ b/src/java/org/apache/commons/collections/map/LRUMap.java @@ -41,7 +41,7 @@ import org.apache.commons.collections.BoundedMap; * ResettableIterator and calling reset(). * * @since Commons Collections 3.0 (previously in main package v1.0) - * @version $Revision: 1.9 $ $Date: 2004/02/18 01:13:19 $ + * @version $Revision: 1.10 $ $Date: 2004/04/16 23:53:59 $ * * @author James Strachan * @author Morgan Delagrange @@ -169,7 +169,7 @@ public class LRUMap * @param value the value to add */ protected void addMapping(int hashIndex, int hashCode, Object key, Object value) { - if (size >= maxSize && removeLRU(header.before)) { + if (size >= maxSize && removeLRU(header.after)) { reuseMapping(header.after, hashIndex, hashCode, key, value); } else { super.addMapping(hashIndex, hashCode, key, value); diff --git a/src/test/org/apache/commons/collections/map/TestLRUMap.java b/src/test/org/apache/commons/collections/map/TestLRUMap.java index 44c971546..421afde5b 100644 --- a/src/test/org/apache/commons/collections/map/TestLRUMap.java +++ b/src/test/org/apache/commons/collections/map/TestLRUMap.java @@ -30,7 +30,7 @@ import org.apache.commons.collections.ResettableIterator; /** * JUnit tests. * - * @version $Revision: 1.6 $ $Date: 2004/02/27 00:25:14 $ + * @version $Revision: 1.7 $ $Date: 2004/04/16 23:53:59 $ * * @author Stephen Colebourne */ @@ -218,6 +218,63 @@ public class TestLRUMap extends AbstractTestOrderedMap { assertSame(map.get("1"), cloned.get("1")); } + public void testRemoveLRU() { + MockLRUMapSubclass map = new MockLRUMapSubclass(2); + assertNull(map.entry); + map.put("A", "a"); + assertNull(map.entry); + map.put("B", "b"); + assertNull(map.entry); + map.put("C", "c"); // removes oldest, which is A=a + assertNotNull(map.entry); + assertEquals("A", map.key); + assertEquals("a", map.value); + assertEquals("C", map.entry.getKey()); // entry is reused + assertEquals("c", map.entry.getValue()); // entry is reused + assertEquals(false, map.containsKey("A")); + assertEquals(true, map.containsKey("B")); + assertEquals(true, map.containsKey("C")); + } + + static class MockLRUMapSubclass extends LRUMap { + LinkEntry entry; + Object key; + Object value; + MockLRUMapSubclass(int size) { + super(size); + } + protected boolean removeLRU(LinkEntry entry) { + this.entry = entry; + this.key = entry.getKey(); + this.value = entry.getValue(); + return true; + } + } + + public void testRemoveLRUBlocksRemove() { + MockLRUMapSubclassBlocksRemove map = new MockLRUMapSubclassBlocksRemove(2); + assertEquals(0, map.size()); + map.put("A", "a"); + assertEquals(1, map.size()); + map.put("B", "b"); + assertEquals(2, map.size()); + map.put("C", "c"); // should remove oldest, which is A=a, but this is blocked + assertEquals(3, map.size()); + assertEquals(2, map.maxSize()); + assertEquals(true, map.containsKey("A")); + assertEquals(true, map.containsKey("B")); + assertEquals(true, map.containsKey("C")); + } + + static class MockLRUMapSubclassBlocksRemove extends LRUMap { + MockLRUMapSubclassBlocksRemove(int size) { + super(size); + } + protected boolean removeLRU(LinkEntry entry) { + return false; + } + } + // public void testCreate() throws Exception { // resetEmpty(); // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/LRUMap.emptyCollection.version3.obj");