public final class LruCache<K,V>
extends java.lang.Object
Null keys are not allowed. LruCache is synchronized.
Modifier and Type | Class and Description |
---|---|
static interface |
LruCache.Entry<K,V>
Interface for entry iterator;
|
Constructor and Description |
---|
LruCache(int initialCapacity)
Create the LRU cache with a specific capacity.
|
LruCache(int initialCapacity,
boolean isStatistics)
Create the LRU cache with a specific capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the cache
|
boolean |
compareAndPut(V testValue,
K key,
V value)
Puts a new item in the cache if the current value matches oldValue.
|
V |
get(K key)
Get an item from the cache and make it most recently used.
|
int |
getCapacity()
Returns the LRU cache capacity
|
long |
getHitCount()
Returns the hit count.
|
long |
getMissCount()
Returns the miss count.
|
java.util.Iterator<LruCache.Entry<K,V>> |
iterator()
Returns the entries
|
java.util.Iterator<K> |
keys()
Returns the keys stored in the cache
|
java.util.Iterator<K> |
keys(java.util.Iterator<K> oldIter)
Returns keys stored in the cache using an old iterator
|
V |
put(K key,
V value)
Puts a new item in the cache.
|
V |
putIfNew(K key,
V value)
Puts a new item in the cache.
|
V |
remove(K key)
Removes an item from the cache
|
boolean |
removeLongestTail()
Remove the last item in the LRU.
|
boolean |
removeTail()
Remove the last item in the LRU
|
void |
setEnableListeners(boolean isEnable)
Disable the listeners
|
void |
setEnableStatistics(boolean isEnable) |
int |
size()
Returns the current number of entries in the cache.
|
java.util.Iterator<V> |
values()
Returns the values in the cache
|
java.util.Iterator<V> |
values(java.util.Iterator<V> oldIter) |
public LruCache(int initialCapacity)
initialCapacity
- minimum capacity of the cachepublic LruCache(int initialCapacity, boolean isStatistics)
initialCapacity
- minimum capacity of the cachepublic void setEnableListeners(boolean isEnable)
public void setEnableStatistics(boolean isEnable)
public int size()
public int getCapacity()
public void clear()
public V get(K key)
key
- key to lookup the itempublic V put(K key, V value)
key
- key to store datavalue
- value to be storedpublic V putIfNew(K key, V value)
key
- key to store datavalue
- value to be storedpublic boolean compareAndPut(V testValue, K key, V value)
key
- the keyvalue
- the new valuetestValue
- the value to test against the currentpublic boolean removeTail()
public boolean removeLongestTail()
public V remove(K key)
key
- the key to removepublic java.util.Iterator<K> keys()
public java.util.Iterator<K> keys(java.util.Iterator<K> oldIter)
public java.util.Iterator<V> values()
public java.util.Iterator<LruCache.Entry<K,V>> iterator()
public long getHitCount()
public long getMissCount()