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 void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
V
get(Object key)
long
getCacheHits()
Get the number of cache hits.long
getCacheMisses()
Get the number of cache misses.int
getMaxCacheSize()
Return the maximum cache Size.boolean
isEmpty()
Set<K>
keySet()
V
lookup(K key)
Returns the value of the specified key, ornull
.V
put(K key, V value)
Put a value in the cache.void
putAll(Map<? extends K,? extends V> m)
V
remove(Object key)
protected boolean
removeEldestEntry(Map.Entry<K,V> eldest)
void
setMaxCacheSize(int maxCacheSize)
Set the maximum cache size.int
size()
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:
removeEldestEntry
in classLinkedHashMap<K,V>
-
put
public final V put(K key, V value)
Description copied from interface:Cache
Put a value in the cache.
-
lookup
public final V lookup(K key)
Description copied from interface:Cache
Returns 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:
containsKey
in interfaceMap<K,V>
- Overrides:
containsKey
in classHashMap<K,V>
-
containsValue
public final boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
- Overrides:
containsValue
in 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:Cache
Return the maximum cache Size.- Specified by:
getMaxCacheSize
in interfaceCache<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 interfaceCache<K,V>
- Parameters:
maxCacheSize
- the new maximum cache size.
-
-