To compare domain objects — your model elements — with each other, LuciadLightspeed often uses the equals
method. It doesn’t use object equality through the ==
operator for that.
You must make sure that implementations of the equals
and hashCode
methods reflect that. If LuciadLightspeed compares two distinct domain objects with the equals
method, the result must be false
: the objects aren’t equal.
Developers most commonly violate this rule when they create domain objects by extending from one of the ALcdShape
classes.
The ALcdShape
classes have an equals
method that returns true
when both shapes have the same geometry.
For example, when you compare two points, the equals
method returns true
when the points are in the same location.
When the domain objects in your ILcdModel
extend from ALcdShape
, make sure to override both the equals
and hashCode
methods.
Usually, each domain object is a unique instance, and you can override those methods as follows:
/**
* When using a shape directly as domain object, the equals and hashCode method should
* be overwritten and made more strict to avoid that two different domain objects
* with the same geometry are considered equal.
*/
static class MyDomainObject extends TLcdLonLatPoint {
@Override
public boolean equals(Object aObject) {
return aObject == this;
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
}