Java


JAVA.COMPARE.CTO.ASSYM : Asymmetric compareTo (Java)

要旨

compareTo() is not symmetrical.

A class C that implements the raw interface java.lang.Comparable must implement the compareTo(Object) method. If C is made to implement the non-raw interface java.lang.Comparable<C>, then it must implement the compareTo(C) method instead.

Moreover, it is good practice to make compareTo() consistent with equals(): if the comparison of two objects yields 0, then they should be equal. The validity of this implication is in general undecidable. There are, however, frequent situations when, typically, this implication does not hold. An example is when equals() is inherited from java.lang.Object.

This checker verifies that compareTo(Object) is defined for classes implementing the raw java.lang.Comparable interface, instead of the (possibly more logical) method compareTo(C). Moreover, it verifies the consistency of compareTo() wrt equals().

Inconsistent definitions of compareTo()/equals() induce unexpected behaviors when objects are put inside most SortedSet classes of the standard Java library.

プロパティ

クラス名 Asymmetric compareTo (Java)
日本語クラス名 Asymmetric compareTo (Java)
クラス分類 信頼性 (reliability)
ニーモニック JAVA.COMPARE.CTO.ASSYM
カテゴリー
CWE CWE:697 Incorrect Comparison
CERT-Java CERT-Java:MET08-J Preserve the equality contract when overriding the equals() method
対応言語 Java で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Asymmetric compareTo (Java)"

The following implementation of compareTo() is not symmetrical. In addition, it might return 0 when the inherited equals() method returns false.

public final class CompareToVsEquals3 implements Comparable<CompareToVsEquals3> {
  private int f;
        
  public CompareToVsEquals3(int f) {
    this.f = f;
  }

  @Override
  public int compareTo(CompareToVsEquals3 o) { // Asymmetric compareTo (Java) warning issued here
                                               // compareTo without equals (Java) warning issued here
    if (f > 0)
      return -1;
    else
      return 0;
  }

  @Override
  public String toString() {
    return String.valueOf(f);
  }
}

The following code corrects these problems.

public final class CompareToVsEquals3 implements Comparable<CompareToVsEquals3> {
  private int f;
        
  public CompareToVsEquals3(int f) {
    this.f = f;
  }

  @Override
  public int compareTo(CompareToVsEquals3 o) {
    return f - o.f;
  }

  @Override
  public boolean equals(Object other) {
    return other instanceof CompareToVsEquals3 && ((CompareToVsEquals3) other).f == f;
  }

  @Override
  public int hashCode() {
    return f;
  }

  @Override
  public String toString() {
    return String.valueOf(f);
  }
}

解決法

Use the non-raw interface java.lang.Comparable<C>; make equals() consistent with compareTo(). In most cases, this just amounts to providing the missing definition of equals().

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。