Implementing 'Comparable' where T is not the same as the implementing class is incorrect, since it violates the symmetry contract of compareTo.


The problem

The type argument of Comparable should always be the type of the current class.

For example, do this:

class Foo implements Comparable<Foo> {
  public int compareTo(Foo other) { ... }

not this:

class Foo implements Comparable<Bar> {
  public int compareTo(Bar other) { ... }

Implementing Comparable for a different type breaks the API contract, which requires x.compareTo(y) == -y.compareTo(x) for all x and y. If x and y are different types, this behaviour can’t be guaranteed.


Suppress false positives by adding the suppression annotation @SuppressWarnings("ComparableType") to the enclosing element.