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>, Serializable, Cloneable, Map<K, V>
public class TimeoutMap<K,V>
extends AbstractDecoratedMap<K,V>
implements ExpiringMap<K,V>, Serializable, Cloneable
A
Map implementation that removes (expires) its elements after
a given period. The map is by default backed by a HashMap,
or can be instantiated with any given Map 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classprivate classprivate classKeeps track of timed objectsprivate classNote: Iterating through this iterator will remove any expired values.private classNested classes/interfaces inherited from class AbstractDecoratedMap
AbstractDecoratedMap.BasicEntry<K,V>, AbstractDecoratedMap.EntrySet, AbstractDecoratedMap.KeySet, AbstractDecoratedMap.Values Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longExpiry timeprivate long///////////////////Fields inherited from class AbstractDecoratedMap
entries, modCount -
Constructor Summary
ConstructorsConstructorDescriptionCreates aTimeoutMapwith the default expiry time of 1 minute.TimeoutMap(long pExpiryTime) Creates aTimeoutMapwith the given expiry time (milliseconds).TimeoutMap(Map<? extends K, ? extends V> pContents) Creates aTimeoutMapcontaining the same elements as the given map with the default expiry time of 1 minute.TimeoutMap(Map<K, Map.Entry<K, V>> pBacking, Map<? extends K, ? extends V> pContents, long pExpiryTime) Creates aTimeoutMapwith the given expiry time (milliseconds). -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all mappings from this map.booleancontainsKey(Object pKey) Returnstrueif this map contains a mapping for the specified pKey.(package private) TimeoutMap<K,V>.TimedEntry createEntry(K pKey, V pValue) entrySet()Returns the value to which this map maps the specified pKey.longGets the maximum time any value will be kept in the map, before it expires.booleanisEmpty()Returnstrueif this map contains no key-value mappings.keySet()voidprocessRemoved(Map.Entry pRemoved) May be used by clients as a call-back to notify when mappings expire from the map.Associates the specified pValue with the specified pKey in this map (optional operation).Removes the mapping for this pKey from this map if present (optional operation).protected voidRemoves any expired mappings.private voidremoveExpiredEntriesSynced(long pTime) Okay, I guess this do resemble DCL...voidsetExpiryTime(long pExpiryTime) Sets the maximum time any value will be kept in the map, before it expires.intsize()Returns the number of key-value mappings in this map.values()Methods inherited from class AbstractDecoratedMap
clone, containsValue, getEntry, init, removeEntryMethods inherited from class AbstractMap
equals, hashCode, putAll, toStringMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, containsValue, equals, forEach, getOrDefault, hashCode, merge, putAll, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
expiryTime
protected long expiryTimeExpiry time -
nextExpiryTime
private volatile long nextExpiryTime///////////////////
-
-
Constructor Details
-
TimeoutMap
public TimeoutMap()Creates aTimeoutMapwith the default expiry time of 1 minute. ThisTimeoutMapwill be backed by a newHashMapinstance.This is constructor is here to comply with the recommendations for "standard" constructors in the
Mapinterface.- See Also:
-
TimeoutMap
Creates aTimeoutMapcontaining the same elements as the given map with the default expiry time of 1 minute. ThisTimeoutMapwill be backed by a newHashMapinstance, and not the map passed in as a paramter.This is constructor is here to comply with the recommendations for "standard" constructors in the
Mapinterface.- Parameters:
pContents- the map whose mappings are to be placed in this map. May benull.- See Also:
-
TimeoutMap
public TimeoutMap(long pExpiryTime) Creates aTimeoutMapwith the given expiry time (milliseconds). ThisTimeoutMapwill be backed by a newHashMapinstance.- Parameters:
pExpiryTime- the expiry time (time to live) for elements in this map
-
TimeoutMap
public TimeoutMap(Map<K, Map.Entry<K, V>> pBacking, Map<? extends K, ? extends V> pContents, long pExpiryTime) Creates aTimeoutMapwith the given expiry time (milliseconds). ThisTimeoutMapwill 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 Details
-
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_VALUEelements, returnsInteger.MAX_VALUE. -
isEmpty
-
containsKey
Returnstrueif this map contains a mapping for the specified pKey.- Specified by:
containsKeyin interfaceMap<K,V> - Overrides:
containsKeyin classAbstractDecoratedMap<K,V> - Parameters:
pKey- pKey whose presence in this map is to be tested.- Returns:
trueif this map contains a mapping for the specified pKey.
-
get
Returns the value to which this map maps the specified pKey. Returnsnullif the map contains no mapping for this pKey. A return value ofnulldoes not necessarily indicate that the map contains no mapping for the pKey; it's also possible that the map explicitly maps the pKey tonull. ThecontainsKeyoperation may be used to distinguish these two cases. -
put
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:
putin interfaceMap<K,V> - Specified by:
putin 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
nullif there was no mapping for pKey. Anullreturn can also indicate that the map previously associatednullwith the specified pKey, if the implementation supportsnullvalues.
-
remove
Removes the mapping for this pKey from this map if present (optional operation).- Specified by:
removein interfaceMap<K,V> - Specified by:
removein classAbstractDecoratedMap<K,V> - Parameters:
pKey- pKey whose mapping is to be removed from the map.- Returns:
- previous value associated with specified pKey, or
nullif there was no mapping for pKey. Anullreturn can also indicate that the map previously associatednullwith the specified pKey, if the implementation supportsnullvalues.
-
clear
-
createEntry
- Overrides:
createEntryin 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
-
entrySet
-
keySet
-
newKeyIterator
- Specified by:
newKeyIteratorin classAbstractDecoratedMap<K,V>
-
newValueIterator
- Specified by:
newValueIteratorin classAbstractDecoratedMap<K,V>
-
newEntryIterator
- Specified by:
newEntryIteratorin classAbstractDecoratedMap<K,V>
-
processRemoved
Description copied from interface:ExpiringMapMay be used by clients as a call-back to notify when mappings expire from the map.- Specified by:
processRemovedin interfaceExpiringMap<K,V> - Parameters:
pRemoved- the removed mapping
-