[udunits] ucar.units.Unit subclasses override equals(Object), but not hashCode()

  • To: udunits@xxxxxxxxxxxxxxxx
  • Subject: [udunits] ucar.units.Unit subclasses override equals(Object), but not hashCode()
  • From: Christian Ward-Garrison <cwardgar@xxxxxxxx>
  • Date: Tue, 03 Mar 2009 02:02:58 -0700
Hello,

Many of the subclasses of ucar.units.Unit override equals(Object), but fail to override hashCode(). As a result, the hashCode() implementation that is inherited from Object no longer satisfies its contract, namely:

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.

Here is an example of the contract being broken:

Unit unit1 = UnitFormatManager.instance().parse("m/s");
Unit unit2 = UnitFormatManager.instance().parse("m/s");

System.out.println(unit1.equals(unit2));  // Prints "true"
System.out.println(unit1.hashCode() == unit2.hashCode());  // Prints "false"

As a result of this behavior, Units are not generally usable in a hashing container.



  • 2009 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the udunits archives: