Knopflerfish OSGi 3.0.0

Class CacheMap

  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by org.knopflerfish.util.CacheMap
All Implemented Interfaces:, java.lang.Cloneable, java.util.Map

public class CacheMap
extends java.util.HashMap

Timeout version of a HashMap.

Entries in the map have a limited lifelength, and the get method will only return the stored object during a limited time period.

After the timeout period, thr object will be removed.

See Also:
CachedObject, Serialized Form

Nested Class Summary
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
Field Summary
protected  long timeout
Constructor Summary
          Create a map with default timeout period.
CacheMap(long timeout)
          Create a map with a specified timeout period.
Method Summary
 boolean containsKey(java.lang.Object key)
          Check if an object exists in the map.
 java.lang.Object get(java.lang.Object key)
          Get an object from the map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Store an object in the map.
 void update()
          Force timeout check on all items in map and remove all expired keys and objects.
Methods inherited from class java.util.HashMap
clear, clone, containsValue, entrySet, isEmpty, keySet, putAll, remove, size, values
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
equals, hashCode

Field Detail


protected long timeout
Constructor Detail


public CacheMap()
Create a map with default timeout period.

Equivalent to CacheMap(CachedObject.DEFAULT_TIMEOUT)

See Also:
CachedObject.DEFAULT_TIMEOUT, CachedObject


public CacheMap(long timeout)
Create a map with a specified timeout period.

After an object has been stored in the map using put, it will only be available for timeout milliseconds. After that period, get will return null

timeout - timeout period in milliseconds for cached objects.
Method Detail


public java.lang.Object get(java.lang.Object key)
Get an object from the map. If the object's timeout period has expired, remove it from the map and return null.

Note: Since the timeout check is done for each cached object, be careful when looping over a map using iterators or other sequences of get. The result from a new get can become null faster than you might think. Thus always check for null return values from get.

Specified by:
get in interface java.util.Map
get in class java.util.HashMap


public boolean containsKey(java.lang.Object key)
Check if an object exists in the map. If non-existent or expired, return false.

Specified by:
containsKey in interface java.util.Map
containsKey in class java.util.HashMap


public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Store an object in the map. The object's timeout period will start from the current system time.

Specified by:
put in interface java.util.Map
put in class java.util.HashMap


public void update()
Force timeout check on all items in map and remove all expired keys and objects.

Normally, the timeout check is only done at get calls, but if necessary (for example at periodic complete printouts or before loops) you can (and probably want to) force a timeout check on all cached objects.

Knopflerfish OSGi 3.0.0