Class LruCache<K,V>

java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
java.util.LinkedHashMap<K,V>
org.jxmpp.util.cache.LruCache<K,V>
Type Parameters:
K - the type of the keys of this cache.
V - the type of the values this cache caches.
All Implemented Interfaces:
Serializable, Cloneable, Map<K,V>, Cache<K,V>

public class LruCache<K,V> extends LinkedHashMap<K,V> implements Cache<K,V>
A specialized Map that is size-limited (using an LRU algorithm). The Map is thread-safe.
See Also:
  • Constructor Details

    • LruCache

      public LruCache(int maxSize)
      Create a new cache and specify the maximum size of for the cache in bytes, and the maximum lifetime of objects.
      Parameters:
      maxSize - the maximum number of objects the cache will hold. -1 means the cache has no max size.
  • Method Details

    • removeEldestEntry

      protected final boolean removeEldestEntry(Map.Entry<K,V> eldest)
      Overrides:
      removeEldestEntry in class LinkedHashMap<K,V>
    • put

      public final V put(K key, V value)
      Description copied from interface: Cache
      Put a value in the cache.
      Specified by:
      put in interface Cache<K,V>
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class HashMap<K,V>
      Parameters:
      key - the key of the value.
      value - the value.
      Returns:
      the previous value or null.
    • lookup

      public final V lookup(K key)
      Description copied from interface: Cache
      Returns the value of the specified key, or null.
      Specified by:
      lookup in interface Cache<K,V>
      Parameters:
      key - the key.
      Returns:
      the value found in the cache, or null.
    • get

      public final V get(Object key)
      Specified by:
      get in interface Map<K,V>
      Overrides:
      get in class LinkedHashMap<K,V>
    • remove

      public final V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
      Overrides:
      remove in class HashMap<K,V>
    • clear

      public final void clear()
      Specified by:
      clear in interface Map<K,V>
      Overrides:
      clear in class LinkedHashMap<K,V>
    • size

      public final int size()
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class HashMap<K,V>
    • isEmpty

      public final boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
      Overrides:
      isEmpty in class HashMap<K,V>
    • values

      public final Collection<V> values()
      Specified by:
      values in interface Map<K,V>
      Overrides:
      values in class LinkedHashMap<K,V>
    • containsKey

      public final boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
      Overrides:
      containsKey in class HashMap<K,V>
    • putAll

      public final void putAll(Map<? extends K,? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
      Overrides:
      putAll in class HashMap<K,V>
    • containsValue

      public final boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
      Overrides:
      containsValue in class LinkedHashMap<K,V>
    • entrySet

      public final Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Overrides:
      entrySet in class LinkedHashMap<K,V>
    • keySet

      public final Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
      Overrides:
      keySet in class LinkedHashMap<K,V>
    • getCacheHits

      public final long getCacheHits()
      Get the number of cache hits.
      Returns:
      the number of cache hits.
    • getCacheMisses

      public final long getCacheMisses()
      Get the number of cache misses.
      Returns:
      the number of cache misses.
    • getMaxCacheSize

      public final int getMaxCacheSize()
      Description copied from interface: Cache
      Return the maximum cache Size.
      Specified by:
      getMaxCacheSize in interface Cache<K,V>
      Returns:
      the maximum cache size.
    • setMaxCacheSize

      public final void setMaxCacheSize(int maxCacheSize)
      Description copied from interface: Cache
      Set the maximum cache size.
      Specified by:
      setMaxCacheSize in interface Cache<K,V>
      Parameters:
      maxCacheSize - the new maximum cache size.