Package adams.core
Class LRUCache<K,V>
- java.lang.Object
-
- adams.core.LRUCache<K,V>
-
- All Implemented Interfaces:
CloneHandler<LRUCache<K,V>>
,Serializable
public class LRUCache<K,V> extends Object implements Serializable, CloneHandler<LRUCache<K,V>>
An LRU cache, based onLinkedHashMap
.
This cache has a fixed maximum number of elements (cacheSize
). If the cache is full and another entry is added, the LRU (least recently used) entry is dropped.
This class is thread-safe. All methods of this class are synchronized.
License: LGPL.- Version:
- $Revision$
- Author:
- Christian d'Heureuse (www.source-code.biz), FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
LRUCache.LRUCacheLinkedHashMap
The class that does the actual caching.
-
Field Summary
Fields Modifier and Type Field Description static float
HASHTABLE_LOAD_FACTOR
the load factor.protected int
m_CacheSize
the cache size.protected boolean
m_Enabled
whether the cache is enabled.protected LinkedHashMap<K,V>
m_Map
the cache.
-
Constructor Summary
Constructors Constructor Description LRUCache(int cacheSize)
Creates a new LRU cache.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Clears the cache.boolean
contains(K key)
Checks whether a given key is already stored in the cache.V
get(K key)
Retrieves an entry from the cache.
The retrieved entry becomes the MRU (most recently used) entry.Collection<Map.Entry<K,V>>
getAll()
Returns aCollection
that contains a copy of all cache entries.LRUCache<K,V>
getClone()
Returns a clone of the object.boolean
isEnabled()
Returns whether the cache is enabled.Set<K>
keySet()
Returns aSet
view of the keys contained in this map.static void
main(String[] args)
Test routine for the LRUCache class.void
put(K key, V value)
Adds an entry to this cache (if enabled).V
remove(K key)
Removes the entry from this cache (if enabled).void
resize(int cacheSize)
Resizes the caches (disables, empties and re-enables the cache).void
setEnabled(boolean value)
Sets the enabled state of the cache.int
size()
Returns the cache size.int
sizeUsed()
Returns the number of used entries in the cache.
-
-
-
Field Detail
-
HASHTABLE_LOAD_FACTOR
public static final float HASHTABLE_LOAD_FACTOR
the load factor.- See Also:
- Constant Field Values
-
m_Map
protected LinkedHashMap<K,V> m_Map
the cache.
-
m_CacheSize
protected int m_CacheSize
the cache size.
-
m_Enabled
protected boolean m_Enabled
whether the cache is enabled.
-
-
Method Detail
-
isEnabled
public boolean isEnabled()
Returns whether the cache is enabled.- Returns:
- true if the cache is enabled
-
setEnabled
public void setEnabled(boolean value)
Sets the enabled state of the cache. Enabling the cache is ignored if the cache size is 0.- Parameters:
value
- if true then the cache is activated
-
size
public int size()
Returns the cache size.- Returns:
- the size of the cache.
-
sizeUsed
public int sizeUsed()
Returns the number of used entries in the cache.- Returns:
- the number of entries currently in the cache.
-
resize
public void resize(int cacheSize)
Resizes the caches (disables, empties and re-enables the cache). If the cache size is 0 then the cache won't get enabled.- Parameters:
cacheSize
- the new siuze of the cache
-
contains
public boolean contains(K key)
Checks whether a given key is already stored in the cache.- Parameters:
key
- the key to look for- Returns:
- true if a value is already stored under this key
-
get
public V get(K key)
Retrieves an entry from the cache.
The retrieved entry becomes the MRU (most recently used) entry.- Parameters:
key
- the key whose associated value is to be returned.- Returns:
- the value associated to this key, or null if no value with this key exists in the cache.
-
put
public void put(K key, V value)
Adds an entry to this cache (if enabled). If the cache is full, the LRU (least recently used) entry is dropped.- Parameters:
key
- the key with which the specified value is to be associated.value
- a value to be associated with the specified key.
-
remove
public V remove(K key)
Removes the entry from this cache (if enabled).- Parameters:
key
- the key of the value to remove from the cachereturn
- the previously with the key associated value, or null if not in cache or cache not enabled
-
clear
public void clear()
Clears the cache.
-
getAll
public Collection<Map.Entry<K,V>> getAll()
Returns aCollection
that contains a copy of all cache entries.- Returns:
- a
Collection
with a copy of the cache content.
-
keySet
public Set<K> keySet()
Returns aSet
view of the keys contained in this map.- Returns:
- the set of keys
-
getClone
public LRUCache<K,V> getClone()
Returns a clone of the object.- Specified by:
getClone
in interfaceCloneHandler<K>
- Returns:
- the clone
-
main
public static void main(String[] args)
Test routine for the LRUCache class.- Parameters:
args
- ignored
-
-