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 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 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>
Builder object for an
ExpiringMap . |
static class |
ExpiringMap.ExpirationPolicy
Map entry expiration policy.
|
Modifier and Type | Method and Description |
---|---|
void |
addExpirationHandler(BiConsumer<K,V> listener)
Adds a
BiConsumer which will be applied to any entries upon
expiry |
static <K,V> ExpiringMap.Builder<K,V> |
builder()
Returns a new
ExpiringMap.Builder for this ExpiringMap |
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()
Note this operation manually maps internal expiring entries upon each
usage of this method.
|
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)
Inserts an entry to this map with a specific
ExpiringMap.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)
Inserts an entry with a specific expiration time
|
void |
putAll(Map<? extends K,? extends V> map)
Inserts all values from another map into this one
|
V |
putIfAbsent(K key,
V value) |
V |
remove(Object key) |
boolean |
remove(Object key,
Object value) |
void |
removeExpirationHandler(BiConsumer<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(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() |
String |
toString() |
Collection<V> |
values()
Note this method maps internal entries into values upon each specific
call.
|
Iterator<V> |
valuesIterator()
Returns an iterator over the map values.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
public static <K,V> ExpiringMap.Builder<K,V> builder()
ExpiringMap.Builder
for this ExpiringMap
K
- The type of the keysV
- The type of the valuespublic static <K,V> ExpiringMap<K,V> create()
K
- The type of the keys for this mapV
- The type of the values for this mapExpiringMap
public void addExpirationHandler(BiConsumer<K,V> listener)
BiConsumer
which will be applied to any entries upon
expirylistener
- The BiConsumer
to applyIllegalArgumentException
- 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()
if possiblepublic boolean equals(Object obj)
public long getExpiration()
public long getExpiration(K key)
key
- The key to check the expiration forpublic 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 keyIllegalArgumentException
- on null keypublic V put(K key, V value, ExpiringMap.ExpirationPolicy expirationPolicy)
ExpiringMap.ExpirationPolicy
key
- to put value forvalue
- to put for keyexpirationPolicy
- The ExpiringMap.ExpirationPolicy
for this entryput(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 keyexpirationPolicy
- The ExpiringMap.ExpirationPolicy
for this entryduration
- 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
enabledIllegalArgumentException
- on null key or timeUnitpublic V put(K key, V value, long duration, TimeUnit timeUnit)
key
- Key to use in mapping to a valuevalue
- Value being mapped toduration
- The duration before this entry expirestimeUnit
- The unit of time for this duration
null
if none existed or it was nullput(Object, Object, ExpirationPolicy, long, TimeUnit)
public void putAll(Map<? extends K,? extends V> map)
putAll
in interface Map<K,V>
map
- A Map
to insert into this ExpiringMap
put(Object, Object)
public V putIfAbsent(K key, V value)
putIfAbsent
in interface ConcurrentMap<K,V>
putIfAbsent
in interface Map<K,V>
public void removeExpirationHandler(BiConsumer<K,V> listener)
listener
- The BiConsumer
to removepublic 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
- 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
- the unit that duration
is expressed inpublic void setExpirationPolicy(ExpiringMap.ExpirationPolicy expirationPolicy)
expirationPolicy
- The new ExpiringMap.ExpirationPolicy
for all entriespublic void setExpirationPolicy(K key, ExpiringMap.ExpirationPolicy expirationPolicy)
key
- to set policy forexpirationPolicy
- to setUnsupportedOperationException
- If variable expiration is not
enabledpublic Collection<V> values()
valuesIterator()
if possiblevalues
in interface Map<K,V>
Collection
of all values in this mappublic Iterator<V> valuesIterator()
Iterator
of values for this mapConcurrentModificationException
- if the map's size changes while
iterating, excluding calls to Iterator.remove()
.Copyright © 2015. All rights reserved.