enhance hashCode and equals methods

- intelliJ generated methods
This commit is contained in:
Jan Löwenstrom 2020-01-01 14:57:08 +01:00
parent 518683b676
commit ec86006a07
3 changed files with 63 additions and 25 deletions

View File

@ -65,22 +65,22 @@ public class AntState implements State, Visualizable {
@Override
public boolean equals(Object obj){
if(obj instanceof AntState){
AntState toCompare = (AntState) obj;
if(!this.pos.equals(toCompare.pos) || !this.hasFood == toCompare.hasFood){
return false;
}
for (int i = 0; i < toCompare.knownWorld.length; i++) {
for (int j = 0; j < toCompare.knownWorld[i].length; j++) {
if(!this.knownWorld[i][j].equals(toCompare.knownWorld[i][j])){
return false;
}
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
}
}
return true;
AntState toCompare = (AntState) obj;
if(!this.pos.equals(toCompare.pos) || !this.hasFood == toCompare.hasFood){
return false;
}
return super.equals(obj);
for (int i = 0; i < toCompare.knownWorld.length; i++) {
for (int j = 0; j < toCompare.knownWorld[i].length; j++) {
if(!this.knownWorld[i][j].equals(toCompare.knownWorld[i][j])){
return false;
}
}
}
return true;
}
@Override

View File

@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import java.io.Serializable;
import java.util.Objects;
@AllArgsConstructor
@Getter
@ -13,20 +14,21 @@ public class DinoState implements State, Serializable {
@Override
public String toString() {
return Integer.toString(xDistanceToObstacle);
return "DinoState{" +
"xDistanceToObstacle=" + xDistanceToObstacle +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DinoState dinoState = (DinoState) o;
return xDistanceToObstacle == dinoState.xDistanceToObstacle;
}
@Override
public int hashCode() {
return this.xDistanceToObstacle;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof DinoState){
DinoState toCompare = (DinoState) obj;
return toCompare.getXDistanceToObstacle() == this.xDistanceToObstacle;
}
return super.equals(obj);
return Objects.hash(xDistanceToObstacle);
}
}

View File

@ -0,0 +1,36 @@
package evironment.jumpingDino;
import lombok.Getter;
import java.util.Objects;
@Getter
public class DinoStateWithSpeed extends DinoState{
private int obstacleSpeed;
public DinoStateWithSpeed(int xDistanceToObstacle, int obstacleSpeed) {
super(xDistanceToObstacle);
this.obstacleSpeed = obstacleSpeed;
}
@Override
public String toString() {
return "DinoStateWithSpeed{" +
"obstacleSpeed=" + obstacleSpeed +
"} " + super.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof DinoStateWithSpeed)) return false;
if (!super.equals(o)) return false;
DinoStateWithSpeed that = (DinoStateWithSpeed) o;
return getObstacleSpeed() == that.getObstacleSpeed();
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), getObstacleSpeed());
}
}