mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-04 17:20:55 +07:00
44 lines
1.2 KiB
HTML
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<Length> {
|
|
private int cm = 0;
|
|
|
|
@Override
|
|
public int compareTo(@NotNull Length o) {
|
|
if (cm == o.cm) return 0;
|
|
return cm < o.cm ? -1 : 1;
|
|
}
|
|
}
|
|
</code></pre>
|
|
<p>After the quick fix is applied:</p>
|
|
<pre><code>
|
|
class Length implements Comparable<Length> {
|
|
private int cm = 0;
|
|
|
|
@Override
|
|
public int compareTo(@NotNull Length o) {
|
|
if (cm == o.cm) return 0;
|
|
return cm < o.cm ? -1 : 1;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
return o instanceof Length && compareTo((Length) o) == 0;
|
|
}
|
|
}
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html> |