fix: implemented hashCode that persists for lifetime of object

This commit is contained in:
Yannik Bretschneider 2021-06-07 01:45:03 +02:00
parent 4ace772be0
commit f3e3f9f7d7

View File

@ -11,6 +11,9 @@ public class Participant {
/** The Websocket to contact the participant with */ /** The Websocket to contact the participant with */
private WebSocket connection; private WebSocket connection;
/** The ID of the device that the client provided at connect */
public final String deviceID;
public final String name; public final String name;
/** The type (as in role) of participant */ /** The type (as in role) of participant */
@ -20,10 +23,18 @@ public class Participant {
// public final boolean AI; // public final boolean AI;
/** Creates a new {@link Participant} */ /** 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.connection = connection;
this.type = type; 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; this.name = name;
} }
@ -33,7 +44,7 @@ public class Participant {
} }
/** Sets the connection {@link WebSocket} for the current participant */ /** Sets the connection {@link WebSocket} for the current participant */
public void setConnection(WebSocket connection) { void setConnection(WebSocket connection) {
if (this.connection != null) { if (this.connection != null) {
Logger.warn("Overriding connection of active participant {}, which seems invalid", this.name); Logger.warn("Overriding connection of active participant {}, which seems invalid", this.name);
} }
@ -47,23 +58,29 @@ public class Participant {
this.connection = null; this.connection = null;
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Participant that = (Participant) o; 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 @Override
public int hashCode() { public int hashCode() {
return Objects.hash(connection, name, type); if (hashCode == null) hashCode = Objects.hash(connection, deviceID, name, type);
return hashCode;
} }
@Override @Override
public String toString() { public String
toString() {
return "Participant{" + return "Participant{" +
"connection=" + connection + "connection=" + connection +
", deviceID='" + deviceID + '\'' +
", name='" + name + '\'' + ", name='" + name + '\'' +
", type=" + type + ", type=" + type +
'}'; '}';