Map to evict expired entries once their expiration
time has been reached.
When putting a key-value pair in the map this decorator uses a
ExpirationPolicy to determine how long the entry should remain alive
as defined by an expiration time value.
When accessing the mapped value for a key, its expiration time is checked,
and if it is a negative value or if it is greater than the current time, the
mapped value is returned. Otherwise, the key is removed from the decorated
map, and null is returned.
When invoking methods that involve accessing the entire map contents (i.e
containsKey(Object), entrySet(), etc.) this decorator
removes all expired entries prior to actually completing the invocation.
Note that PassiveExpiringMap is not synchronized and is not
thread-safe. If you wish to use this map from multiple threads
concurrently, you must use appropriate synchronization. The simplest approach
is to wrap this map using java..
This class may throw exceptions when accessed by concurrent threads without
synchronization.
| Modifier and Type | Class and Description |
|---|---|
| public static class | PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy<
the type of the keys in the map K, the type of the values in the map V>A |
| public static interface | PassiveExpiringMap.ExpirationPolicy<
the key object type. K, the value object type V>A policy to determine the expiration time for key-value entries. |
| Modifier and Type | Field and Description |
|---|---|
| private final Map | expirationMap
map used to manage expiration times for the actual map entries. |
| private final PassiveExpiringMap. | expiringPolicy
the policy used to determine time-to-live values for map entries. |
| private static final long | serialVersionUID
Serialization version |
| Access | Constructor and Description |
|---|---|
| public | |
| public | PassiveExpiringMap(final PassiveExpiringMap.
the policy used to determine expiration times of
entries as they are added. expiringPolicyConstruct a map decorator using the given expiration policy to determine expiration times. |
| public | PassiveExpiringMap(final PassiveExpiringMap.
the policy used to determine expiration times of
entries as they are added. expiringPolicy,the map to decorate, must not be null. map)Construct a map decorator that decorates the given map and uses the given expiration policy to determine expiration times. |
| public | PassiveExpiringMap(final long
the constant amount of time (in milliseconds) an
entry is available before it expires. A negative value results in
entries that NEVER expire. A zero value results in entries that
ALWAYS expire. timeToLiveMillis)Construct a map decorator that decorates the given map using the given
time-to-live value measured in milliseconds to create and use a
|
| public | PassiveExpiringMap(final long
the constant amount of time (in milliseconds) an
entry is available before it expires. A negative value results in
entries that NEVER expire. A zero value results in entries that
ALWAYS expire. timeToLiveMillis, final Map<K, V> the map to decorate, must not be null. map)Construct a map decorator using the given time-to-live value measured in
milliseconds to create and use a
|
| public | PassiveExpiringMap(final long
the constant amount of time an entry is available
before it expires. A negative value results in entries that NEVER
expire. A zero value results in entries that ALWAYS expire. timeToLive, final TimeUnit the unit of time for the timeUnit)timeToLive
parameter, must not be null.Construct a map decorator using the given time-to-live value measured in
the given time units of measure to create and use a
|
| public | PassiveExpiringMap(final long
the constant amount of time an entry is available
before it expires. A negative value results in entries that NEVER
expire. A zero value results in entries that ALWAYS expire. timeToLive, final TimeUnit the unit of time for the timeUnit, final Map<K, V> timeToLive
parameter, must not be null.the map to decorate, must not be null. map)Construct a map decorator that decorates the given map using the given
time-to-live value measured in the given time units of measure to create
|
| public | PassiveExpiringMap(final Map<K, V>
the map to decorate, must not be null. map)Constructs a map decorator that decorates the given map and results in entries NEVER expiring. |
| Modifier and Type | Method and Description |
|---|---|
| public void | clear()
Overrides org. Implements java. Map#clear() behavior with the addition of clearing all
expiration entries as well.
|
| public boolean | containsKey(final Object
key whose presence in this map is to be tested key)Overrides org. Implements java. |
| public boolean | containsValue(final Object
value whose presence in this map is to be tested value)Overrides org. Implements java. |
| public Set | entrySet()
Overrides org. Implements java. |
| public V | get(final Object
the key whose associated value is to be returned key)Overrides org. Implements java. |
| public boolean | isEmpty()
Overrides org. Implements java. |
| private boolean | Returns: true if expirationTimeObject is ≥ 0
and expirationTimeObject < now.
false otherwise.the time in milliseconds used to compare against the
expiration time. now, final Long the expiration time value retrieved from
expirationTimeObject)expirationMap, can be null.Determines if the given expiration time is less than |
| public Set | keySet()
Overrides org. Implements java. |
| private long | |
| public V | put(final K
key with which the specified value is to be associated key, final V value to be associated with the specified key value)Overrides org. Implements java. expiringPolicy.
|
| public void | putAll(final Map<? extends K, ? extends V>
mappings to be stored in this map mapToCopy)Overrides org. Implements java. |
| private void | |
| public V | remove(final Object
key whose mapping is to be removed from the map key)Overrides org. Implements java. Map#remove(Object) behavior with the addition of removing
any expiration entry as well.
|
| private void | removeAllExpired(final long now)
Removes all entries in the map whose expiration time is less than
|
| private void | removeIfExpired(final Object key, final long now)
Removes the entry with the given key if the entry's expiration time is
less than |
| public int | size()
Overrides org. Implements java. |
| private static long | validateAndConvertToMillis(final long
the constant amount of time an entry is available
before it expires. A negative value results in entries that NEVER
expire. A zero value results in entries that ALWAYS expire. timeToLive, final TimeUnit the unit of time for the timeUnit)timeToLive
parameter, must not be null.First validate the input parameters. |
| public Collection | values()
Overrides org. Implements java. |
| private void |
| expirationMap | back to summary |
|---|---|
| private final Map<Object, Long> expirationMap map used to manage expiration times for the actual map entries. | |
| expiringPolicy | back to summary |
|---|---|
| private final PassiveExpiringMap. the policy used to determine time-to-live values for map entries. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version | |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap() Default constructor. Constructs a map decorator that results in entries NEVER expiring. | |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final PassiveExpiringMap. Construct a map decorator using the given expiration policy to determine expiration times.
| |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final PassiveExpiringMap. Construct a map decorator that decorates the given map and uses the given expiration policy to determine expiration times. If there are any elements already in the map being decorated, they will NEVER expire unless they are replaced.
| |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final long timeToLiveMillis) Construct a map decorator that decorates the given map using the given
time-to-live value measured in milliseconds to create and use a
| |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final long timeToLiveMillis, final Map<K, V> map) Construct a map decorator using the given time-to-live value measured in
milliseconds to create and use a
| |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final long timeToLive, final TimeUnit timeUnit) Construct a map decorator using the given time-to-live value measured in
the given time units of measure to create and use a
| |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final long timeToLive, final TimeUnit timeUnit, final Map<K, V> map) Construct a map decorator that decorates the given map using the given
time-to-live value measured in the given time units of measure to create
| |
| PassiveExpiringMap | back to summary |
|---|---|
| public PassiveExpiringMap(final Map<K, V> map) Constructs a map decorator that decorates the given map and results in entries NEVER expiring. If there are any elements already in the map being decorated, they also will NEVER expire.
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides org. Implements java. Normal
| |
| containsKey | back to summary |
|---|---|
| public boolean containsKey(final Object key) Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to determining the
contains result.
Returns | |
| containsValue | back to summary |
|---|---|
| public boolean containsValue(final Object value) Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to determining the
contains result.
Returns | |
| entrySet | back to summary |
|---|---|
| public Set Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to returning the entry set.
Returns a | |
| get | back to summary |
|---|---|
| public V get(final Object key) Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to returning the entry value.
Returns the value to which the specified key is mapped,
or More formally, if this map contains a mapping from a key
If this map permits null values, then a return value of
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to determining if it is empty.
Returns
| |
| isExpired | back to summary |
|---|---|
| private boolean isExpired(final long now, final Long expirationTimeObject) Determines if the given expiration time is less than
| |
| keySet | back to summary |
|---|---|
| public Set Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to returning the key set.
Returns a | |
| now | back to summary |
|---|---|
| private long now() The current time in milliseconds. | |
| put | back to summary |
|---|---|
| public V put(final K key, final V value) Overrides org. Implements java. Doc from java. Add the given key-value pair to this map as well as recording the entry's expiration time based on
the current time in milliseconds and this map's
Associates the specified value with the specified key in this map
(optional operation). If the map previously contained a mapping for
the key, the old value is replaced by the specified value. (A map
| |
| putAll | back to summary |
|---|---|
| public void putAll(final Map<? extends K, ? extends V> mapToCopy) Overrides org. Implements java. Doc from java. Copies all of the mappings from the specified map to this map
(optional operation). The effect of this call is equivalent to that
of calling | |
| readObject | back to summary |
|---|---|
| private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException Read the map in using a custom routine.
| |
| remove | back to summary |
|---|---|
| public V remove(final Object key) Overrides org. Implements java. Doc from java. Normal Returns the value to which this map previously associated the key,
or If this map permits null values, then a return value of
The map will not contain a mapping for the specified key once the call returns. | |
| removeAllExpired | back to summary |
|---|---|
| private void removeAllExpired(final long now) Removes all entries in the map whose expiration time is less than
| |
| removeIfExpired | back to summary |
|---|---|
| private void removeIfExpired(final Object key, final long now) Removes the entry with the given key if the entry's expiration time is
less than | |
| size | back to summary |
|---|---|
| public int size() Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to returning the size.
Returns the number of key-value mappings in this map. If the
map contains more than
| |
| validateAndConvertToMillis | back to summary |
|---|---|
| private static long validateAndConvertToMillis(final long timeToLive, final TimeUnit timeUnit) First validate the input parameters. If the parameters are valid, convert the given time measured in the given units to the same time measured in milliseconds.
| |
| values | back to summary |
|---|---|
| public Collection Overrides org. Implements java. Doc from java. All expired entries are removed from the map prior to returning the value collection.
Returns a
| |
| writeObject | back to summary |
|---|---|
| private void writeObject(final ObjectOutputStream out) throws IOException Write the map out using a custom routine.
| |
ExpirationPolicy
that returns a expiration time that is a
constant about of time in the future from the current time.
| Modifier and Type | Field and Description |
|---|---|
| private static final long | serialVersionUID
Serialization version |
| private final long | timeToLiveMillis
the constant time-to-live value measured in milliseconds. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | ConstantTimeToLiveExpirationPolicy(final long
the constant amount of time (in milliseconds)
an entry is available before it expires. A negative value
results in entries that NEVER expire. A zero value results in
entries that ALWAYS expire. timeToLiveMillis)Construct a policy with the given time-to-live constant measured in milliseconds. |
| public | ConstantTimeToLiveExpirationPolicy(final long
the constant amount of time an entry is available
before it expires. A negative value results in entries that
NEVER expire. A zero value results in entries that ALWAYS
expire. timeToLive, final TimeUnit the unit of time for the timeUnit)timeToLive
parameter, must not be null.Construct a policy with the given time-to-live constant measured in the given time unit of measure. |
| Modifier and Type | Method and Description |
|---|---|
| public long | Returns: iftimeToLiveMillis ≥ 0, an expiration time of
timeToLiveMillis +
System#currentTimeMillis() is returned. Otherwise, -1
is returned indicating the entry never expires.the key for the entry (ignored). key, final V the value for the entry (ignored). value)Implements org. |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version | |
| timeToLiveMillis | back to summary |
|---|---|
| private final long timeToLiveMillis the constant time-to-live value measured in milliseconds. | |
| ConstantTimeToLiveExpirationPolicy | back to summary |
|---|---|
| public ConstantTimeToLiveExpirationPolicy() Default constructor. Constructs a policy using a negative time-to-live value that results in entries never expiring. | |
| ConstantTimeToLiveExpirationPolicy | back to summary |
|---|---|
| public ConstantTimeToLiveExpirationPolicy(final long timeToLiveMillis) Construct a policy with the given time-to-live constant measured in milliseconds. A negative time-to-live value indicates entries never expire. A zero time-to-live value indicates entries expire (nearly) immediately.
| |
| ConstantTimeToLiveExpirationPolicy | back to summary |
|---|---|
| public ConstantTimeToLiveExpirationPolicy(final long timeToLive, final TimeUnit timeUnit) Construct a policy with the given time-to-live constant measured in the given time unit of measure.
| |
| expirationTime | back to summary |
|---|---|
| public long expirationTime(final K key, final V value) Implements org. Determine the expiration time for the given key-value entry.
| |
| Modifier and Type | Method and Description |
|---|---|
| public long | Returns: the expiration time value measured in milliseconds. A negative return value indicates the entry never expires.the key for the entry. key, V the value for the entry. value)Determine the expiration time for the given key-value entry. |
| expirationTime | back to summary |
|---|---|
| public long expirationTime(K key, V value) Determine the expiration time for the given key-value entry.
| |