Class PassiveExpiringMap<K,V>

Type Parameters:
K - the type of the keys in the map
V - the type of the values in the map
All Implemented Interfaces:
Serializable, Map<K,V>, Get<K,V>, IterableGet<K,V>, IterableMap<K,V>, Put<K,V>

public class PassiveExpiringMap<K,V> extends AbstractMapDecorator<K,V> implements Serializable
Decorates a 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 PassiveExpiringMap.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 Collections.synchronizedMap(Map). This class may throw exceptions when accessed by concurrent threads without synchronization.

Since:
4.0
Version:
$Id: PassiveExpiringMap.java 1686855 2015-06-22 13:00:27Z tn $
See Also:
  • Constructor Details

    • PassiveExpiringMap

      public PassiveExpiringMap()
      Default constructor. Constructs a map decorator that results in entries NEVER expiring.
    • PassiveExpiringMap

      public PassiveExpiringMap(PassiveExpiringMap.ExpirationPolicy<K,V> expiringPolicy)
      Construct a map decorator using the given expiration policy to determine expiration times.
      Parameters:
      expiringPolicy - the policy used to determine expiration times of entries as they are added.
      Throws:
      NullPointerException - if expiringPolicy is null
    • PassiveExpiringMap

      public PassiveExpiringMap(PassiveExpiringMap.ExpirationPolicy<K,V> expiringPolicy, Map<K,V> map)
      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.
      Parameters:
      expiringPolicy - the policy used to determine expiration times of entries as they are added.
      map - the map to decorate, must not be null.
      Throws:
      NullPointerException - if the map or expiringPolicy is null.
    • PassiveExpiringMap

      public PassiveExpiringMap(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.ConstantTimeToLiveExpirationPolicy expiration policy.
      Parameters:
      timeToLiveMillis - 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.
    • PassiveExpiringMap

      public PassiveExpiringMap(long timeToLiveMillis, Map<K,V> map)
      Construct a map decorator using the given time-to-live value measured in milliseconds to create and use a PassiveExpiringMap.ConstantTimeToLiveExpirationPolicy expiration policy. If there are any elements already in the map being decorated, they will NEVER expire unless they are replaced.
      Parameters:
      timeToLiveMillis - 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.
      map - the map to decorate, must not be null.
      Throws:
      NullPointerException - if the map is null.
    • PassiveExpiringMap

      public PassiveExpiringMap(long timeToLive, 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.ConstantTimeToLiveExpirationPolicy expiration policy.
      Parameters:
      timeToLive - 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.
      timeUnit - the unit of time for the timeToLive parameter, must not be null.
      Throws:
      NullPointerException - if the time unit is null.
    • PassiveExpiringMap

      public PassiveExpiringMap(long timeToLive, TimeUnit timeUnit, 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.ConstantTimeToLiveExpirationPolicy expiration policy. This policy is used to determine expiration times. If there are any elements already in the map being decorated, they will NEVER expire unless they are replaced.
      Parameters:
      timeToLive - 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.
      timeUnit - the unit of time for the timeToLive parameter, must not be null.
      map - the map to decorate, must not be null.
      Throws:
      NullPointerException - if the map or time unit is null.
    • PassiveExpiringMap

      public PassiveExpiringMap(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.
      Parameters:
      map - the map to decorate, must not be null.
      Throws:
      NullPointerException - if the map is null.
  • Method Details