K - Key typeV - Value typepublic class ExpiringMap<K,V> extends Object implements ConcurrentMap<K,V>
Entries are tracked by expiration time and expired by a single Timer thread.
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 O(n). When variable expiration is enabled, put/remove operations impose an O(log n) cost.
Example usages:
Map<String, Integer> map = ExpiringMap.create();
Map<String, Integer> map = ExpiringMap.builder().expiration(30, TimeUnit.SECONDS).build();
Map<String, Connection> map = ExpiringMap.builder()
.expiration(10, TimeUnit.MINUTES)
.entryLoader(new EntryLoader<String, Connection>() {
public Connection load(String address) {
return new Connection(address);
}
})
.expirationListener(new ExpirationListener<String, Connection>() {
public void expired(String key, Connection connection) {
connection.close();
}
})
.build();
| Modifier and Type | Class and Description |
|---|---|
static class |
ExpiringMap.Builder<K,V>
Builds ExpiringMap instances.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addExpirationListener(ExpirationListener<K,V> listener)
Adds an expiration listener.
|
static ExpiringMap.Builder<Object,Object> |
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 an expiration of 60 seconds.
|
Set<Map.Entry<K,V>> |
entrySet() |
boolean |
equals(Object obj) |
V |
get(Object key) |
long |
getExpectedExpiration(K key)
Gets the expected expiration in milliseconds for the entry corresponding to the given 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,
ExpirationPolicy expirationPolicy) |
V |
put(K key,
V value,
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 |
putIfAbsent(K key,
V value) |
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
void |
removeExpirationListener(ExpirationListener<K,V> listener)
Removes an expiration listener.
|
V |
replace(K key,
V value) |
boolean |
replace(K key,
V oldValue,
V newValue) |
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(ExpirationPolicy expirationPolicy)
Sets the global expiration policy for the map.
|
void |
setExpirationPolicy(K key,
ExpirationPolicy expirationPolicy)
Sets the expiration policy for the entry corresponding to the given key.
|
int |
size() |
String |
toString() |
Collection<V> |
values() |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAllpublic static ExpiringMap.Builder<Object,Object> builder()
public static <K,V> ExpiringMap<K,V> create()
public void addExpirationListener(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 boolean equals(Object obj)
public long getExpiration()
public long getExpiration(K key)
key - NullPointerException - if key is nullNoSuchElementException - If no entry exists for the given keypublic long getExpectedExpiration(K key)
key - NullPointerException - if key is nullNoSuchElementException - 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 - if key is nullpublic V put(K key, V value, ExpirationPolicy expirationPolicy)
public V put(K key, V value, 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 - the unit that duration is expressed inUnsupportedOperationException - If variable expiration is not enabledNullPointerException - if key, expirationPolicy or timeUnit are nullpublic V putIfAbsent(K key, V value)
putIfAbsent in interface ConcurrentMap<K,V>putIfAbsent in interface Map<K,V>public void removeExpirationListener(ExpirationListener<K,V> listener)
listener - NullPointerException - if listener is nullpublic void resetExpiration(K key)
key.key - to reset expiration forNullPointerException - if key is nullpublic 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 - the unit that duration is expressed inNullPointerException - if key or timeUnit are nullUnsupportedOperationException - 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 - the unit that duration is expressed inNullPointerException - timeUnit is nullUnsupportedOperationException - If variable expiration is not enabledpublic void setExpirationPolicy(ExpirationPolicy expirationPolicy)
expirationPolicy - NullPointerException - expirationPolicy is nullpublic void setExpirationPolicy(K key, ExpirationPolicy expirationPolicy)
key - to set policy forexpirationPolicy - to setNullPointerException - if key or expirationPolicy are nullUnsupportedOperationException - If variable expiration is not enabledCopyright © 2015. All rights reserved.