From 28ac25fa5509ecc7a381a95ce1093385380a2368 Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Tue, 11 May 2021 21:17:06 +0200 Subject: [PATCH] refactor: generated equals, hashCode and toString for entity classes --- .../gamelibrary/entities/Character.java | 31 +++++++++++++++++++ .../gamelibrary/entities/InfinityStone.java | 24 ++++++++++++++ .../gamelibrary/entities/Inventory.java | 24 ++++++++++++++ .../marvelous/gamelibrary/entities/Rock.java | 27 ++++++++++++++++ .../marvelous/gamelibrary/entities/Stat.java | 25 +++++++++++++++ 5 files changed, 131 insertions(+) diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java index de5db8e..b03ad35 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Character.java @@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities; import uulm.teamname.marvelous.gamelibrary.IntVector2; +import java.util.Objects; + /** Represents a playable character inside a match. */ public class Character extends Entity { /** The name of the character */ @@ -72,4 +74,33 @@ public class Character extends Entity { clone.ap.setValue(ap.getValue()); return clone; } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Character character = (Character) o; + return attackRange == character.attackRange && rangedDamage == character.rangedDamage && meleeDamage == character.meleeDamage && Objects.equals(name, character.name) && Objects.equals(hp, character.hp) && Objects.equals(mp, character.mp) && Objects.equals(ap, character.ap) && Objects.equals(inventory, character.inventory); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), name, hp, mp, ap, attackRange, rangedDamage, meleeDamage, inventory); + } + + @Override + public String toString() { + return "Character{" + + "name='" + name + '\'' + + ", hp=" + hp + + ", mp=" + mp + + ", ap=" + ap + + ", attackRange=" + attackRange + + ", rangedDamage=" + rangedDamage + + ", meleeDamage=" + meleeDamage + + ", inventory=" + inventory + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java index 0b6089a..6f59f4c 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/InfinityStone.java @@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities; import uulm.teamname.marvelous.gamelibrary.IntVector2; +import java.util.Objects; + /** Represents an infinity stone {@link Entity}. Can only exist on the map. */ public class InfinityStone extends Entity { /** The {@link StoneType} of the infinity stone */ @@ -24,4 +26,26 @@ public class InfinityStone extends Entity { public InfinityStone clone() { return new InfinityStone(id, position, type); } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + InfinityStone that = (InfinityStone) o; + return type == that.type; + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), type); + } + + @Override + public String toString() { + return "InfinityStone{" + + "type=" + type + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java index 6c13d0e..420cbc4 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Inventory.java @@ -3,6 +3,7 @@ package uulm.teamname.marvelous.gamelibrary.entities; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; +import java.util.Objects; /** Represents an inventory of 6 slots of {@link StoneType}s that can be manipulated. */ public class Inventory implements Iterable { @@ -79,4 +80,27 @@ public class Inventory implements Iterable { public Iterator iterator() { return content.iterator(); } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Inventory that = (Inventory) o; + return size == that.size && Objects.equals(content, that.content); + } + + @Override + public int hashCode() { + return Objects.hash(size, content); + } + + + @Override + public String toString() { + return "Inventory{" + + "size=" + size + + ", content=" + content + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java index 0c73035..ba7e94d 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Rock.java @@ -2,6 +2,8 @@ package uulm.teamname.marvelous.gamelibrary.entities; import uulm.teamname.marvelous.gamelibrary.IntVector2; +import java.util.Objects; + /** Represents a rock entity on the map. */ public class Rock extends Entity { /** The maximum hp of the rock */ @@ -38,4 +40,29 @@ public class Rock extends Entity { clone.hp = hp; return clone; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + if (!super.equals(o)) return false; + Rock rock = (Rock) o; + return maxHP == rock.maxHP && hp == rock.hp; + } + + + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), maxHP, hp); + } + + + @Override + public String toString() { + return "Rock{" + + "maxHP=" + maxHP + + ", hp=" + hp + + '}'; + } } diff --git a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Stat.java b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Stat.java index 7d64c55..0ff4a2b 100644 --- a/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Stat.java +++ b/src/main/java/uulm/teamname/marvelous/gamelibrary/entities/Stat.java @@ -1,5 +1,7 @@ package uulm.teamname.marvelous.gamelibrary.entities; +import java.util.Objects; + /** Represents a stat property of a {@link Character}. */ public class Stat { /** The {@link StatType} of the stat */ @@ -37,4 +39,27 @@ public class Stat { public void decreaseValue(int value) { this.value -= value; } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Stat stat = (Stat) o; + return max == stat.max && value == stat.value && type == stat.type; + } + + @Override + public int hashCode() { + return Objects.hash(type, max, value); + } + + @Override + public String toString() { + return "Stat{" + + "type=" + type + + ", max=" + max + + ", value=" + value + + '}'; + } }