Files
openide/java/java-impl/resources/inspectionDescriptions/ComparableImplementedButEqualsNotOverridden.html
Leonid Shalupov 40795fe787 IJI-2422: community/java: move resources under resources root
GitOrigin-RevId: 8b2b63fc6db476ca0c2cfe5cadd84db6c4236d0f
2025-02-05 04:43:28 +00:00

44 lines
1.2 KiB
HTML

<html>
<body>
Reports classes that implement <code>java.lang.Comparable</code>
but do not override <code>equals()</code>.
<p>
If <code>equals()</code>
is not overridden, the <code>equals()</code> implementation is not consistent with
the <code>compareTo()</code> implementation. If an object of such a class is added
to a collection such as <code>java.util.SortedSet</code>, this collection will violate
the contract of <code>java.util.Set</code>, which is defined in terms of
<code>equals()</code>.
</p>
<p><b>Example:</b></p>
<pre><code>
class Length implements Comparable&lt;Length> {
private int cm = 0;
@Override
public int compareTo(@NotNull Length o) {
if (cm == o.cm) return 0;
return cm &lt; o.cm ? -1 : 1;
}
}
</code></pre>
<p>After the quick fix is applied:</p>
<pre><code>
class Length implements Comparable&lt;Length> {
private int cm = 0;
@Override
public int compareTo(@NotNull Length o) {
if (cm == o.cm) return 0;
return cm &lt; o.cm ? -1 : 1;
}
@Override
public boolean equals(Object o) {
return o instanceof Length && compareTo((Length) o) == 0;
}
}
</code></pre>
<!-- tooltip end -->
</body>
</html>