From d61b68aa41d00c736ec28e9e22342e26ff716ee8 Mon Sep 17 00:00:00 2001 From: Hanno Fleischer Date: Fri, 6 Dec 2024 01:15:06 +0100 Subject: [PATCH] overrode hachCode and equals method of Piece --- .../src/main/java/pp/mdga/game/Piece.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java b/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java index 3e7a32d1..d3eecd65 100644 --- a/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java +++ b/Projekte/mdga/model/src/main/java/pp/mdga/game/Piece.java @@ -131,4 +131,37 @@ public UUID getUuid() { public String toString() { return "Piece with UUID: %s and color: %s with state: %s".formatted(this.uuid, color, state); } + + /** + * This method will be used to create the hash code of this Piece class. + * + * @return hashCode as an Integer. + */ + @Override + public int hashCode() { + return this.uuid.hashCode(); + } + + /** + * This method will be used to check if the given obj parameter is equal to this object. + * It will return false if the obj parameter is null or if the hash codes of both objects are not equal. Otherwise + * it will return true. + * + * @param obj as the object which will be compared as an Object. + * @return true or false. + */ + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + + if (obj instanceof Piece) { + Piece piece = (Piece) obj; + + return this.hashCode() == piece.hashCode(); + } + + return false; + } }