Package adams.core

Class 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 on LinkedHashMap.
    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
    • Field Detail

      • HASHTABLE_LOAD_FACTOR

        public static final float HASHTABLE_LOAD_FACTOR
        the load factor.
        See Also:
        Constant Field Values
      • m_CacheSize

        protected int m_CacheSize
        the cache size.
      • m_Enabled

        protected boolean m_Enabled
        whether the cache is enabled.
    • Constructor Detail

      • LRUCache

        public LRUCache​(int cacheSize)
        Creates a new LRU cache.
        Parameters:
        cacheSize - the maximum number of entries that will be kept in this cache.
    • 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
        return - 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 a Collection that contains a copy of all cache entries.
        Returns:
        a Collection with a copy of the cache content.
      • keySet

        public Set<K> keySet()
        Returns a Set view of the keys contained in this map.
        Returns:
        the set of keys
      • main

        public static void main​(String[] args)
        Test routine for the LRUCache class.
        Parameters:
        args - ignored