unitsofmeasurement / uom-lib

Units of Measurement Libraries
Other
14 stars 13 forks source link

bug in Unit equals #75

Closed ChMThiel closed 2 years ago

ChMThiel commented 2 years ago

If you compare a BaseUnit 1000kg with a TransformedUnit of 1000kg using equals, false will be returned. The equals of BaseUnit returns false if the other is not of class BaseUnit. The equals of TransformedUnit returns false if the other class is not instanceof TransfromedUnit.

Imho the comparison should return true, if the 'real' value (in my example 1000kg) equals, regardless of the actual Unit-implementation used.

keilw commented 2 years ago

Please use Unit.isEquivalentTo() (or in your example more likely Quantity.isEquivalentTo() because 1000 kg is not just a Unit anymore) it allows to compare 1000m with 1km and similar, and of course it also tolerates the difference between different class types. There are standard Java rules that make Integer.valueOf(1).equals(BigInteger.ONE) return false as well, even though both have the numeric value of 1.