Add: PING PONG TCP

This commit is contained in:
rubenpirreram
2026-05-07 18:09:34 +02:00
parent a2dd6f3d9b
commit 68be878ef1
@@ -0,0 +1,75 @@
package it.polimi.ingsw.gc14;
import java.util.ArrayList;
/**
* An {@link ArrayList} 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.
*
* @param <T> the type of elements held in this list.
*/
public class LimitedList<T> extends ArrayList<T> {
/**
* The maximum number of elements allowed in the list before the action is triggered.
*/
private int limit;
/**
* The action to execute when the list size reaches or exceeds the limit.
*/
private Runnable action;
/**
* Creates a new {@code LimitedList} 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) {
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.
*
* @param element the element to add.
* @return {@code true} if the element was successfully added.
*/
@Override
public boolean add(T element) {
boolean result = super.add(element);
if (size() >= limit) {
action.run();
}
return result;
}
/**
* Sets a new size limit for this list.
*
* @param num the new limit.
*/
public void setLimit(int num) {
this.limit = num;
}
/**
* Returns the current size limit of this list.
*
* @return the current limit.
*/
public int getLimit() {
return limit;
}
/**
* Sets a new action to execute when the list size reaches or exceeds the limit.
*
* @param action the new action to set.
*/
public void setAction(Runnable action) {
this.action = action;
}
}