K - Key typeV - Value typepublic class ExpiringMap<K,V> extends Object implements Map<K,V>
Entries are tracked by expiration time and expired by a single static Timer.
Expiration listeners will automatically be assigned to run in the context of the Timer thread or in a separate thread based on their first timed duration.
When variable expiration is disabled (default), put/remove operations are constant. When variable expiration is enabled, put/remove operations impose a log(n) cost.
Example usages:
Mapmap = ExpiringMap.create(); Map map = ExpiringMap.builder().expiration(30, TimeUnit.SECONDS).build(); Map map = ExpiringMap.builder() .expiration(10, TimeUnit.MINUTES) .expirationListener(new ExpirationListener () { public void expired(String key, Connection connection) { connection.close(); }) .build();
| Modifier and Type | Class and Description |
|---|---|
static class |
ExpiringMap.Builder
Builds ExpiringMap instances.
|
static interface |
ExpiringMap.ExpirationListener<K,V>
A listener for expired object events.
|
static class |
ExpiringMap.ExpirationPolicy
Map entry expiration policy.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addExpirationListener(ExpiringMap.ExpirationListener<K,V> listener)
Adds an expiration listener.
|
static ExpiringMap.Builder |
builder()
Creates an ExpiringMap builder.
|
void |
clear() |
boolean |
containsKey(Object key) |
boolean |
containsValue(Object value) |
static <K,V> ExpiringMap<K,V> |
create()
Creates a new instance of ExpiringMap with ExpirationPolicy.CREATED and expiration duration of
60 TimeUnit.SECONDS.
|
Set<Map.Entry<K,V>> |
entrySet()
Not currently supported.
|
boolean |
equals(Object obj) |
V |
get(Object key) |
long |
getExpiration()
Returns the map's default expiration duration in milliseconds.
|
long |
getExpiration(K key)
Gets the expiration duration in milliseconds for the entry corresponding to the given key.
|
int |
hashCode() |
boolean |
isEmpty() |
Set<K> |
keySet() |
V |
put(K key,
V value)
Puts
value in the map for key. |
V |
put(K key,
V value,
ExpiringMap.ExpirationPolicy expirationPolicy) |
V |
put(K key,
V value,
ExpiringMap.ExpirationPolicy expirationPolicy,
long duration,
TimeUnit timeUnit)
Puts
value in the map for key. |
V |
put(K key,
V value,
long duration,
TimeUnit timeUnit) |
void |
putAll(Map<? extends K,? extends V> map) |
V |
remove(Object key) |
void |
removeExpirationListener(ExpiringMap.ExpirationListener<K,V> listener)
Removes an expiration listener.
|
void |
resetExpiration(K key)
Resets expiration for the entry corresponding to
key. |
void |
setExpiration(K key,
long duration,
TimeUnit timeUnit)
Sets the expiration duration for the entry corresponding to the given key.
|
void |
setExpiration(long duration,
TimeUnit timeUnit)
Updates the default map entry expiration.
|
void |
setExpirationPolicy(ExpiringMap.ExpirationPolicy expirationPolicy)
Sets the global expiration policy for the map.
|
void |
setExpirationPolicy(K key,
ExpiringMap.ExpirationPolicy expirationPolicy)
Sets the expiration policy for the entry corresponding to the given key.
|
int |
size() |
Collection<V> |
values()
Not currently supported.
|
Iterator<V> |
valuesIterator()
Returns an iterator over the map values.
|
public static ExpiringMap.Builder builder()
public static <K,V> ExpiringMap<K,V> create()
public void addExpirationListener(ExpiringMap.ExpirationListener<K,V> listener)
listener - to addNullPointerException - if listener is nullpublic boolean containsKey(Object key)
containsKey in interface Map<K,V>public boolean containsValue(Object value)
containsValue in interface Map<K,V>public Set<Map.Entry<K,V>> entrySet()
keySet() and this#entrySetIterable() instead.entrySet in interface Map<K,V>UnsupportedOperationExceptionpublic boolean equals(Object obj)
public long getExpiration()
public long getExpiration(K key)
key - NoSuchElementException - If no entry exists for the given keypublic int hashCode()
public V put(K key, V value)
value in the map for key. Resets the entry's expiration unless an entry
already exists for the same key and value.put in interface Map<K,V>key - to put value forvalue - to put for keyNullPointerException - on null keypublic V put(K key, V value, ExpiringMap.ExpirationPolicy expirationPolicy)
#put(Object, Object, ExpirationPolicy, long, TimeUnit)}public V put(K key, V value, ExpiringMap.ExpirationPolicy expirationPolicy, long duration, TimeUnit timeUnit)
value in the map for key. Resets the entry's expiration unless an entry
already exists for the same key and value. Requires that variable expiration be
enabled.key - Key to put value forvalue - Value to put for keyduration - the length of time after an entry is created that it should be removedtimeUnit - unit the unit that duration is expressed inUnsupportedOperationException - If variable expiration is not enabledNullPointerException - on null key or timeUnitpublic V put(K key, V value, long duration, TimeUnit timeUnit)
#put(Object, Object, ExpirationPolicy, long, TimeUnit)}public void removeExpirationListener(ExpiringMap.ExpirationListener<K,V> listener)
listener - public void resetExpiration(K key)
key.key - to reset expiration forpublic void setExpiration(K key, long duration, TimeUnit timeUnit)
key - Key to set expiration forduration - the length of time after an entry is created that it should be removedtimeUnit - unit the unit that duration is expressed inUnsupportedOperationException - If variable expiration is not enabledpublic void setExpiration(long duration,
TimeUnit timeUnit)
duration - the length of time after an entry is created that it should be removedtimeUnit - unit the unit that duration is expressed inpublic void setExpirationPolicy(ExpiringMap.ExpirationPolicy expirationPolicy)
expirationPolicy - public void setExpirationPolicy(K key, ExpiringMap.ExpirationPolicy expirationPolicy)
key - to set policy forexpirationPolicy - to setUnsupportedOperationException - If variable expiration is not enabledpublic Collection<V> values()
valuesIterator() instead.values in interface Map<K,V>UnsupportedOperationExceptionpublic Iterator<V> valuesIterator()
ConcurrentModificationException - if the map's size changes while iterating, excluding
calls to (Iteratorremove(Object).Copyright © 2014. All Rights Reserved.