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>

public class LimitedMap<K,V> extends Object implements 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.
  • Field Details

    • map

      private final ConcurrentHashMap<K,V> map
      The underlying thread-safe hash map storing all key-value pairs.
    • limit

      private volatile int limit
      The maximum number of elements allowed in the map before the action is triggered.
    • action

      private volatile Runnable action
      The action to execute when the map size reaches or exceeds the limit.
  • Constructor Details

    • LimitedMap

      public LimitedMap(int limit, Runnable action)
      Creates a new LimitedMap with 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

      public V put(K key, V value)
      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.
      Specified by:
      put in interface Map<K,V>
      Parameters:
      key - the key with which the specified value is to be associated.
      value - the value to be associated with the specified key.
      Returns:
      the previous value associated with the key, or null if there was no mapping.
    • remove

      public V remove(Object key)
      Specified by:
      remove in interface Map<K,V>
    • get

      public V get(Object key)
      Specified by:
      get in interface Map<K,V>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<K,V>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<K,V>
    • size

      public int size()
      Specified by:
      size in interface Map<K,V>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<K,V>
    • putAll

      public void putAll(Map<? extends K, ? extends V> m)
      Specified by:
      putAll in interface Map<K,V>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<K,V>
    • keySet

      public Set<K> keySet()
      Specified by:
      keySet in interface Map<K,V>
    • values

      public Collection<V> values()
      Specified by:
      values in interface Map<K,V>
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
    • setLimit

      public void setLimit(int num)
      Sets a new size limit for this map.
      Parameters:
      num - the new limit.
    • setAction

      public void setAction(Runnable action)
      Sets a new action to execute when the map size reaches or exceeds the limit.
      Parameters:
      action - the new action to set; must not be null.