com.samskivert.util
Class LRUHashMap<K,V>

java.lang.Object
  extended by com.samskivert.util.LRUHashMap<K,V>
All Implemented Interfaces:
Map<K,V>

public class LRUHashMap<K,V>
extends Object
implements Map<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

_delegate

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. Yay! I wish we could support code reuse as well as Sun does.


_maxSize

protected int _maxSize
The maximum size of this cache.


_size

protected int _size
The current size of this cache.


_canFlush

protected boolean _canFlush
Used to temporarily disable flushing.


_remobs

protected LRUHashMap.RemovalObserver<K,V> _remobs
Notified when items are removed from the map, if non-null.


_sizer

protected LRUHashMap.ItemSizer<V> _sizer
Used to compute the size of items in this cache.


_tracking

protected boolean _tracking
Tracking info.


_seenKeys

protected HashSet<K> _seenKeys

_hits

protected int _hits

_misses

protected int _misses
Constructor Detail

LRUHashMap

public LRUHashMap(int maxSize)
Construct a LRUHashMap with the specified maximum size. All items in the cache will be considered to have a size of one.


LRUHashMap

public 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 Detail

setMaxSize

public void setMaxSize(int maxSize)
Updates the cache's maximum size, flushing elements from the cache if necessary.


getMaxSize

public int getMaxSize()
Returns this cache's maximum size.


setRemovalObserver

public void setRemovalObserver(LRUHashMap.RemovalObserver<K,V> obs)
Configures this hash map with a removal observer.


setCanFlush

public void setCanFlush(boolean canFlush)
Used to temporarily disable flushing elements from the cache. Generally this is only used to avoid undesired garbage collection until such time as it is acceptable. Beware the risks of leaving flushing disabled for too long.


setTracking

public void setTracking(boolean track)
Turn performance tracking on/off.


getTrackedEffectiveness

public int[] getTrackedEffectiveness()
Return a measure of the effectiveness of this cache, the ratio of hits to misses.

Returns:
an array containing {hits, misses}

size

public int size()
Specified by:
size in interface Map<K,V>

adjustSize

public void adjustSize(int sizeDifference)
Update the overall size of the cache if an already added item changes size.

Parameters:
sizeDifference - the amount to adjust the size by.

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

flush

protected void flush()
Flushes entries from the cache until we're back under our desired cache size.


entryRemoved

protected void entryRemoved(V entry)
Adjust our size to reflect the removal of the specified entry.


remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> t)
Specified by:
putAll in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

equals

public boolean equals(Object o)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object


Copyright © 2000-2008 Michael Bayne. All Rights Reserved.