-
Notifications
You must be signed in to change notification settings - Fork 463
Description
Summary
Multiple classes in JTS implement Comparable with meaningful compareTo() implementations,
but do not override equals(). This means two objects considered equal by compareTo() (returns 0)
are not equal by equals() (identity comparison), violating the Comparable contract recommendation.
Affected classes
| Class | compareTo compares by | File |
|---|---|---|
DD |
hi and lo double values |
modules/core/src/.../math/DD.java |
EdgeIntersection |
segment index and distance | modules/core/src/.../geomgraph/EdgeIntersection.java |
EdgeEnd |
coordinates and direction | modules/core/src/.../geomgraph/EdgeEnd.java |
LinearLocation |
component/segment index + fraction | modules/core/src/.../linearref/LinearLocation.java |
Corner |
angle | modules/core/src/.../coverage/Corner.java |
BoundablePair |
distance | modules/core/src/.../index/strtree/BoundablePair.java |
SweepLineEvent (sweepline) |
x-value | modules/core/src/.../index/sweepline/SweepLineEvent.java |
SweepLineEvent (geomgraph) |
x-value | modules/core/src/.../geomgraph/index/SweepLineEvent.java |
NodeSection |
coordinates | modules/core/src/.../operation/relateng/NodeSection.java |
OffsetCurveSection |
location | modules/core/src/.../operation/buffer/OffsetCurveSection.java |
Special case — DD: Has equals(DD y) (line 988) but this is NOT an override of
Object.equals(Object) — the parameter type is wrong. Standard Object.equals() (identity
comparison) is used in collections and by frameworks.
JDK contract reference
From Comparable Javadoc (JDK 21):
"It is strongly recommended, but not strictly required, that
(x.compareTo(y)==0) == (x.equals(y)).
[...] Sorted sets (and sorted maps) without explicit comparators behave 'strangely' when they
are used with elements (or keys) whose natural ordering is inconsistent with equals."
Impact
Using these classes in TreeSet/TreeMap may cause unexpected behavior where elements considered
equal by the sorted collection's ordering are treated as distinct by equals()-based operations.
How this was found
Detected by an automated JDK conformance oracle (ComparableOracles.checkCompareToEqualsConsistency)
across 10 classes in the JTS codebase.