You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2015/04/07 14:25:18 UTC
svn commit: r1671832 - in /commons/proper/collections/trunk/src:
changes/changes.xml main/java/org/apache/commons/collections4/map/LRUMap.java
test/java/org/apache/commons/collections4/map/LRUMapTest.java
Author: tn
Date: Tue Apr 7 12:25:18 2015
New Revision: 1671832
URL: http://svn.apache.org/r1671832
Log:
[COLLECTIONS-395] Added LRUMap#get(Object, boolean) to query the map without affecting the lru order. Thanks to David Hawthorne.
Modified:
commons/proper/collections/trunk/src/changes/changes.xml
commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java
commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java
Modified: commons/proper/collections/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/changes/changes.xml?rev=1671832&r1=1671831&r2=1671832&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/changes/changes.xml (original)
+++ commons/proper/collections/trunk/src/changes/changes.xml Tue Apr 7 12:25:18 2015
@@ -22,6 +22,10 @@
<body>
<release version="4.1" date="TBD" description="">
+ <action issue="COLLECTIONS-395" dev="tn" type="add" due-to="David Hawthorne">
+ Added method "LRUMap#get(Object, boolean)" that allows to query the map
+ without affecting the least recently used order.
+ </action>
<action issue="COLLECTIONS-558" dev="tn" type="fix" due-to="Felix Rabe">
Changed return type of "ListOrderedSet#remove(int)" from Object to the generic type parameter.
</action>
Modified: commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java?rev=1671832&r1=1671831&r2=1671832&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java (original)
+++ commons/proper/collections/trunk/src/main/java/org/apache/commons/collections4/map/LRUMap.java Tue Apr 7 12:25:18 2015
@@ -116,7 +116,7 @@ public class LRUMap<K, V>
* Constructs a new, empty map with the specified initial capacity and
* load factor.
*
- * @param maxSize the maximum size of the ma
+ * @param maxSize the maximum size of the map
* @param loadFactor the load factor
* @param scanUntilRemovable scan until a removeable entry is found, default false
* @throws IllegalArgumentException if the maximum size is less than one
@@ -166,18 +166,36 @@ public class LRUMap<K, V>
* Gets the value mapped to the key specified.
* <p>
* This operation changes the position of the key in the map to the
- * most recently used position (first).
+ * most recently used position (last).
*
* @param key the key
* @return the mapped value, null if no match
*/
@Override
public V get(final Object key) {
+ return get(key, true);
+ }
+
+ /**
+ * Gets the value mapped to the key specified.
+ * <p>
+ * If {@code updateToMRU} is {@code true}, the position of the key in the map
+ * is changed to the most recently used position (last), otherwise the iteration
+ * order is not changed by this operation.
+ *
+ * @param key the key
+ * @param updateToMRU whether the key shall be updated to the
+ * most recently used position
+ * @return the mapped value, null if no match
+ */
+ public V get(final Object key, final boolean updateToMRU) {
final LinkEntry<K, V> entry = getEntry(key);
if (entry == null) {
return null;
}
- moveToMRU(entry);
+ if (updateToMRU) {
+ moveToMRU(entry);
+ }
return entry.getValue();
}
@@ -214,7 +232,7 @@ public class LRUMap<K, V>
/**
* Updates an existing key-value mapping.
* <p>
- * This implementation moves the updated entry to the top of the list
+ * This implementation moves the updated entry to the end of the list
* using {@link #moveToMRU(AbstractLinkedMap.LinkEntry)}.
*
* @param entry the entry to update
Modified: commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java?rev=1671832&r1=1671831&r2=1671832&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java (original)
+++ commons/proper/collections/trunk/src/test/java/org/apache/commons/collections4/map/LRUMapTest.java Tue Apr 7 12:25:18 2015
@@ -224,6 +224,55 @@ public class LRUMapTest<K, V> extends Ab
assertSame(values[3], vit.next());
}
+ public void testAccessOrder2() {
+ if (!isPutAddSupported() || !isPutChangeSupported()) {
+ return;
+ }
+ final K[] keys = getSampleKeys();
+ final V[] values = getSampleValues();
+ Iterator<K> kit = null;
+ Iterator<V> vit = null;
+
+ resetEmpty();
+ LRUMap<K, V> lruMap = (LRUMap<K, V>) map;
+
+ lruMap.put(keys[0], values[0]);
+ lruMap.put(keys[1], values[1]);
+ kit = lruMap.keySet().iterator();
+ assertSame(keys[0], kit.next());
+ assertSame(keys[1], kit.next());
+ vit = lruMap.values().iterator();
+ assertSame(values[0], vit.next());
+ assertSame(values[1], vit.next());
+
+ // no change to order
+ lruMap.put(keys[1], values[1]);
+ kit = lruMap.keySet().iterator();
+ assertSame(keys[0], kit.next());
+ assertSame(keys[1], kit.next());
+ vit = lruMap.values().iterator();
+ assertSame(values[0], vit.next());
+ assertSame(values[1], vit.next());
+
+ // no change to order
+ lruMap.get(keys[1], false);
+ kit = lruMap.keySet().iterator();
+ assertSame(keys[0], kit.next());
+ assertSame(keys[1], kit.next());
+ vit = lruMap.values().iterator();
+ assertSame(values[0], vit.next());
+ assertSame(values[1], vit.next());
+
+ // change to order
+ lruMap.get(keys[0], true);
+ kit = lruMap.keySet().iterator();
+ assertSame(keys[1], kit.next());
+ assertSame(keys[0], kit.next());
+ vit = lruMap.values().iterator();
+ assertSame(values[1], vit.next());
+ assertSame(values[0], vit.next());
+ }
+
@SuppressWarnings("unchecked")
public void testClone() {
final LRUMap<K, V> map = new LRUMap<K, V>(10);