rspec/rules/S1210/description.adoc

16 lines
650 B
Plaintext
Raw Normal View History

2021-01-27 13:42:22 +01:00
According to the Java ``++Comparable.compareTo(T o)++`` documentation:
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2021-01-06 17:38:34 +01:00
____
It is strongly recommended, but not strictly required that ``++(x.compareTo(y)==0) == (x.equals(y))++``.
2020-06-30 12:47:33 +02:00
Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact.
2020-06-30 12:47:33 +02:00
The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
____
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
If this rule is violated, weird and unpredictable failures can occur.
2021-02-02 15:02:10 +01:00
2021-01-27 13:42:22 +01:00
For example, in Java 5 the ``++PriorityQueue.remove()++`` method relied on ``++compareTo()++``, but since Java 6 it has relied on ``++equals()++``.