K
- the key classV
- the value classpublic class AgeOutHashMap<K,V> extends Object
Note that this design uses a "passive" age-out model, meaning that aged-out entries are only detected and removed as a side-effect of interaction with the map. For example:
AgeOutHashMap<Foo, Bar> map = new AgeOutHashMap<Foo, Bar>(100); map.put(Foo.F1, Bar.B2); // .. wait longer than 100ms for age-out .. // At this point, the entry is still in the map, but is only (silently) // removed when we attempt to access it: Bar b = map.get(Foo.F1); assertNull(b);Entries in the map can be "actively" aged-out by invoking the
prune()
method.Modifier and Type | Field and Description |
---|---|
static long |
DEFAULT_AGE_OUT_MS
The default age-out (ms).
|
static long |
MIN_AGE_OUT_MS
The minimum age-out (ms) allowed.
|
Constructor and Description |
---|
AgeOutHashMap()
Constructs a map with the initial age-out set to the default
value of 5000L milliseconds.
|
AgeOutHashMap(long ageOutMs)
Constructs a map with the specified initial age-out value (ms).
|
AgeOutHashMap(long ageOutMs,
boolean ageOutDeadwoodOnly)
Constructs a map with the specified initial age-out value (ms).
|
Modifier and Type | Method and Description |
---|---|
protected void |
deadwood(Set<V> values)
Invoked when a set of values are silently removed from the map.
|
protected void |
deadwood(V value)
Invoked when a value is silently removed from the map.
|
V |
get(K key)
Returns the value in the map for the given key.
|
long |
getAgeOutMs()
Returns the current age-out value (ms).
|
int |
prune()
Prunes aged-out entries from the map, returning the true map size.
|
V |
put(K key,
V value)
Creates an entry in the map for the given key and value, returning the
old value if there was one; null otherwise.
|
V |
remove(K key)
Removes the entry from the map for the given key, returning its value.
|
void |
setAgeOut(long ageOutMs)
Sets a new age-out value (ms) on the map.
|
int |
size()
Returns the number of entries in the map.
|
String |
toString() |
boolean |
touch(K key)
Refreshes the timestamp for the map entry with the given key, if
it exists.
|
boolean |
touchOrPut(K key,
V value)
Refreshes the timestamp for the map entry with the given key, if it
exists, or creates a new entry.
|
public static final long DEFAULT_AGE_OUT_MS
public static final long MIN_AGE_OUT_MS
public AgeOutHashMap()
public AgeOutHashMap(long ageOutMs)
ageOutMs
- the initial age-out valueIllegalArgumentException
- if ageOutMs is less than
10Lpublic AgeOutHashMap(long ageOutMs, boolean ageOutDeadwoodOnly)
remove(Object)
, the
ageOutDeadwoodOnly
flag determines if
deadwood(Object)
is invoked.ageOutMs
- the initial age-out valueageOutDeadwoodOnly
- the age-out deadwood only flagIllegalArgumentException
- if ageOutMs is less than
10Lpublic long getAgeOutMs()
public void setAgeOut(long ageOutMs)
ageOutMs
- the new age-out value (ms)public int size()
prune()
first.public int prune()
public V put(K key, V value)
key
- the keyvalue
- the new valuepublic boolean touch(K key)
true
to indicate that the
entry exists and had its timestamp updated; false
if there
was no entry for the given key.key
- the keypublic boolean touchOrPut(K key, V value)
true
to
indicate that the entry already existed and had its timestamp updated;
false
if a new entry was created.key
- the keyvalue
- the valuepublic V get(K key)
key
- the keypublic V remove(K key)
deadwood(Object)
callback will be invoked if a value was
found and ageOutDeadwoodOnly
is not set.key
- the keyprotected void deadwood(V value)
This method is provided so that subclasses may override it and do something interesting with the value just removed.
value
- the value removed from the mapprotected void deadwood(Set<V> values)
This method is provided so that subclasses may override it and do something interesting with the values just removed.
values
- the values removed from the mapCopyright © 2016. All Rights Reserved.