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:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user