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>
-
- Type Parameters:
K
- the type of the keys of this cache.V
- the type of the values this cache caches.
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
,Cache<K,V>
public class LruCache<K,V> extends java.util.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
-
-
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(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
java.util.Set<java.util.Map.Entry<K,V>>
entrySet()
V
get(java.lang.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()
java.util.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(java.util.Map<? extends K,? extends V> m)
V
remove(java.lang.Object key)
protected boolean
removeEldestEntry(java.util.Map.Entry<K,V> eldest)
void
setMaxCacheSize(int maxCacheSize)
Set the maximum cache size.int
size()
java.util.Collection<V>
values()
-
Methods inherited from class java.util.HashMap
clone, compute, computeIfAbsent, computeIfPresent, merge, putIfAbsent, remove, replace, replace
-
-
-
-
Method Detail
-
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
.
-
get
public final V get(java.lang.Object key)
-
remove
public final V remove(java.lang.Object key)
-
clear
public final void clear()
-
size
public final int size()
-
isEmpty
public final boolean isEmpty()
-
values
public final java.util.Collection<V> values()
-
containsKey
public final boolean containsKey(java.lang.Object key)
-
containsValue
public final boolean containsValue(java.lang.Object value)
-
keySet
public final java.util.Set<K> keySet()
-
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.
-
-