Menu

#2012 DADescriptor equals/compareTo issue

Function
closed-fixed
5
2010-09-15
2010-06-30
No

The DADescriptor class implements the compareTo method but not the equals method, and there are cases when compareTo() returns 0 but equals() returns false.

Pattern: Class defines compareTo(...) and uses Object.equals()
id: EQ_COMPARETO_USE_OBJECT_EQUALS, type: Eq, category: BAD_PRACTICE

This class defines a compareTo(...) method but inherits its equals() method from java.lang.Object. Generally, the value of compareTo should return zero if and only if equals returns true. If this is violated, weird and unpredictable failures will occur in classes such as PriorityQueue. In Java 5 the PriorityQueue.remove method uses the compareTo method, while in Java 6 it uses the equals method.

From the JavaDoc for the compareTo method in the Comparable interface:

It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."

Discussion

  • Dave Blaschke

    Dave Blaschke - 2010-06-30

    Fixing this eliminates 1 FindBugs issue

     
  • Dave Blaschke

    Dave Blaschke - 2010-06-30

    First pass proposal:

    @Override
    public boolean equals\(Object pObj\) \{
        if \(\!\(pObj instanceof DADescriptor\)\) return false;
        DADescriptor that = \(DADescriptor\) pObj;
        return this.iURL.equals\(that\);
    \}
    
     
  • Dave Blaschke

    Dave Blaschke - 2010-06-30

    First pass proposal based off compareTo:

        DADescriptor that = o;
        return this.iURL.compareTo\(that.iURL\);
    
     
  • Dave Blaschke

    Dave Blaschke - 2010-06-30
    • status: open --> open-accepted
     
  • Dave Blaschke

    Dave Blaschke - 2010-07-01
    • status: open-accepted --> open-fixed
     
  • Dave Blaschke

    Dave Blaschke - 2010-07-01

    Patch sent for community review. During a 2 week period any
    exploiter may comment on the patch, request changes or turn it
    down completely (with good reason). For the time being the patch is part of the "Experimental" branch in CVS.

     
  • Dave Blaschke

    Dave Blaschke - 2010-07-28

    Patch against HEAD

     
  • Dave Blaschke

    Dave Blaschke - 2010-07-28
    • status: open-fixed --> pending-fixed
     
  • Dave Blaschke

    Dave Blaschke - 2010-07-28

    The community review has completed and we received no substantial critisism. Therefore the patch has been approved and merged into the "HEAD" branch. The next release will pick it up.

     
  • Dave Blaschke

    Dave Blaschke - 2010-09-15

    The patch was picked up by release 2.1.6 and will therefore be closed.

     
  • Dave Blaschke

    Dave Blaschke - 2010-09-15
    • status: pending-fixed --> closed-fixed
     

Log in to post a comment.