[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Support #SJE-633108]: [udunits] ucar.units.Unit subclasses override equals( Object), but not hashCode()



Christian,

> Many of the subclasses of ucar.units.Unit override equals(Object), but
> fail to override hashCode().

Thanks for bringing this to my attention.  I'll see what I can do
about fixing it ASAP.

> 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.
> 
> _______________________________________________
> udunits mailing list
> address@hidden
> For list information, to unsubscribe, or change your membership options, 
> visit: http://www.unidata.ucar.edu/mailing_lists/

Regards,
Steve Emmerson

Ticket Details
===================
Ticket ID: SJE-633108
Department: Support UDUNITS
Priority: Normal
Status: Open