overrode hachCode and equals method of Piece

This commit is contained in:
Hanno Fleischer
2024-12-06 01:15:06 +01:00
parent e98418b274
commit d61b68aa41

View File

@@ -131,4 +131,37 @@ public UUID getUuid() {
public String toString() { public String toString() {
return "Piece with UUID: %s and color: %s with state: %s".formatted(this.uuid, color, state); 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;
}
} }