Class CapacityLimitedHashtable<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- java.util.concurrent.ConcurrentHashMap<K,V>
-
- com.snowbound.common.utils.CapacityLimitedHashtable<K,V>
-
- Type Parameters:
K
- the map key typeV
- the map value type
- All Implemented Interfaces:
java.io.Serializable
,java.util.concurrent.ConcurrentMap<K,V>
,java.util.Map<K,V>
- Direct Known Subclasses:
LRUByteLimitedHashtable
,LRUHashtable
public abstract class CapacityLimitedHashtable<K,V> extends java.util.concurrent.ConcurrentHashMap<K,V>
Map that will eject its least-recently-accessed values to remain within a specified capacity.
The type of capacity is determined by implementations of this abstract class's getObjectSize method - for example, an implementation that returns 1 would count capacity as the number of items in the map, while a different implementation might return the object's byte size and make capacity a maximum amount of memory.
Useful for caching.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ConcurrentHashMap
java.util.concurrent.ConcurrentHashMap.KeySetView<K extends java.lang.Object,V extends java.lang.Object>
-
-
Constructor Summary
Constructors Constructor Description CapacityLimitedHashtable(long capacity)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
currentSize()
Return the current size of all of the table's values as judged by the implementation of getObjectSize.V
get(java.lang.Object key)
Retrieve a value from the map.abstract long
getObjectSize(V obj)
Return the size this object will be inside of this hashtable.V
put(K key, V value)
Maps the specified key to the specified value in this table.V
remove(java.lang.Object key)
Removes the key (and its corresponding value) from this map.-
Methods inherited from class java.util.concurrent.ConcurrentHashMap
clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, merge, newKeySet, newKeySet, putAll, putIfAbsent, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, remove, replace, replace, replaceAll, search, searchEntries, searchKeys, searchValues, size, toString, values
-
-
-
-
Method Detail
-
currentSize
public int currentSize()
Return the current size of all of the table's values as judged by the implementation of getObjectSize.- Returns:
- size of the values in the table
-
getObjectSize
public abstract long getObjectSize(V obj)
Return the size this object will be inside of this hashtable. The implementation of this method will determine how the hash's capacity will work; for example, returning 1 for every object will make the capacity a simple length limit, while returning the object's byte size will make capacity a memory size limitation.
- Parameters:
obj
- An object to size- Returns:
- the size of the object (unit will vary by implementation)
-
remove
public V remove(java.lang.Object key)
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map.- Specified by:
remove
in interfacejava.util.Map<K,V>
- Overrides:
remove
in classjava.util.concurrent.ConcurrentHashMap<K,V>
- Parameters:
key
- the key that needs to be removed- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
- Throws:
java.lang.NullPointerException
- if the specified key is null
-
put
public V put(K key, V value)
Maps the specified key to the specified value in this table. Neither the key nor the value can be null.
If adding this value puts the table above its maximum capacity, objects that were accessed least recently will be removed from the table until it is back at capacity.
- Specified by:
put
in interfacejava.util.Map<K,V>
- Overrides:
put
in classjava.util.concurrent.ConcurrentHashMap<K,V>
- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
- Throws:
java.lang.NullPointerException
- if the specified key or value is null- See Also:
ConcurrentHashMap.put(java.lang.Object, java.lang.Object)
-
get
public V get(java.lang.Object key)
Retrieve a value from the map.
-
-