From f3e3f9f7d7f0015bf280b2e591240766aee7ae1d Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Mon, 7 Jun 2021 01:45:03 +0200 Subject: [PATCH] fix: implemented hashCode that persists for lifetime of object --- .../server/lobbymanager/Participant.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java index 307f205..3d8794e 100644 --- a/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java +++ b/Server/src/main/java/uulm/teamname/marvelous/server/lobbymanager/Participant.java @@ -11,6 +11,9 @@ public class Participant { /** The Websocket to contact the participant with */ private WebSocket connection; + /** The ID of the device that the client provided at connect */ + public final String deviceID; + public final String name; /** The type (as in role) of participant */ @@ -20,10 +23,18 @@ public class Participant { // public final boolean AI; /** Creates a new {@link Participant} */ - public Participant (WebSocket connection, ParticipantType type, /* boolean AI,*/ String name) { + public Participant(WebSocket connection, ParticipantType type, String deviceID, String name) { this.connection = connection; this.type = type; - // this.AI = AI; + this.deviceID = deviceID; + this.name = name; + } + + /** Creates a new {@link Participant} */ + public Participant(WebSocket connection, ParticipantType type, String name) { + this.connection = connection; + this.type = type; + this.deviceID = ""; this.name = name; } @@ -33,7 +44,7 @@ public class Participant { } /** Sets the connection {@link WebSocket} for the current participant */ - public void setConnection(WebSocket connection) { + void setConnection(WebSocket connection) { if (this.connection != null) { Logger.warn("Overriding connection of active participant {}, which seems invalid", this.name); } @@ -47,23 +58,29 @@ public class Participant { this.connection = null; } + @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Participant that = (Participant) o; - return Objects.equals(connection, that.connection) && Objects.equals(name, that.name) && type == that.type; + return Objects.equals(connection, that.connection) && Objects.equals(deviceID, that.deviceID) && Objects.equals(name, that.name) && type == that.type; } + Integer hashCode; + @Override public int hashCode() { - return Objects.hash(connection, name, type); + if (hashCode == null) hashCode = Objects.hash(connection, deviceID, name, type); + return hashCode; } @Override - public String toString() { + public String + toString() { return "Participant{" + "connection=" + connection + + ", deviceID='" + deviceID + '\'' + ", name='" + name + '\'' + ", type=" + type + '}';