[COLLECTIONS-395] Added LRUMap#get(Object, boolean) to query the map without affecting the lru order. Thanks to David Hawthorne.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1671832 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
640670bd86
commit
0f4a98cfe1
|
@ -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>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -224,6 +224,55 @@ public class LRUMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue