Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: replaced code samples, old NCCE violated MET13-J

...

Noncompliant Code Example

This noncompliant code example violates the third condition in the contract. This requirement states that the objects that compareTo() considers equal (by returning 0) must be ordered identically when compared to other objects. For example, it may be necessary to compare a card with any other card to check whether both belong to the same suit or have the same rank. When neither of these conditions is true, compareTo() is expected to order the cards based on rank alone. This situation may arise in a game like Uno or Crazy Eights, where one can place a card on the pile only when it shares a suit or rank with the topmost card on the pileprogram implements the classic game of Roshambo, using the compareTo() operator to determine the winner of a game.

Code Block
bgColor#FFCCCC
public final class CardGameEntry implements Comparable {
  private String suit;
  private int rank;

  public Card(String s, int r) {
    if (s == null) {
      throw new NullPointerException();
    enum Roshambo {ROCK, PAPER, SCISSORS}
    suitprivate =Roshambo svalue;
    rank = r;
  }

  public boolean equalsGameEntry(ObjectRoshambo ovalue) {
    if (o instanceof Card) {
      Card c = (Card)o;
      return suit.equals(c.suit) || (rank == c.rank); // Bad this.value = value;
    }
    return false;
  }

  // This method violates its contract
  public int compareTo(Object othat) {
        if (!(othat instanceof CardRoshambo)) {
      Card c = (Card)o;
     throw if (suit.equals(c.suit) ) new ClassCastException();
        return 0;}
      if ((c.rank >= rank + Integer.MIN_VALUE) &&  GameEntry t = (GameEntry) that;
        return  (c.rankvalue <== rank + Integer.MAX_VALUE) )t.value) ? 0
        // Check for integer overflow
: (value == Roshambo.ROCK && t.value ==  return c.rank - rank; // Order based on rankRoshambo.PAPER) ? -1
    }
    throw new ClassCastException();
  }

  public static void main(String[] args) {
    Card a = new Card("Clubs", 2);: (value == Roshambo.PAPER && t.value == Roshambo.SCISSORS) ? -1
    Card b = new Card("Clubs", 10);
   : Card(value c == new Card("Hearts", 7);
    System.out.println(a.compareTo(b)); // Returns 0Roshambo.SCISSORS && t.value == Roshambo.ROCK) ? -1
    System.out.println(a.compareTo(c)); // Returns a negative number
    System.out.println(b.compareTo(c)); // Returns a positive number
  : 1;
    }
}

Here, comparing a with c} indicates that {{c is larger, and comparing b with c indicates that b is larger. Consequently, b must be larger than a. However, comparing a with b reports that a and b are equal. Therefore, the compareTo() implementation violates the general contract.However, this game violates the notion of transitivity, since Rock beats Scissors, Scissors beats Paper, but Rock does not beat Paper.

Compliant Solution

This compliant solution ensures both that the compareTo() contract is satisfied and also that the corresponding equals() method is consistent with the compareTo() methodimplements the game, but does not use the Comparable interface.

Code Block
bgColor#ccccff
public final 
class Card implementsGameEntry Comparable{
  private  Stringpublic suit;
enum Roshambo private int rank;

  public Card(String s, int r) {{ROCK, PAPER, SCISSORS}
    private Roshambo value;

    ifpublic GameEntry(s == nullRoshambo value) {
        throwthis.value new= NullPointerException()value;
    }
    suit = s;
    rank = r;
  }

  public booleanint equalsbeats(Object othat) {
        if (o!(that instanceof CardRoshambo)) {
      Card c=(Card)o;
     throw returnnew suit.equals(c.suit) && (rank == c.rank); // GoodClassCastException();
    }
    return false;
  }

  // This method fulfills its contract
 GameEntry publict int= compareTo(Object oGameEntry) {that;
    if (o instanceof Card) {
return (value == t.value)   Card c=(Card)o;? 0
      if (suit.equals(c.suit) &&
       :   (c.rankvalue >== rank + Integer.MIN_VALUE)Roshambo.ROCK &&
          (c.rank <= rank + Integer.MAX_VALUE) )  t.value == Roshambo.PAPER) ? -1
        return c.rank - rank;
 : (value == Roshambo.PAPER && t.value return== suit.compareTo(c.suit);
    }Roshambo.SCISSORS) ? -1
    throw new ClassCastException();
  }

  public static void: main(String[] args) {
    Card a = new Card("Clubs", 2);
    Card b = new Card("Clubs", 10);
    Card c = new Card("Hearts", 7);
    System.out.println(a.compareTo(b)); // Returns 0
    System.out.println(a.compareTo(c)); // Returns a negative number
    System.out.println(b.compareTo(c)); // Returns a negative number
  }
}

...

value == Roshambo.SCISSORS && t.value == Roshambo.ROCK) ? -1
            : 1;
    }
}

Risk Assessment

Violating the general contract when implementing the compareTo() method can result in unexpected results, possibly leading to invalid comparisons and information disclosure.

...