Class LimitedMap<K,V>
java.lang.Object
it.polimi.ingsw.gc14.LimitedMap<K,V>
- Type Parameters:
K- the type of keys maintained by this map.V- the type of mapped values.
- All Implemented Interfaces:
Map<K,V>
A
ConcurrentHashMap-backed map with a configurable size limit and an associated action.
When the number of elements reaches or exceeds the limit, the specified action is automatically triggered.
This implementation is thread-safe for put; callers that need compound operations must synchronize externally.-
Nested Class Summary
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate RunnableThe action to execute when the map size reaches or exceeds the limit.private intThe maximum number of elements allowed in the map before the action is triggered.private final ConcurrentHashMap<K, V> The underlying thread-safe hash map storing all key-value pairs. -
Constructor Summary
ConstructorsConstructorDescriptionLimitedMap(int limit, Runnable action) Creates a newLimitedMapwith the specified limit and action. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()booleancontainsKey(Object key) booleancontainsValue(Object value) entrySet()booleanisEmpty()keySet()Associates the specified value with the specified key in this map.voidvoidSets a new action to execute when the map size reaches or exceeds the limit.voidsetLimit(int num) Sets a new size limit for this map.intsize()values()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
map
The underlying thread-safe hash map storing all key-value pairs. -
limit
private volatile int limitThe maximum number of elements allowed in the map before the action is triggered. -
action
The action to execute when the map size reaches or exceeds the limit.
-
-
Constructor Details
-
LimitedMap
Creates a newLimitedMapwith the specified limit and action.- Parameters:
limit- the maximum number of elements before the action is triggered.action- the action to execute when the limit is reached.
-
-
Method Details
-
put
Associates the specified value with the specified key in this map. If the map size reaches or exceeds the limit after the insertion, the configured action is triggered. -
remove
-
get
-
containsKey
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
size
-
isEmpty
-
putAll
-
clear
-
keySet
-
values
-
entrySet
-
setLimit
public void setLimit(int num) Sets a new size limit for this map.- Parameters:
num- the new limit.
-
setAction
Sets a new action to execute when the map size reaches or exceeds the limit.- Parameters:
action- the new action to set; must not benull.
-