Server/Server/src/main/java/uulm/teamname/marvelous/server/game/pipelining/Packet.java

82 lines
2.4 KiB
Java

package uulm.teamname.marvelous.server.game.pipelining;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.net.Client;
import java.util.*;
/**
* The {@link Packet} contains all {@link Request Requests} and the belonging {@link Client}. You can add and
* remove {@link Request Requests} at will.
*/
public class Packet extends ArrayList<Request> {
private final Client origin;
public Packet(Request[] requests, Client origin) {
this.origin = origin;
addAll(Arrays.asList(requests));
}
/**
* This method checks if a {@link RequestType} is part of the {@link Packet} or not
*
* @param requiredType RequestType to check.
* @return boolean
*/
public boolean containsRequestOfType(RequestType requiredType) {
for (Request request : this) {
if (request.type == requiredType) {
return true;
}
}
return false;
}
/**
* This method removes all {@link Request Requests} of given {@link RequestType types} from the {@link Packet}.
*
* @param types RequestType(s) to remove
*/
public void removeRequestsOfTypes(RequestType... types) {
var listOfTypes = Arrays.asList(types);
this.removeIf(request -> listOfTypes.contains(request.type));
}
/**
* This method removes all {@link Request Requests} except of given {@link RequestType types} from the {@link
* Packet}.
*
* @param types RequestType(s) to not remove
*/
public void removeRequestsNotOfTypes(RequestType... types) {
var listOfTypes = Arrays.asList(types);
this.removeIf(request -> !listOfTypes.contains(request.type));
}
public Client getOrigin() {
return origin;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
Packet packet = (Packet) o;
return Objects.equals(origin, packet.origin);
}
@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public Object clone() {
return new Packet(this.toArray(new Request[0]), this.origin);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), origin);
}
}