public class LruCache<K,V>
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
protected static class |
LruCache.Entry<K,V>
An LRU cache entry.
|
Constructor and Description |
---|
LruCache() |
Modifier and Type | Method and Description |
---|---|
protected void |
add(LruCache.Entry<K,V> entry)
Adds a new entry to this cache in response to
put(Object, Object) . |
void |
clear()
Removes all entries from this cache.
|
protected void |
doAdd(LruCache.Entry<K,V> entry)
Actually adds a new entry to this cache.
|
protected void |
doRemove(LruCache.Entry<K,V> entry)
Actually removes an existing entry from this cache.
|
protected LruCache.Entry<K,V> |
entryByKey(java.lang.Object key)
Returns the corresponding entry for the given key, or
null if this cache contains no entry for the key. |
V |
get(java.lang.Object key)
Returns the corresponding value for the given key and moves the
corresponding entry to the most recently used position in this cache.
|
protected LruCache.Entry<K,V> |
getLruEntry()
Returns the least recently used cache entry, or
null if this cache is empty. |
protected LruCache.Entry<K,V> |
getMruEntry()
Returns the most recently used cache entry, or
null if this cache is empty. |
boolean |
isEmpty()
Returns whether this cache is empty.
|
protected void |
moveToMru(LruCache.Entry<K,V> entry)
Moves an existing cache entry to the MRU position.
|
protected LruCache.Entry<K,V> |
newEntry(K key,
V value)
Creates a new cache entry with the given key and value.
|
V |
peek(java.lang.Object key)
Returns the corresponding value for the given key without disturbing
cache ordering, or
null if this cache contains no value
for the key. |
V |
put(K key,
V value)
Caches the given value for the given key and moves the corresponding
entry to the most recently used position in this cache.
|
protected void |
remove(LruCache.Entry<K,V> entry)
Removes an existing entry from this cache in response to
remove(Object) . |
V |
remove(java.lang.Object key)
Removes the cache entry for the given key if it is present.
|
int |
size()
Returns the size of this cache.
|
java.util.Map<K,V> |
snapshot()
Returns a snapshot of the current contents of this cache,
ordered from most recently accessed to least recently accessed.
|
java.lang.String |
toString() |
protected void |
update(LruCache.Entry<K,V> entry,
V value)
Updates an existing cache entry to change its value
and moves it to the MRU position in response to
put(Object, Object) . |
public final int size()
public final boolean isEmpty()
true
if the cache is empty,
and false
otherwisepublic final V get(java.lang.Object key)
null
is returned.key
- the key whose corresponding value is to be returnednull
if the cache contains no value for the keypublic final V peek(java.lang.Object key)
null
if this cache contains no value
for the key.key
- the key whose corresponding value is to be returnednull
if the cache contains no value for the keypublic final V put(K key, V value)
null
if the cache contained no value for the key.key
- the key for which the given value is to be cached
(not null
)value
- the value to be cached for the given key
(not null
)null
if the cache contained no value for the keypublic final V remove(java.lang.Object key)
null
if this cache contained no value for the key.key
- the key whose entry is to be removed from the cachenull
if the cache contained no value for the keypublic void clear()
public final java.util.Map<K,V> snapshot()
null
)public java.lang.String toString()
toString
in class java.lang.Object
protected final LruCache.Entry<K,V> getMruEntry()
null
if this cache is empty.null
if the cache is emptyprotected final LruCache.Entry<K,V> getLruEntry()
null
if this cache is empty.null
if the cache is emptyprotected final LruCache.Entry<K,V> entryByKey(java.lang.Object key)
null
if this cache contains no entry for the key.key
- the key whose corresponding entry is to be returnednull
if the cache contains no entry for the keyprotected LruCache.Entry<K,V> newEntry(K key, V value)
key
- the key of the new entry (never null
)value
- the value of the new entry (never null
)null
)protected void add(LruCache.Entry<K,V> entry)
put(Object, Object)
.
This implementation invokes doAdd(Entry)
.
entry
- the entry to add (never null
)protected void update(LruCache.Entry<K,V> entry, V value)
put(Object, Object)
.
This implementation changes the entry value and then invokes
moveToMru(Entry)
.
entry
- the entry to update (never null
)value
- a new value for the entry (never null
)protected void remove(LruCache.Entry<K,V> entry)
remove(Object)
.
This implementation invokes doRemove(Entry)
.
entry
- the entry to remove (never null
)protected void doAdd(LruCache.Entry<K,V> entry)
entry
- the entry to add (never null
)protected void doRemove(LruCache.Entry<K,V> entry)
entry
- the entry to remove (never null
)protected void moveToMru(LruCache.Entry<K,V> entry)
entry
- the entry to move (never null
)Copyright (c) 2014, 2019 1C-Soft LLC and others. Made available under the Eclipse Public License 2.0