Package com.twelvemonkeys.util
Class TimeoutMap<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- com.twelvemonkeys.util.AbstractDecoratedMap<K,V>
-
- com.twelvemonkeys.util.TimeoutMap<K,V>
-
- All Implemented Interfaces:
ExpiringMap<K,V>
,java.io.Serializable
,java.lang.Cloneable
,java.util.Map<K,V>
public class TimeoutMap<K,V> extends AbstractDecoratedMap<K,V> implements ExpiringMap<K,V>, java.io.Serializable, java.lang.Cloneable
AMap
implementation that removes (expires) its elements after a given period. The map is by default backed by aHashMap
, or can be instantiated with any givenMap
as backing.Notes to consider when using this map:
- Elements may not expire on the exact millisecond as expected.
- The value returned by the
size()
method of the map, or any of its collection views, may not represent the exact number of entries in the map at any given time. - Elements in this map may expire at any time
(but never between invocations of
Iterator.hasNext()
andIterator.next()
orIterator.remove()
, when iterating the collection views).
- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/TimeoutMap.java#2 $
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
TimeoutMap.EntryIterator
private class
TimeoutMap.KeyIterator
private class
TimeoutMap.TimedEntry
Keeps track of timed objectsprivate class
TimeoutMap.TimeoutMapIterator<E>
Note: Iterating through this iterator will remove any expired values.private class
TimeoutMap.ValueIterator
-
Nested classes/interfaces inherited from class com.twelvemonkeys.util.AbstractDecoratedMap
AbstractDecoratedMap.BasicEntry<K,V>, AbstractDecoratedMap.EntrySet, AbstractDecoratedMap.KeySet, AbstractDecoratedMap.Values
-
-
Field Summary
Fields Modifier and Type Field Description protected long
expiryTime
Expiry timeprivate long
nextExpiryTime
-
Fields inherited from class com.twelvemonkeys.util.AbstractDecoratedMap
entries, modCount
-
-
Constructor Summary
Constructors Constructor Description TimeoutMap()
Creates aTimeoutMap
with the default expiry time of 1 minute.TimeoutMap(long pExpiryTime)
Creates aTimeoutMap
with the given expiry time (milliseconds).TimeoutMap(java.util.Map<? extends K,? extends V> pContents)
Creates aTimeoutMap
containing the same elements as the given map with the default expiry time of 1 minute.TimeoutMap(java.util.Map<K,java.util.Map.Entry<K,V>> pBacking, java.util.Map<? extends K,? extends V> pContents, long pExpiryTime)
Creates aTimeoutMap
with the given expiry time (milliseconds).
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all mappings from this map.boolean
containsKey(java.lang.Object pKey)
Returnstrue
if this map contains a mapping for the specified pKey.(package private) TimeoutMap.TimedEntry
createEntry(K pKey, V pValue)
java.util.Set<java.util.Map.Entry<K,V>>
entrySet()
V
get(java.lang.Object pKey)
Returns the value to which this map maps the specified pKey.long
getExpiryTime()
Gets the maximum time any value will be kept in the map, before it expires.boolean
isEmpty()
Returnstrue
if this map contains no key-value mappings.java.util.Set<K>
keySet()
protected java.util.Iterator<java.util.Map.Entry<K,V>>
newEntryIterator()
protected java.util.Iterator<K>
newKeyIterator()
protected java.util.Iterator<V>
newValueIterator()
void
processRemoved(java.util.Map.Entry pRemoved)
May be used by clients as a call-back to notify when mappings expire from the map.V
put(K pKey, V pValue)
Associates the specified pValue with the specified pKey in this map (optional operation).V
remove(java.lang.Object pKey)
Removes the mapping for this pKey from this map if present (optional operation).protected void
removeExpiredEntries()
Removes any expired mappings.private void
removeExpiredEntriesSynced(long pTime)
Okay, I guess this do resemble DCL...void
setExpiryTime(long pExpiryTime)
Sets the maximum time any value will be kept in the map, before it expires.int
size()
Returns the number of key-value mappings in this map.java.util.Collection<V>
values()
-
Methods inherited from class com.twelvemonkeys.util.AbstractDecoratedMap
clone, containsValue, getEntry, init, removeEntry
-
-
-
-
Constructor Detail
-
TimeoutMap
public TimeoutMap()
Creates aTimeoutMap
with the default expiry time of 1 minute. ThisTimeoutMap
will be backed by a newHashMap
instance.This is constructor is here to comply with the recommendations for "standard" constructors in the
Map
interface.- See Also:
TimeoutMap(long)
-
TimeoutMap
public TimeoutMap(java.util.Map<? extends K,? extends V> pContents)
Creates aTimeoutMap
containing the same elements as the given map with the default expiry time of 1 minute. ThisTimeoutMap
will be backed by a newHashMap
instance, and not the map passed in as a paramter.This is constructor is here to comply with the recommendations for "standard" constructors in the
Map
interface.- Parameters:
pContents
- the map whose mappings are to be placed in this map. May benull
.- See Also:
TimeoutMap(java.util.Map, Map, long)
,Map
-
TimeoutMap
public TimeoutMap(long pExpiryTime)
Creates aTimeoutMap
with the given expiry time (milliseconds). ThisTimeoutMap
will be backed by a newHashMap
instance.- Parameters:
pExpiryTime
- the expiry time (time to live) for elements in this map
-
TimeoutMap
public TimeoutMap(java.util.Map<K,java.util.Map.Entry<K,V>> pBacking, java.util.Map<? extends K,? extends V> pContents, long pExpiryTime)
Creates aTimeoutMap
with the given expiry time (milliseconds). ThisTimeoutMap
will be backed by the givenMap
.Note that structurally modifying the backing map directly (not through this map or its collection views), is not allowed, and will produce undeterministic exceptions.
- Parameters:
pBacking
- the map that will be used as backing.pContents
- the map whose mappings are to be placed in this map. May benull
.pExpiryTime
- the expiry time (time to live) for elements in this map
-
-
Method Detail
-
getExpiryTime
public long getExpiryTime()
Gets the maximum time any value will be kept in the map, before it expires.- Returns:
- the expiry time
-
setExpiryTime
public void setExpiryTime(long pExpiryTime)
Sets the maximum time any value will be kept in the map, before it expires. Removes any items that are older than the specified time.- Parameters:
pExpiryTime
- the expiry time (time to live) for elements in this map
-
size
public int size()
Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
.
-
isEmpty
public boolean isEmpty()
Returnstrue
if this map contains no key-value mappings.
-
containsKey
public boolean containsKey(java.lang.Object pKey)
Returnstrue
if this map contains a mapping for the specified pKey.- Specified by:
containsKey
in interfacejava.util.Map<K,V>
- Overrides:
containsKey
in classAbstractDecoratedMap<K,V>
- Parameters:
pKey
- pKey whose presence in this map is to be tested.- Returns:
true
if this map contains a mapping for the specified pKey.
-
get
public V get(java.lang.Object pKey)
Returns the value to which this map maps the specified pKey. Returnsnull
if the map contains no mapping for this pKey. A return value ofnull
does not necessarily indicate that the map contains no mapping for the pKey; it's also possible that the map explicitly maps the pKey tonull
. ThecontainsKey
operation may be used to distinguish these two cases.- Specified by:
get
in interfacejava.util.Map<K,V>
- Specified by:
get
in classAbstractDecoratedMap<K,V>
- Parameters:
pKey
- pKey whose associated value is to be returned.- Returns:
- the value to which this map maps the specified pKey, or
null
if the map contains no mapping for this pKey. - See Also:
containsKey(java.lang.Object)
-
put
public V put(K pKey, V pValue)
Associates the specified pValue with the specified pKey in this map (optional operation). If the map previously contained a mapping for this pKey, the old pValue is replaced.- Specified by:
put
in interfacejava.util.Map<K,V>
- Specified by:
put
in classAbstractDecoratedMap<K,V>
- Parameters:
pKey
- pKey with which the specified pValue is to be associated.pValue
- pValue to be associated with the specified pKey.- Returns:
- previous pValue associated with specified pKey, or
null
if there was no mapping for pKey. Anull
return can also indicate that the map previously associatednull
with the specified pKey, if the implementation supportsnull
values.
-
remove
public V remove(java.lang.Object pKey)
Removes the mapping for this pKey from this map if present (optional operation).- Specified by:
remove
in interfacejava.util.Map<K,V>
- Specified by:
remove
in classAbstractDecoratedMap<K,V>
- Parameters:
pKey
- pKey whose mapping is to be removed from the map.- Returns:
- previous value associated with specified pKey, or
null
if there was no mapping for pKey. Anull
return can also indicate that the map previously associatednull
with the specified pKey, if the implementation supportsnull
values.
-
clear
public void clear()
Removes all mappings from this map.
-
createEntry
TimeoutMap.TimedEntry createEntry(K pKey, V pValue)
- Overrides:
createEntry
in classAbstractDecoratedMap<K,V>
-
removeExpiredEntries
protected void removeExpiredEntries()
Removes any expired mappings.
-
removeExpiredEntriesSynced
private void removeExpiredEntriesSynced(long pTime)
Okay, I guess this do resemble DCL...- Parameters:
pTime
- now
-
values
public java.util.Collection<V> values()
-
keySet
public java.util.Set<K> keySet()
-
newKeyIterator
protected java.util.Iterator<K> newKeyIterator()
- Specified by:
newKeyIterator
in classAbstractDecoratedMap<K,V>
-
newValueIterator
protected java.util.Iterator<V> newValueIterator()
- Specified by:
newValueIterator
in classAbstractDecoratedMap<K,V>
-
newEntryIterator
protected java.util.Iterator<java.util.Map.Entry<K,V>> newEntryIterator()
- Specified by:
newEntryIterator
in classAbstractDecoratedMap<K,V>
-
processRemoved
public void processRemoved(java.util.Map.Entry pRemoved)
Description copied from interface:ExpiringMap
May be used by clients as a call-back to notify when mappings expire from the map.- Specified by:
processRemoved
in interfaceExpiringMap<K,V>
- Parameters:
pRemoved
- the removed mapping
-
-