Package org.jxmpp.util.cache
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>
-
- 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:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Constructor Summary
Constructors Constructor Description LruCache(int maxSize)Create a new cache and specify the maximum size of for the cache in bytes, and the maximum lifetime of objects.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<K,V>>entrySet()Vget(Object key)longgetCacheHits()Get the number of cache hits.longgetCacheMisses()Get the number of cache misses.intgetMaxCacheSize()Return the maximum cache Size.booleanisEmpty()Set<K>keySet()Vlookup(K key)Returns the value of the specified key, ornull.Vput(K key, V value)Put a value in the cache.voidputAll(Map<? extends K,? extends V> m)Vremove(Object key)protected booleanremoveEldestEntry(Map.Entry<K,V> eldest)voidsetMaxCacheSize(int maxCacheSize)Set the maximum cache size.intsize()Collection<V>values()-
Methods inherited from class java.util.LinkedHashMap
forEach, getOrDefault, replaceAll
-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, merge, putIfAbsent, remove, replace, replace
-
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
-
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, hashCode, merge, putIfAbsent, remove, replace, replace
-
-
-
-
Constructor Detail
-
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 Detail
-
removeEldestEntry
protected final boolean removeEldestEntry(Map.Entry<K,V> eldest)
- Overrides:
removeEldestEntryin classLinkedHashMap<K,V>
-
put
public final V put(K key, V value)
Description copied from interface:CachePut a value in the cache.
-
lookup
public final V lookup(K key)
Description copied from interface:CacheReturns the value of the specified key, ornull.
-
clear
public final void clear()
-
size
public final int size()
-
isEmpty
public final boolean isEmpty()
-
values
public final Collection<V> values()
-
containsKey
public final boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<K,V>- Overrides:
containsKeyin classHashMap<K,V>
-
containsValue
public final boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<K,V>- Overrides:
containsValuein classLinkedHashMap<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:CacheReturn the maximum cache Size.- Specified by:
getMaxCacheSizein interfaceCache<K,V>- Returns:
- the maximum cache size.
-
setMaxCacheSize
public final void setMaxCacheSize(int maxCacheSize)
Description copied from interface:CacheSet the maximum cache size.- Specified by:
setMaxCacheSizein interfaceCache<K,V>- Parameters:
maxCacheSize- the new maximum cache size.
-
-