Package org.apache.mina.util
Class ExpiringMap<K,V>
- java.lang.Object
-
- org.apache.mina.util.ExpiringMap<K,V>
-
- Type Parameters:
K
- The key typeV
- The value type
- All Implemented Interfaces:
Map<K,V>
public class ExpiringMap<K,V> extends Object implements Map<K,V>
A map with expiration. This class contains a worker thread that will periodically check this class in order to determine if any objects should be removed based on the provided time-to-live value.- Author:
- Apache MINA Project
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
ExpiringMap.Expirer
A Thread that monitors anExpiringMap
and will remove elements that have passed the threshold.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_EXPIRATION_INTERVAL
The default value, 1 secondstatic int
DEFAULT_TIME_TO_LIVE
The default value, 60 seconds
-
Constructor Summary
Constructors Constructor Description ExpiringMap()
Creates a new instance of ExpiringMap using the default values DEFAULT_TIME_TO_LIVE and DEFAULT_EXPIRATION_INTERVALExpiringMap(int timeToLive)
Creates a new instance of ExpiringMap using the supplied time-to-live value and the default value for DEFAULT_EXPIRATION_INTERVALExpiringMap(int timeToLive, int expirationInterval)
Creates a new instance of ExpiringMap using the supplied values and aConcurrentHashMap
for the internal data structure.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addExpirationListener(ExpirationListener<V> listener)
Adds a listener in the expiration listenersvoid
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
boolean
equals(Object obj)
V
get(Object key)
int
getExpirationInterval()
Get the interval in which an object will live in the map before it is removed.ExpiringMap.Expirer
getExpirer()
int
getTimeToLive()
int
hashCode()
boolean
isEmpty()
Set<K>
keySet()
V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> inMap)
V
remove(Object key)
void
removeExpirationListener(ExpirationListener<V> listener)
Removes a listener from the expiration listenersvoid
setExpirationInterval(int expirationInterval)
Set the interval in which an object will live in the map before it is removed.void
setTimeToLive(int timeToLive)
Update the value for the time-to-liveint
size()
Collection<V>
values()
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
DEFAULT_TIME_TO_LIVE
public static final int DEFAULT_TIME_TO_LIVE
The default value, 60 seconds- See Also:
- Constant Field Values
-
DEFAULT_EXPIRATION_INTERVAL
public static final int DEFAULT_EXPIRATION_INTERVAL
The default value, 1 second- See Also:
- Constant Field Values
-
-
Constructor Detail
-
ExpiringMap
public ExpiringMap()
Creates a new instance of ExpiringMap using the default values DEFAULT_TIME_TO_LIVE and DEFAULT_EXPIRATION_INTERVAL
-
ExpiringMap
public ExpiringMap(int timeToLive)
Creates a new instance of ExpiringMap using the supplied time-to-live value and the default value for DEFAULT_EXPIRATION_INTERVAL- Parameters:
timeToLive
- The time-to-live value (seconds)
-
ExpiringMap
public ExpiringMap(int timeToLive, int expirationInterval)
Creates a new instance of ExpiringMap using the supplied values and aConcurrentHashMap
for the internal data structure.- Parameters:
timeToLive
- The time-to-live value (seconds)expirationInterval
- The time between checks to see if a value should be removed (seconds)
-
-
Method Detail
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<K,V>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
-
hashCode
public int hashCode()
-
equals
public boolean equals(Object obj)
-
addExpirationListener
public void addExpirationListener(ExpirationListener<V> listener)
Adds a listener in the expiration listeners- Parameters:
listener
- The listener to add
-
removeExpirationListener
public void removeExpirationListener(ExpirationListener<V> listener)
Removes a listener from the expiration listeners- Parameters:
listener
- The listener to remove
-
getExpirer
public ExpiringMap.Expirer getExpirer()
- Returns:
- The Expirer instance
-
getExpirationInterval
public int getExpirationInterval()
Get the interval in which an object will live in the map before it is removed.- Returns:
- The expiration time in second
-
getTimeToLive
public int getTimeToLive()
- Returns:
- the Time-to-live value in seconds.
-
setExpirationInterval
public void setExpirationInterval(int expirationInterval)
Set the interval in which an object will live in the map before it is removed.- Parameters:
expirationInterval
- The expiration time in seconds
-
setTimeToLive
public void setTimeToLive(int timeToLive)
Update the value for the time-to-live- Parameters:
timeToLive
- The time-to-live (seconds)
-
-