Fix: full FA refactor

This commit is contained in:
2026-06-14 13:49:54 +02:00
parent 3888529b96
commit 6c070e1ce5
5 changed files with 37 additions and 25 deletions
@@ -1,15 +1,14 @@
package it.polimi.ingsw.gc14;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* A {@link LinkedHashMap} with a configurable size limit and an associated action.
* A {@link 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.
* This implementation is thread-safe for {@code put}; callers that need compound operations must synchronize externally.
*
* @param <K> the type of keys maintained by this map.
* @param <V> the type of mapped values.
@@ -50,8 +49,8 @@ public class LimitedMap<K, V> implements Map<K, V> {
@Override
public synchronized V put(K key, V value) {
boolean added = true;
if(map.size()==limit) {
if(!map.containsKey(key))
if (map.size() == limit) {
if (!map.containsKey(key))
return null;
added = false;
}
@@ -63,7 +62,7 @@ public class LimitedMap<K, V> implements Map<K, V> {
}
@Override
public V remove(Object key) { return map.remove(key); }
public synchronized V remove(Object key) { return map.remove(key); }
@Override
public V get(Object key) { return map.get(key); }
@@ -112,7 +111,10 @@ public class LimitedMap<K, V> implements Map<K, V> {
/**
* Sets a new action to execute when the map size reaches or exceeds the limit.
*
* @param action the new action to set.
* @param action the new action to set; must not be {@code null}.
*/
public void setAction(Runnable action) { this.action = action; }
public void setAction(Runnable action) {
if (action == null) throw new IllegalArgumentException("action must not be null");
this.action = action;
}
}