Must I always adhere to the contract established for Comparable Interface
? Is there acceptable leverage when all three cases of the contract are unclear?
The contract simply states:
If this object precedes that; return negative value
If this object equals that; return zero
If this object proceeds that; return positive value
I desire to compare two points which are Point2D
objects. Comparing two points using the distance(Point2D point)
behavior makes a compareTo()
appear binary; Either two points have a zero distance or their distance is nonzero.
If I were to write such a compareTo()
behavior that overrides the default, perhaps it would be as follows:
private Point2D location;
public int compareTo(Object o)
{
if(this.getLocation().distance(o.getLocation() == 0)
{
return 0;
}
else
{
return 1;
}
}
Perhaps comparing two Point2D objects would be better suited using simple behavior testing distance and not bother implementing Comparable
. There may even be a way to adhere to the contract I do not know.