Class AbstractLRUMap<K,V>

java.lang.Object
org.apache.commons.jcs3.utils.struct.AbstractLRUMap<K,V>
All Implemented Interfaces:
Map<K,V>
Direct Known Subclasses:
BlockDiskKeyStore.LRUMapSizeLimited, IndexedDiskCache.LRUMapSizeLimited, LRUMap

public abstract class AbstractLRUMap<K,V> extends Object implements Map<K,V>
This is a simple LRUMap. It implements most of the map methods. It is not recommended that you use any but put, get, remove, and clear.

Children can implement the processRemovedLRU method if they want to handle the removal of the least recently used item.

This class was abstracted out of the LRU Memory cache. Put, remove, and get should be thread safe. It uses a hashtable and our own double linked list.

Locking is done on the instance.

  • Constructor Details

    • AbstractLRUMap

      public AbstractLRUMap()
      This creates an unbounded version. Setting the max objects will result in spooling on subsequent puts.
  • Method Details

    • size

      public int size()
      This simply returns the number of elements in the map.

      Specified by:
      size in interface Map<K,V>
      See Also:
    • clear

      public void clear()
      This removes all the items. It clears the map and the double linked list.

      Specified by:
      clear in interface Map<K,V>
      See Also:
    • isEmpty

      public boolean isEmpty()
      Returns true if the map is empty.

      Specified by:
      isEmpty in interface Map<K,V>
      See Also:
    • containsKey

      public boolean containsKey(Object key)
      Returns true if the map contains an element for the supplied key.

      Specified by:
      containsKey in interface Map<K,V>
      See Also:
    • containsValue

      public boolean containsValue(Object value)
      This is an expensive operation that determines if the object supplied is mapped to any key.

      Specified by:
      containsValue in interface Map<K,V>
      See Also:
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
      Returns:
      map.values();
    • putAll

      public void putAll(Map<? extends K,? extends V> source)
      Specified by:
      putAll in interface Map<K,V>
      Parameters:
      source -
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Parameters:
      key -
      Returns:
      Object
    • getQuiet

      public V getQuiet(Object key)
      This gets an element out of the map without adjusting it's position in the LRU. In other words, this does not count as being used. If the element is the last item in the list, it will still be the last time in the list.

      Parameters:
      key -
      Returns:
      Object
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
      Parameters:
      key -
      Returns:
      Object removed
    • put

      public V put(K key, V value)
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key -
      value -
      Returns:
      Object
    • shouldRemove

      protected abstract boolean shouldRemove()
    • dumpCacheEntries

      public void dumpCacheEntries()
      Dump the cache entries from first to list for debugging.
    • dumpMap

      public void dumpMap()
      Dump the cache map for debugging.
    • verifyCache

      protected void verifyCache()
      Checks to see if all the items that should be in the cache are. Checks consistency between List and map.
    • processRemovedLRU

      protected void processRemovedLRU(K key, V value)
      This is called when an item is removed from the LRU. We just log some information.

      Children can implement this method for special behavior.

      Parameters:
      key -
      value -
    • getStatistics

      Returns:
      IStats
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      This returns a set of entries. Our LRUMapEntry is used since the value stored in the underlying map is a node in the double linked list. We wouldn't want to return this to the client, so we construct a new entry with the payload of the node.

      TODO we should return out own set wrapper, so we can avoid the extra object creation if it isn't necessary.

      Specified by:
      entrySet in interface Map<K,V>
      See Also:
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Returns:
      map.keySet();