enhance hashCode and equals methods
- intelliJ generated methods
This commit is contained in:
parent
518683b676
commit
ec86006a07
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue