groovyx.gpars.memoize
Class LRUProtectionStorage

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by java.util.LinkedHashMap<java.lang.Object,java.lang.Object>
              extended by groovyx.gpars.memoize.LRUProtectionStorage
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.Object,java.lang.Object>

public final class LRUProtectionStorage
extends java.util.LinkedHashMap<java.lang.Object,java.lang.Object>

Protects stored resources from eviction from memory following the LRU (Last Recently Used) strategy. If the maximum size has been reached all newly added elements will cause the oldest element to be removed from the storage in order not to exceed the maximum capacity. The touch method can be used to renew an element and move it to the from the LRU queue.

Author:
Vaclav Pech Date: Jun 22, 2010
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
private  int maxSize
           
private static long serialVersionUID
           
 
Constructor Summary
LRUProtectionStorage(int maxSize)
           
 
Method Summary
 java.lang.Object clone()
          Performs a shallow clone
 java.lang.Object get(java.lang.Object key)
           
protected  boolean removeEldestEntry(java.util.Map.Entry<java.lang.Object,java.lang.Object> eldest)
           
 void touch(java.lang.Object key, java.lang.Object value)
          The touch method can be used to renew an element and move it to the from of the LRU queue.
 
Methods inherited from class java.util.LinkedHashMap
clear, containsValue
 
Methods inherited from class java.util.HashMap
containsKey, entrySet, isEmpty, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsKey, entrySet, equals, hashCode, isEmpty, keySet, put, putAll, remove, size, values
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

maxSize

private final int maxSize
Constructor Detail

LRUProtectionStorage

public LRUProtectionStorage(int maxSize)
Method Detail

removeEldestEntry

protected boolean removeEldestEntry(java.util.Map.Entry<java.lang.Object,java.lang.Object> eldest)
Overrides:
removeEldestEntry in class java.util.LinkedHashMap<java.lang.Object,java.lang.Object>

touch

public void touch(java.lang.Object key,
                  java.lang.Object value)
The touch method can be used to renew an element and move it to the from of the LRU queue.

Parameters:
key - The key of the element to renew
value - A value to newly associate with the key

get

public java.lang.Object get(java.lang.Object key)
Specified by:
get in interface java.util.Map<java.lang.Object,java.lang.Object>
Overrides:
get in class java.util.LinkedHashMap<java.lang.Object,java.lang.Object>

clone

public java.lang.Object clone()
Performs a shallow clone

Overrides:
clone in class java.util.HashMap<java.lang.Object,java.lang.Object>
Returns:
The cloned instance

Copyright © 2008–2010 Václav Pech. All Rights Reserved.