Add: PING PONG TCP

This commit is contained in:
rubenpirreram
2026-05-07 18:09:39 +02:00
parent 68be878ef1
commit 7757b38155
10 changed files with 388 additions and 160 deletions
@@ -1,75 +1,111 @@
package it.polimi.ingsw.gc14;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
/**
* An {@link ArrayList} with a configurable size limit and an associated action.
* A {@link LinkedHashMap} 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.
*
* @param <T> the type of elements held in this list.
* @param <K> the type of keys maintained by this map.
* @param <V> the type of mapped values.
*/
public class LimitedList<T> extends ArrayList<T> {
public class LimitedMap<K, V> implements Map<K, V> {
private final LinkedHashMap<K, V> map = new LinkedHashMap<>();
/**
* The maximum number of elements allowed in the list before the action is triggered.
* The maximum number of elements allowed in the map before the action is triggered.
*/
private int limit;
private volatile int limit;
/**
* The action to execute when the list size reaches or exceeds the limit.
* The action to execute when the map size reaches or exceeds the limit.
*/
private Runnable action;
private volatile Runnable action;
/**
* Creates a new {@code LimitedList} with the specified limit and action.
* Creates a new {@code LimitedMap} with the specified limit and action.
*
* @param limit the maximum number of elements before the action is triggered.
* @param action the action to execute when the limit is reached.
*/
public LimitedList(int limit, Runnable action) {
public LimitedMap(int limit, Runnable action) {
this.limit = limit;
this.action = action;
}
/**
* Adds the specified element to the list.
* If the list size reaches or exceeds the limit after the insertion, the configured action is triggered.
* 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.
*
* @param element the element to add.
* @return {@code true} if the element was successfully added.
* @param key the key with which the specified value is to be associated.
* @param value the value to be associated with the specified key.
* @return the previous value associated with the key, or {@code null} if there was no mapping.
*/
@Override
public boolean add(T element) {
boolean result = super.add(element);
if (size() >= limit) {
public synchronized V put(K key, V value) {
V result = map.put(key, value);
if (map.size() >= limit) {
action.run();
}
return result;
}
@Override
public synchronized V remove(Object key) { return map.remove(key); }
@Override
public synchronized V get(Object key) { return map.get(key); }
@Override
public synchronized boolean containsKey(Object key) { return map.containsKey(key); }
@Override
public synchronized boolean containsValue(Object value) { return map.containsValue(value); }
@Override
public synchronized int size() { return map.size(); }
@Override
public synchronized boolean isEmpty() { return map.isEmpty(); }
@Override
public synchronized void putAll(Map<? extends K, ? extends V> m) { m.forEach(this::put); }
@Override
public synchronized void clear() { map.clear(); }
@Override
public synchronized Set<K> keySet() { return map.keySet(); }
@Override
public synchronized Collection<V> values() { return map.values(); }
@Override
public synchronized Set<Entry<K, V>> entrySet() { return map.entrySet(); }
/**
* Sets a new size limit for this list.
* Sets a new size limit for this map.
*
* @param num the new limit.
*/
public void setLimit(int num) {
this.limit = num;
}
public void setLimit(int num) { this.limit = num; }
/**
* Returns the current size limit of this list.
* Returns the current size limit of this map.
*
* @return the current limit.
*/
public int getLimit() {
return limit;
}
public int getLimit() { return limit; }
/**
* Sets a new action to execute when the list size reaches or exceeds the limit.
* Sets a new action to execute when the map size reaches or exceeds the limit.
*
* @param action the new action to set.
*/
public void setAction(Runnable action) {
this.action = action;
}
public void setAction(Runnable action) { this.action = action; }
}