mirror of
https://gitflic.ru/project/openide/openide.git
synced 2025-12-16 14:23:28 +07:00
34 lines
1.4 KiB
HTML
34 lines
1.4 KiB
HTML
<html>
|
|
<body>
|
|
Reports problems in <code>Comparator.compare()</code> and <code>Comparable.compareTo()</code> implementations.
|
|
<p>The following cases are reported:</p>
|
|
<ul>
|
|
<li>
|
|
A parameter is not used. Most likely this is a typo and the other parameter is compared
|
|
with itself, or the method is not implemented correctly.
|
|
</li>
|
|
<li>
|
|
It's evident that the method does not return <code>0</code> for the same elements. Such a comparison method violates the contract
|
|
and can produce unpredictable results when equal elements are encountered. In particular,
|
|
sorting may fail with an exception on some data.</li>
|
|
<li>
|
|
The comparison method never returns positive or negative value. To fulfill the contract, if the comparison method returns positive values,
|
|
it should also return negative ones if arguments are supplied in reversed order.
|
|
</li>
|
|
<li>
|
|
The comparison method returns <code>Integer.MIN_VALUE</code>. While allowed by the contract, it may be error-prone, as some call sites
|
|
may incorrectly try to invert the return value of the comparison method using the unary minus operator.
|
|
The negated value of <code>Integer.MIN_VALUE</code> is <code>Integer.MIN_VALUE</code>.
|
|
</li>
|
|
</ul>
|
|
<p><b>Example:</b></p>
|
|
<pre><code>
|
|
Comparator<String> lambda =
|
|
(a, b) -> a.length() > b.length()
|
|
? 0
|
|
: Math.random() > 0.5 ? -1 : 1;
|
|
</code></pre>
|
|
<!-- tooltip end -->
|
|
</body>
|
|
</html>
|