overrode hachCode and equals method of Piece
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user