unitsofmeasurement / indriya

JSR 385 - Reference Implementation
Other
119 stars 42 forks source link

Incorrect equality between Quantity #386

Closed fillumina closed 1 year ago

fillumina commented 1 year ago

The following test defines two quantities that should be equals but because they are using a different Unit class they are not. I think this impacts the usability of the library. Regards.

@Test
public void shouldNotEqualizeQuantities() {
    // uses TransformedUnit
    Quantity<Mass> w1 = Quantities.getQuantity(12.5, MetricPrefix.KILO(GRAM));

    // uses BasicUnit
    Quantity<Mass> w2 = (Quantity<Mass>) Quantities.getQuantity("12.5 kg");

    assertFalse(w1.equals(w2));
    assertFalse(w2.equals(w1));

    // workaround
    assertEquals(w1.toString(), w2.toString());
}
keilw commented 1 year ago

Looks like a duplicate of #384

keilw commented 1 year ago

It is not directly related to #384, but we won't fix this, because MetricPrefix.KILO(GRAM) becomes "1000 g" and that as you correctly asserted is a different unit, similar for the Number type of the value, because if you parse Quantity<Mass> w2 = (Quantity<Mass>) Quantities.getQuantity("12.5 kg"); with locales like GERMAN, etc. (or probably ITALIAN in your case) "12.5" also turns into 125, an Integer unlike the Double value of 12.5.

// uses BasicUnit
Quantity<Mass> w1 = Quantities.getQuantity(12.5, KILOGRAM);

// uses BasicUnit
Quantity<Mass> w2 = (Quantity<Mass>) Quantities.getQuantity("12,5 kg");

are equal, for the other way of constructing quantities please compare them with isEquivalentTo().