Override hashCode and equals methods of Entity.

In order to make sure entities of same type, having the same name are
equals, these two methods have been override to make sure that Sets
contains only one object with a specific name.
This commit is contained in:
2025-11-04 13:56:03 +01:00
parent 6125cdade7
commit 90e7c81594
2 changed files with 110 additions and 1 deletions

View File

@@ -8,13 +8,16 @@ public class Entity {
private String name;
public EntityType type;
private EntityType type;
public int getId() {
return id;
}
public void setId(int id) {
if (id < 0) {
throw new IllegalArgumentException("Id can't be null");
}
this.id = id;
}
@@ -33,5 +36,27 @@ public class Entity {
public void setType(EntityType type) {
this.type = type;
}
@Override
public int hashCode() {
return getName() == null ? super.hashCode() : getName().hashCode();
}
@Override
public boolean equals(Object obj) {
if(super.equals(obj)) {
return true;
}
if (obj instanceof Entity) {
Entity entity = (Entity) obj;
if (getType() != entity.getType()) {
return false;
}
boolean namesAreTheSame = entity.getName() != null && getName() != null && getName().equals(entity.getName());
return namesAreTheSame;
}
return false;
}
}