#11 Equals/hashcode

closed-fixed
nobody
None
5
2011-07-06
2010-12-10
No

There are classes like Polygon which implement equals but not hashCode. This is a well-known bug pattern.

I recommend downloading FindBugs, run the GUI, and start fixing the serious ssues it brings up. It's what I did and I discovered a variety of problems with JTS.

Discussion

  • David Smiley

    David Smiley - 2011-01-27
    • status: open --> open-rejected
     
  • David Smiley

    David Smiley - 2011-01-27

    Geometry (from which Polygon extends) does not implement equals(Object), it implements equals(Geometry) and hence I got confused.

     
  • ZeroOne

    ZeroOne - 2011-02-18

    I don't understand why this issue was rejected, I think there's no question about the fact that there's something wrong with the hash codes. Here's a complete example:

    import com.vividsolutions.jts.geom.Geometry;
    import com.vividsolutions.jts.io.WKTReader;
    public class GeometryProcessor {
    public static void main(String[] args) throws Exception {
    WKTReader reader = new WKTReader();
    Geometry RECTANGLE1 = reader.read("POLYGON((1 1, 1 4, 3 4, 3 1, 1 1))");
    Geometry RECTANGLE2 = reader.read("POLYGON((1 1, 1 4, 3 4, 3 1, 1 1))");
    System.out.println("1: "+RECTANGLE1.hashCode());
    System.out.println("2: "+RECTANGLE2.hashCode());
    System.out.println("3: "+RECTANGLE1.equals(RECTANGLE2));
    System.out.println("4: "+RECTANGLE1.equalsExact(RECTANGLE2));
    }
    }

    Notice how the hash codes are different even though equals returns true. This is against the Java specifications, see http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#hashCode\() :

    "If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result."

     
  • David Smiley

    David Smiley - 2011-02-18

    villes01, your example is not what it seems. JTS's Geometry class overloads the equals method with equals(Geometry). On the mailing list, it was recently decided that this overloaded method should be deprecated because it causes confusion. It was also decided that equals(Object) should invoke equalsExact() and that hashCode should work based off that. These things were recently committed to trunk.

     
  • ZeroOne

    ZeroOne - 2011-02-18

    Oh, okay, I see it now. That sounds reasonable. I'm looking forward to the new version then. Thanks for clarifying this!

     
  • Dr JTS

    Dr JTS - 2011-07-06

    Geometry now implements hashcode.

     
  • Dr JTS

    Dr JTS - 2011-07-06
    • status: open-rejected --> closed-fixed
     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks