Package org.eclipse.handly.util
Class LruCache<K,V>
java.lang.Object
org.eclipse.handly.util.LruCache<K,V>
- Direct Known Subclasses:
BoundedLruCache
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidadd(LruCache.Entry<K, V> entry) Adds a new entry to this cache in response toput(Object, Object).voidclear()Removes all entries from this cache.protected voiddoAdd(LruCache.Entry<K, V> entry) Actually adds a new entry to this cache.protected voiddoRemove(LruCache.Entry<K, V> entry) Actually removes an existing entry from this cache.protected final LruCache.Entry<K,V> entryByKey(Object key) Returns the corresponding entry for the given key, ornullif this cache contains no entry for the key.final VReturns the corresponding value for the given key and moves the corresponding entry to the most recently used position in this cache.protected final LruCache.Entry<K,V> Returns the least recently used cache entry, ornullif this cache is empty.protected final LruCache.Entry<K,V> Returns the most recently used cache entry, ornullif this cache is empty.final booleanisEmpty()Returns whether this cache is empty.protected voidmoveToMru(LruCache.Entry<K, V> entry) Moves an existing cache entry to the MRU position.protected LruCache.Entry<K,V> Creates a new cache entry with the given key and value.final VReturns the corresponding value for the given key without disturbing cache ordering, ornullif this cache contains no value for the key.final VCaches the given value for the given key and moves the corresponding entry to the most recently used position in this cache.final VRemoves the cache entry for the given key if it is present.protected voidremove(LruCache.Entry<K, V> entry) Removes an existing entry from this cache in response toremove(Object).final intsize()Returns the size of this cache.snapshot()Returns a snapshot of the current contents of this cache, ordered from most recently accessed to least recently accessed.toString()protected voidupdate(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 toput(Object, Object).
-
Constructor Details
-
LruCache
public LruCache()
-
-
Method Details
-
size
public final int size()Returns the size of this cache.- Returns:
- the size of the cache
-
isEmpty
public final boolean isEmpty()Returns whether this cache is empty.- Returns:
trueif the cache is empty, andfalseotherwise
-
get
Returns the corresponding value for the given key and moves the corresponding entry to the most recently used position in this cache. If the cache contains no value for the key,nullis returned.- Parameters:
key- the key whose corresponding value is to be returned- Returns:
- the corresponding value for the given key, or
nullif the cache contains no value for the key
-
peek
Returns the corresponding value for the given key without disturbing cache ordering, ornullif this cache contains no value for the key.- Parameters:
key- the key whose corresponding value is to be returned- Returns:
- the corresponding value for the given key, or
nullif the cache contains no value for the key
-
put
Caches the given value for the given key and moves the corresponding entry to the most recently used position in this cache. Returns the previous value of the updated cache entry, ornullif the cache contained no value for the key.- Parameters:
key- the key for which the given value is to be cached (notnull)value- the value to be cached for the given key (notnull)- Returns:
- the previous value of the updated cache entry, or
nullif the cache contained no value for the key
-
remove
Removes the cache entry for the given key if it is present. Returns the value of the removed cache entry, ornullif this cache contained no value for the key.- Parameters:
key- the key whose entry is to be removed from the cache- Returns:
- the value of the removed cache entry, or
nullif the cache contained no value for the key
-
clear
public void clear()Removes all entries from this cache. -
snapshot
Returns a snapshot of the current contents of this cache, ordered from most recently accessed to least recently accessed.- Returns:
- a snapshot of the current contents of the cache
(never
null)
-
toString
-
getMruEntry
Returns the most recently used cache entry, ornullif this cache is empty.- Returns:
- the MRU entry, or
nullif the cache is empty
-
getLruEntry
Returns the least recently used cache entry, ornullif this cache is empty.- Returns:
- the LRU entry, or
nullif the cache is empty
-
entryByKey
Returns the corresponding entry for the given key, ornullif this cache contains no entry for the key.- Parameters:
key- the key whose corresponding entry is to be returned- Returns:
- the corresponding entry for the given key, or
nullif the cache contains no entry for the key
-
newEntry
Creates a new cache entry with the given key and value.- Parameters:
key- the key of the new entry (nevernull)value- the value of the new entry (nevernull)- Returns:
- the created entry (not
null)
-
add
Adds a new entry to this cache in response toput(Object, Object).This implementation invokes
doAdd(Entry).- Parameters:
entry- the entry to add (nevernull)
-
update
Updates an existing cache entry to change its value and moves it to the MRU position in response toput(Object, Object).This implementation changes the entry value and then invokes
moveToMru(Entry).- Parameters:
entry- the entry to update (nevernull)value- a new value for the entry (nevernull)
-
remove
Removes an existing entry from this cache in response toremove(Object).This implementation invokes
doRemove(Entry).- Parameters:
entry- the entry to remove (nevernull)
-
doAdd
Actually adds a new entry to this cache.- Parameters:
entry- the entry to add (nevernull)
-
doRemove
Actually removes an existing entry from this cache.- Parameters:
entry- the entry to remove (nevernull)
-
moveToMru
Moves an existing cache entry to the MRU position.- Parameters:
entry- the entry to move (nevernull)
-