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.- 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 classLRUCache.LRUCacheLinkedHashMapThe class that does the actual caching.
-
Field Summary
Fields Modifier and Type Field Description static floatHASHTABLE_LOAD_FACTORthe load factor.protected intm_CacheSizethe cache size.protected booleanm_Enabledwhether the cache is enabled.protected LinkedHashMap<K,V>m_Mapthe 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 voidclear()Clears the cache.booleancontains(K key)Checks whether a given key is already stored in the cache.Vget(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 aCollectionthat contains a copy of all cache entries.LRUCache<K,V>getClone()Returns a clone of the object.booleanisEnabled()Returns whether the cache is enabled.Set<K>keySet()Returns aSetview of the keys contained in this map.static voidmain(String[] args)Test routine for the LRUCache class.voidput(K key, V value)Adds an entry to this cache (if enabled).voidputAll(LRUCache<K,V> cache)Caches the elements from the provided map.voidputAll(Map<K,V> map)Caches the elements from the provided map.Vremove(K key)Removes the entry from this cache (if enabled).voidresize(int cacheSize)Resizes the caches (disables, empties and re-enables the cache).voidsetEnabled(boolean value)Sets the enabled state of the cache.intsize()Returns the cache size.intsizeUsed()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 cache- Returns:
- the value previously associated with the key, 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 aCollectionthat contains a copy of all cache entries.- Returns:
- a
Collectionwith a copy of the cache content.
-
putAll
public void putAll(Map<K,V> map)
Caches the elements from the provided map.- Parameters:
map- the map to cache
-
putAll
public void putAll(LRUCache<K,V> cache)
Caches the elements from the provided map.- Parameters:
cache- the map to cache
-
keySet
public Set<K> keySet()
Returns aSetview 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:
getClonein interfaceCloneHandler<K>- Returns:
- the clone
-
main
public static void main(String[] args)
Test routine for the LRUCache class.- Parameters:
args- ignored
-
-