org.knopflerfish.util
Class CacheMap
java.lang.Object
|
+--java.util.AbstractMap
|
+--java.util.HashMap
|
+--org.knopflerfish.util.CacheMap
- All Implemented Interfaces:
- java.lang.Cloneable, java.util.Map, java.io.Serializable
- 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
| Inner classes inherited from class java.util.Map |
java.util.Map.Entry |
|
Field Summary |
protected long |
timeout
|
|
Constructor Summary |
CacheMap()
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 |
timeout
protected long timeout
CacheMap
public CacheMap()
- Create a map with default timeout period.
Equivalent to CacheMap(CachedObject.DEFAULT_TIMEOUT)
- See Also:
CachedObject.DEFAULT_TIMEOUT,
CachedObject
CacheMap
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
- Parameters:
timeout - timeout period in milliseconds for cached objects.
get
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.
- Overrides:
get in class java.util.HashMap
containsKey
public boolean containsKey(java.lang.Object key)
- Check if an object exists in the map. If non-existent or expired, return
false.
- Overrides:
containsKey in class java.util.HashMap
put
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.
- Overrides:
put in class java.util.HashMap
update
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.