K
- the type of keys maintained by this mapV
- the type of mapped valuespublic class ConcurrentMapWithTimedEvictionDecorator<K,V> extends AbstractMap<K,V> implements ConcurrentMapWithTimedEviction<K,V>
ConcurrentMapWithTimedEviction
implementation which decorates an
existing ConcurrentMap
implementation. This
class uses an instance of EvictionScheduler
for automatically
evicting entries upon expiration.
This class does not allow null to be used as a value. Whether or not null can be used as a key depends on the map being decorated.
This class and its views and iterators implement all of the optional
methods of the Map
and Iterator
interfaces.
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
Constructor and Description |
---|
ConcurrentMapWithTimedEvictionDecorator(ConcurrentMap<K,EvictibleEntry<K,V>> delegate,
EvictionScheduler<K,V> scheduler)
Creates a new map that supports timed entry eviction with the specified
delegate and eviction scheduler.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
Removes all of the mappings from this map.
|
boolean |
containsKey(Object key)
Returns true if this map contains a mapping for the specified
key and it has not expired yet.
|
boolean |
containsValue(Object value)
Returns true if this map maps one or more keys to the specified
value and at least one of them has not expired yet.
|
Set<Map.Entry<K,V>> |
entrySet()
Returns a
Set view of the mappings contained in this map. |
V |
get(Object key)
Returns the value to which the specified key is mapped, or
null
if this map contains no mapping for the key or it has expired. |
Set<K> |
keySet()
Returns a
Set view of the keys contained in this map. |
V |
put(K key,
V value)
Associates the specified value with the specified key in this map.
|
V |
put(K key,
V value,
long evictMs)
Associates the specified value with the specified key in this map for the
specified duration.
|
V |
putIfAbsent(K key,
V value)
If the specified key is not already associated with a value, associate it
with the given value.
|
V |
putIfAbsent(K key,
V value,
long evictMs)
If the specified key is not already associated with a value, associate it
with the given value for the specified duration.
|
V |
remove(Object key)
Removes the mapping for a key from this map if it is present.
|
boolean |
remove(Object key,
Object value)
Removes the entry for a key only if currently mapped to a given value.
|
V |
replace(K key,
V value)
Replaces the entry for a key only if currently mapped to some value.
|
V |
replace(K key,
V value,
long evictMs)
Replaces the entry for a key only if currently mapped to some value, for
the specified duration.
|
boolean |
replace(K key,
V oldValue,
V newValue)
Replaces the entry for a key only if currently mapped to a given value.
|
boolean |
replace(K key,
V oldValue,
V newValue,
long evictMs)
Replaces the entry for a key only if currently mapped to a given value,
for the specified duration.
|
int |
size()
Returns the number of key-value mappings in this map.
|
clone, equals, hashCode, isEmpty, putAll, toString, values
public ConcurrentMapWithTimedEvictionDecorator(ConcurrentMap<K,EvictibleEntry<K,V>> delegate, EvictionScheduler<K,V> scheduler)
delegate
- the actual map implementation to which all operations will be
eventually delegatedscheduler
- the scheduler used for automatically evicting entries when the
time they are allowed to stay in the map has elapsedNullPointerException
- if either the delegate or the scheduler is null
public int size()
ConcurrentMap
, it is ok to return
a weakly consistent value.public boolean containsKey(Object key)
containsKey
in interface Map<K,V>
containsKey
in class AbstractMap<K,V>
key
- key whose presence in this map is to be testedClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the specified key is null
and the delegate
map does not permit null
keyspublic boolean containsValue(Object value)
containsValue
in interface Map<K,V>
containsValue
in class AbstractMap<K,V>
value
- value whose presence in this map is to be testedClassCastException
- if the value is of an inappropriate type for this mapNullPointerException
- if the specified value is null
public V get(Object key)
null
if this map contains no mapping for the key or it has expired.
If the delegate map permits null values, then a return value of
null
does not necessarily indicate that the map contains
no mapping for the key; it's also possible that the map explicitly maps
the key to null
. The containsKey
operation
may be used to distinguish these two cases.
get
in interface Map<K,V>
get
in class AbstractMap<K,V>
key
- the key whose associated value is to be returnednull
if this map contains no mapping for the key or it has expiredClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the specified key is null
and the delegate
map does not permit null
keyspublic V put(K key, V value)
put
in interface Map<K,V>
put
in class AbstractMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyUnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of the specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of the specified key or value prevents it
from being stored in this mappublic V put(K key, V value, long evictMs)
m.containsKey(k)
would return true
.)put
in interface ConcurrentMapWithTimedEviction<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyevictMs
- the time in ms during which the entry can stay in the map
(time-to-live). When this time has elapsed, the entry will be
evicted from the map automatically. A value of 0 for this
argument means "forever", i.e. put(key, value, 0) is
equivalent to put(key, value).UnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of the specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of the specified key or value prevents it
from being stored in this map, or if evictMs is negativepublic V putIfAbsent(K key, V value)
if (!map.containsKey(key)) return map.put(key, value); else return map.get(key);except that the action is performed atomically.
putIfAbsent
in interface ConcurrentMap<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyUnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of the specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of the specified key or value prevents it
from being stored in this mappublic V putIfAbsent(K key, V value, long evictMs)
if (!map.containsKey(key)) return map.put(key, value, evictMs); else return map.get(key);except that the action is performed atomically.
putIfAbsent
in interface ConcurrentMapWithTimedEviction<K,V>
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyevictMs
- the time in ms during which the entry can stay in the map
(time-to-live). When this time has elapsed, the entry will be
evicted from the map automatically. A value of 0 for this
argument means "forever", i.e.
putIfAbsent(key, value, 0) is equivalent to
putIfAbsent(key, value).UnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of the specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of the specified key or value prevents it
from being stored in this map, or if evictMs is negativepublic V remove(Object key)
(key==null ? k==null : key.equals(k))
,
that mapping is removed. (The map can contain at most one such mapping.)
Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key or it has expired.
If the delegate map permits null values, then a return value of null does not necessarily indicate that the map contained no mapping for the key; it's also possible that the map explicitly mapped the key to null.
The map will not contain a mapping for the specified key once the call returns.
remove
in interface Map<K,V>
remove
in class AbstractMap<K,V>
key
- key whose mapping is to be removed from the map, if it has not
expired yet.UnsupportedOperationException
- if the remove operation is not supported by the
delegate mapClassCastException
- if the key is of an inappropriate type for this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keyspublic boolean remove(Object key, Object value)
if (map.containsKey(key) && map.get(key).equals(value)) { map.remove(key); return true; } else return false;except that the action is performed atomically.
remove
in interface ConcurrentMap<K,V>
key
- key with which the specified value is associatedvalue
- value expected to be associated with the specified keyUnsupportedOperationException
- if the remove operation is not supported by the
delegate mapClassCastException
- if the key or value is of an inappropriate type for this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullpublic V replace(K key, V value)
if (map.containsKey(key)) { return map.put(key, value); } else return null;except that the action is performed atomically.
replace
in interface ConcurrentMap<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keyUnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of the specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of the specified key or value prevents it
from being stored in this mappublic V replace(K key, V value, long evictMs)
if (map.containsKey(key)) { return map.put(key, value, evictMs); } else return null;except that the action is performed atomically.
replace
in interface ConcurrentMapWithTimedEviction<K,V>
key
- key with which the specified value is associatedvalue
- value to be associated with the specified keyevictMs
- the time in ms during which the entry can stay in the map
(time-to-live). When this time has elapsed, the entry will be
evicted from the map automatically. A value of 0 for this
argument means "forever", i.e. replace(key, value, 0)
is equivalent to replace(key, value).UnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of the specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of the specified key or value prevents it
from being stored in this map, or if evictMs is negativepublic boolean replace(K key, V oldValue, V newValue)
if (map.containsKey(key) && map.get(key).equals(oldValue)) { map.put(key, newValue); return true; } else return false;except that the action is performed atomically.
replace
in interface ConcurrentMap<K,V>
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified keyUnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of a specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of a specified key or value prevents it from
being stored in this mappublic boolean replace(K key, V oldValue, V newValue, long evictMs)
if (map.containsKey(key) && map.get(key).equals(oldValue)) { map.put(key, newValue, evictMs); return true; } else return false;except that the action is performed atomically.
replace
in interface ConcurrentMapWithTimedEviction<K,V>
key
- key with which the specified value is associatedoldValue
- value expected to be associated with the specified keynewValue
- value to be associated with the specified keyevictMs
- the time in ms during which the entry can stay in the map
(time-to-live). When this time has elapsed, the entry will be
evicted from the map automatically. A value of 0 for this
argument means "forever", i.e.
replace(key, oldValue, newValue, 0) is equivalent to
put(key, oldValue, newValue).UnsupportedOperationException
- if the put operation is not supported by the
delegate mapClassCastException
- if the class of a specified key or value prevents it from
being stored in this mapNullPointerException
- if the specified key is null and the delegate map does not
permit null keys, or if the specified value is nullIllegalArgumentException
- if some property of a specified key or value prevents it from
being stored in this map, or if evictMs is negativepublic void clear()
clear
in interface Map<K,V>
clear
in class AbstractMap<K,V>
UnsupportedOperationException
- if the clear operation is not supported by the
delegate mappublic Set<K> keySet()
Set
view of the keys contained in this map. The set is
backed by the map, so changes to the map are reflected in the set, and
vice-versa. If the map is modified while an iteration over the set is in
progress (except through the iterator's own remove operation),
the results of the iteration are undefined. The set supports element
removal, which removes the corresponding mapping from the map, via the
Iterator.remove, Set.remove, removeAll,
retainAll, and clear operations. It does not support
the add or addAll operations.public Set<Map.Entry<K,V>> entrySet()
Set
view of the mappings contained in this map. The set
is backed by the map, so changes to the map are reflected in the set, and
vice-versa. If the map is modified while an iteration over the set is in
progress (except through the iterator's own remove operation, or
through the setValue operation on a map entry returned by the
iterator) the results of the iteration are undefined. The set supports
element removal, which removes the corresponding mapping from the map,
via the Iterator.remove, Set.remove, removeAll
, retainAll and clear operations. It does not support
the add or addAll operations.Copyright © 2012–2016. All rights reserved.