to.etc.domui.util
Class LRUHashMap<K,V>

java.lang.Object
  extended by to.etc.domui.util.LRUHashMap<K,V>
All Implemented Interfaces:
java.util.Map<K,V>

public class LRUHashMap<K,V>
extends java.lang.Object
implements java.util.Map<K,V>

This is a LRU hashmap which will remove objects automatically when it's size becomes full.

Author:
Frits Jalvingh Created on Jun 4, 2008

Nested Class Summary
static interface LRUHashMap.SizeCalculator<V>
           
 
Constructor Summary
LRUHashMap(LRUHashMap.SizeCalculator<V> szc, int maxsize)
           
LRUHashMap(LRUHashMap.SizeCalculator<V> szc, int maxsize, int initial)
           
 
Method Summary
 void clear()
          Clear the map.
 java.lang.Object clone()
          FIXME Needs an implementation
 boolean containsKey(java.lang.Object key)
          Returns T if this map contains the keyed object.
 boolean containsValue(java.lang.Object value)
          Returns T if this map contains the value.
 java.util.Set<java.util.Map.Entry<K,V>> entrySet()
          Returns a collection view of the mappings contained in this map.
 V get(java.lang.Object key)
          Retrieve a value by key.
 int getMaxSize()
           
 int getObjectSize()
          Return the size of all stored objects.
 boolean isEmpty()
           
 java.util.Set<K> keySet()
           
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(java.util.Map<? extends K,? extends V> m)
          Adds all of the elements to this map.
 V remove(java.lang.Object key)
          Remove an entry by key.
 int size()
          Returns the current #elements in the map.
 java.util.Collection<V> values()
           
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

LRUHashMap

public LRUHashMap(LRUHashMap.SizeCalculator<V> szc,
                  int maxsize)

LRUHashMap

public LRUHashMap(LRUHashMap.SizeCalculator<V> szc,
                  int maxsize,
                  int initial)
Method Detail

size

public int size()
Returns the current #elements in the map.

Specified by:
size in interface java.util.Map<K,V>

getMaxSize

public int getMaxSize()

getObjectSize

public int getObjectSize()
Return the size of all stored objects.

Returns:

isEmpty

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

clear

public void clear()
Clear the map.

Specified by:
clear in interface java.util.Map<K,V>

get

public V get(java.lang.Object key)
Retrieve a value by key. If a value is found it gets moved to the most recently used position.

Specified by:
get in interface java.util.Map<K,V>

containsKey

public boolean containsKey(java.lang.Object key)
Returns T if this map contains the keyed object. If the element is found it is not touched as used.

Specified by:
containsKey in interface java.util.Map<K,V>

put

public V put(K key,
             V value)
Associates the specified value with the specified key in this map. If the map previously contained a mapping for this key, the old value is replaced.

Specified by:
put in interface java.util.Map<K,V>
Parameters:
key - key with which the specified value is to be associated.
value - value to be associated with the specified key.
Returns:
previous value associated with specified key, or null if there was no mapping for key. A null return can also indicate that the HashMap previously associated null with the specified key.

putAll

public void putAll(java.util.Map<? extends K,? extends V> m)
Adds all of the elements to this map.

Specified by:
putAll in interface java.util.Map<K,V>

remove

public V remove(java.lang.Object key)
Remove an entry by key.

Specified by:
remove in interface java.util.Map<K,V>
See Also:
Map.remove(java.lang.Object)

containsValue

public boolean containsValue(java.lang.Object value)
Returns T if this map contains the value. This is EXPENSIVE as it needs to walk all of the objects in the map.

Specified by:
containsValue in interface java.util.Map<K,V>
See Also:
Map.containsValue(java.lang.Object)

clone

public java.lang.Object clone()
FIXME Needs an implementation

Overrides:
clone in class java.lang.Object

keySet

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

values

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

entrySet

public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
Returns a collection view of the mappings contained in this map. Each element in the returned collection is a Map.Entry. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. The collection supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface java.util.Map<K,V>
Returns:
a collection view of the mappings contained in this map.
See Also:
Map.Entry