File: ComparisonContractViolated.md

package info (click to toggle)
error-prone-java 2.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 23,204 kB
  • sloc: java: 222,992; xml: 1,319; sh: 25; makefile: 7
file content (13 lines) | stat: -rw-r--r-- 712 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
The comparison contract states that `sgn(compare(x, y)) == -sgn(compare(y, x))`.
(An immediate corollary is that `compare(x, x) == 0`.) This comparison
implementation either a) cannot return 0, b) cannot return a negative value but
may return a positive value, or c) cannot return a positive value but may return
a negative value.

The results of violating this contract can include `TreeSet.contains` never
returning true or `Collections.sort` failing with an IllegalArgumentException
arbitrarily.

In the long term, essentially all Comparators should be rewritten to use the
Java 8 Comparator factory methods, but our automated migration tools will, of
course, only work for correctly implemented Comparators.