Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect equality between Quantity #386

Closed
fillumina opened this issue Oct 22, 2022 · 2 comments
Closed

Incorrect equality between Quantity #386

fillumina opened this issue Oct 22, 2022 · 2 comments

Comments

@fillumina
Copy link

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
Copy link
Member

keilw commented Nov 3, 2022

Looks like a duplicate of #384

@keilw
Copy link
Member

keilw commented Nov 4, 2022

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().

@keilw keilw closed this as completed Nov 4, 2022
@keilw keilw added this to the 2.1.4 milestone Nov 4, 2022
@keilw keilw removed this from JSR 385 MR2 Dec 7, 2022
@keilw keilw moved this to ✅ Done in Units of Measurement backlog Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

2 participants