|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.samskivert.util.LRUHashMap<K,V>
public class LRUHashMap<K,V>
A HashMap with LRU functionality and rudimentary performance tracking facilities.
| Nested Class Summary | |
|---|---|
static interface |
LRUHashMap.ItemSizer<V>
Used to return the "size" of a cache item for systems that wish to differentiate cache items based on memory footprint or other metric. |
static interface |
LRUHashMap.RemovalObserver<K,V>
An observer may be registered with a LRU hash map to be notified when items are removed from the table (either explicitly or by being replaced with another value or due to being flushed). |
| Nested classes/interfaces inherited from interface java.util.Map |
|---|
Map.Entry<K,V> |
| Field Summary | |
|---|---|
protected boolean |
_canFlush
Used to temporarily disable flushing. |
protected LinkedHashMap<K,V> |
_delegate
Since we can't override addEntry and removeEntryForKey in Sun's lovely collection classes, we have to delegate to a HashMap and reimplement a crapload of stuff so that we can provide our required size tracking support. |
protected int |
_hits
|
protected int |
_maxSize
The maximum size of this cache. |
protected int |
_misses
|
protected LRUHashMap.RemovalObserver<K,V> |
_remobs
Notified when items are removed from the map, if non-null. |
protected HashSet<K> |
_seenKeys
|
protected int |
_size
The current size of this cache. |
protected LRUHashMap.ItemSizer<V> |
_sizer
Used to compute the size of items in this cache. |
protected boolean |
_tracking
Tracking info. |
| Constructor Summary | |
|---|---|
LRUHashMap(int maxSize)
Construct a LRUHashMap with the specified maximum size. |
|
LRUHashMap(int maxSize,
LRUHashMap.ItemSizer<V> sizer)
Construct a LRUHashMap with the specified maximum total size and the supplied item sizer which will be used to compute the size of each item. |
|
| Method Summary | |
|---|---|
void |
adjustSize(int sizeDifference)
Update the overall size of the cache if an already added item changes size. |
void |
clear()
|
boolean |
containsKey(Object key)
|
boolean |
containsValue(Object value)
|
protected void |
entryRemoved(V entry)
Adjust our size to reflect the removal of the specified entry. |
Set<Map.Entry<K,V>> |
entrySet()
|
boolean |
equals(Object o)
|
protected void |
flush()
Flushes entries from the cache until we're back under our desired cache size. |
V |
get(Object key)
|
int |
getMaxSize()
Returns this cache's maximum size. |
int[] |
getTrackedEffectiveness()
Return a measure of the effectiveness of this cache, the ratio of hits to misses. |
int |
hashCode()
|
boolean |
isEmpty()
|
Set<K> |
keySet()
|
V |
put(K key,
V value)
|
void |
putAll(Map<? extends K,? extends V> t)
|
V |
remove(Object key)
|
void |
setCanFlush(boolean canFlush)
Used to temporarily disable flushing elements from the cache. |
void |
setMaxSize(int maxSize)
Updates the cache's maximum size, flushing elements from the cache if necessary. |
void |
setRemovalObserver(LRUHashMap.RemovalObserver<K,V> obs)
Configures this hash map with a removal observer. |
void |
setTracking(boolean track)
Turn performance tracking on/off. |
int |
size()
|
Collection<V> |
values()
|
| Methods inherited from class java.lang.Object |
|---|
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected LinkedHashMap<K,V> _delegate
protected int _maxSize
protected int _size
protected boolean _canFlush
protected LRUHashMap.RemovalObserver<K,V> _remobs
protected LRUHashMap.ItemSizer<V> _sizer
protected boolean _tracking
protected HashSet<K> _seenKeys
protected int _hits
protected int _misses
| Constructor Detail |
|---|
public LRUHashMap(int maxSize)
public LRUHashMap(int maxSize,
LRUHashMap.ItemSizer<V> sizer)
| Method Detail |
|---|
public void setMaxSize(int maxSize)
public int getMaxSize()
public void setRemovalObserver(LRUHashMap.RemovalObserver<K,V> obs)
public void setCanFlush(boolean canFlush)
public void setTracking(boolean track)
public int[] getTrackedEffectiveness()
public int size()
size in interface Map<K,V>public void adjustSize(int sizeDifference)
sizeDifference - the amount to adjust the size by.public boolean isEmpty()
isEmpty in interface Map<K,V>public boolean containsKey(Object key)
containsKey in interface Map<K,V>public boolean containsValue(Object value)
containsValue in interface Map<K,V>public V get(Object key)
get in interface Map<K,V>
public V put(K key,
V value)
put in interface Map<K,V>protected void flush()
protected void entryRemoved(V entry)
public V remove(Object key)
remove in interface Map<K,V>public void putAll(Map<? extends K,? extends V> t)
putAll in interface Map<K,V>public void clear()
clear in interface Map<K,V>public Set<K> keySet()
keySet in interface Map<K,V>public Collection<V> values()
values in interface Map<K,V>public Set<Map.Entry<K,V>> entrySet()
entrySet in interface Map<K,V>public boolean equals(Object o)
equals in interface Map<K,V>equals in class Objectpublic int hashCode()
hashCode in interface Map<K,V>hashCode in class Object
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||