org.pf.util
Class TemporaryMap

java.lang.Object
  extended by org.pf.util.MapWrapper
      extended by org.pf.util.TemporaryMap
All Implemented Interfaces:
java.io.Serializable, java.util.Map, TriggerClient

public class TemporaryMap
extends MapWrapper
implements TriggerClient

This implementation of a Map behaves like a Hashtable except that it associates a timestamp with each entry. Periodically it checks all its entries' timestamp against an expire period. If an entry is expired it will be automatically removed. Therefore all elements just live temporarily in this map.

Version:
1.4
Author:
Manfred Duchrow
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
TemporaryMap(long expiresAfterMs, long checkAfterMs)
          Initialize the new instance with values that define the expiration of any object in the map and the interval for checking if expired objects are in the map.
 
Method Summary
 boolean canBeTriggeredBy(Trigger trigger)
          FOR INTERNAL USE ONLY!
 boolean containsKey(java.lang.Object key)
          This method returns true, if the given key can be found in this map.
 boolean containsValue(java.lang.Object value)
          This method returns true, if the given value is a value of any entry in this map.
 java.util.Set entrySet()
          This method automatically touches every entry, so that its expire period starts anew.
 java.lang.Object get(java.lang.Object key)
          Returns the object that was registered under the specified key.
 java.util.Set keySet()
          Returns all keys in this map.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Puts the given value under the specified key into the map.
 void putAll(java.util.Map map)
          Each entry of the given map will be added.
 java.lang.Object remove(java.lang.Object key)
          Remove the entry with the given key and return the associated object.
 boolean triggeredBy(Trigger trigger)
          FOR INTERNAL USE ONLY!
 java.util.Collection values()
          Returns all values of the map.
 
Methods inherited from class org.pf.util.MapWrapper
clear, isEmpty, size
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

TemporaryMap

public TemporaryMap(long expiresAfterMs,
                    long checkAfterMs)
Initialize the new instance with values that define the expiration of any object in the map and the interval for checking if expired objects are in the map.

Parameters:
expiresAfterMs - Defines after what time (milliseconds) an unsued object must be removed (must be >= 100)
checkAfterMs - Defines the interval for lookup and remove expired objects (must be >= 10)
Method Detail

containsKey

public boolean containsKey(java.lang.Object key)
This method returns true, if the given key can be found in this map. If an entry with this key is found, its associated timestamp will be set anew, to ensure that any immediate call to get() still returns the object. Otherwise it could happen, that the object is already removed by the background process that checks the timestamps and expiration times.

Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class MapWrapper
See Also:
Map.containsKey(Object)

containsValue

public boolean containsValue(java.lang.Object value)
This method returns true, if the given value is a value of any entry in this map. If such an entry is found, its associated timestamp will be set anew, to ensure that any immediate call to get() still returns the object. Otherwise it could happen, that the object is already removed by the background process that checks the timestamps and expiration times.

Specified by:
containsValue in interface java.util.Map
Overrides:
containsValue in class MapWrapper
See Also:
Map.containsValue(Object)

entrySet

public java.util.Set entrySet()
This method automatically touches every entry, so that its expire period starts anew.

Specified by:
entrySet in interface java.util.Map
Overrides:
entrySet in class MapWrapper
See Also:
Map.entrySet()

get

public java.lang.Object get(java.lang.Object key)
Returns the object that was registered under the specified key. Here this method also renews the timestamp associated with the key's entry. That is, the expiration time starts from 0 again.

Specified by:
get in interface java.util.Map
Overrides:
get in class MapWrapper
See Also:
Map.get(Object)

keySet

public java.util.Set keySet()
Returns all keys in this map.
Resets the timestamp of all entries to ensure that the immediate usage of the returned keys with the get() method produce the associated values!

Specified by:
keySet in interface java.util.Map
Overrides:
keySet in class MapWrapper
See Also:
Map.keySet()

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Puts the given value under the specified key into the map. It also associates a timestamp with this entry. Therefore it can be automatically removed after the defined expiration time.

Specified by:
put in interface java.util.Map
Overrides:
put in class MapWrapper
See Also:
Map.put(Object, Object)

putAll

public void putAll(java.util.Map map)
Each entry of the given map will be added. Each added entry gets an associated timestamp.

Specified by:
putAll in interface java.util.Map
Overrides:
putAll in class MapWrapper
See Also:
Map.putAll(Map)

values

public java.util.Collection values()
Returns all values of the map. With this method the timestamps of all entries in this map will be set anew. That means, their expiration time starts again at 0.

Specified by:
values in interface java.util.Map
Overrides:
values in class MapWrapper
See Also:
Map.values()

remove

public java.lang.Object remove(java.lang.Object key)
Remove the entry with the given key and return the associated object.

Specified by:
remove in interface java.util.Map
Overrides:
remove in class MapWrapper
Parameters:
key - The key of the entry to be removed
Returns:
The associated object or null if the key could not be found
See Also:
Map.remove(Object)

canBeTriggeredBy

public boolean canBeTriggeredBy(Trigger trigger)
FOR INTERNAL USE ONLY!
Will be called by a Trigger to check if object clean-up can be executed.

Specified by:
canBeTriggeredBy in interface TriggerClient
Parameters:
trigger - The trigger that calls this method
Returns:
true, if the service method triggeredBy() can be called
See Also:
TriggerClient.canBeTriggeredBy(Trigger)

triggeredBy

public boolean triggeredBy(Trigger trigger)
FOR INTERNAL USE ONLY!
Will be called by a Trigger to execute the clean-up of expired objects.

Specified by:
triggeredBy in interface TriggerClient
Parameters:
trigger - The trigger that calls this method
Returns:
true, if the trigger should continue, otherwise false
See Also:
TriggerClient.triggeredBy(Trigger)