Skip to content
This repository has been archived by the owner on May 26, 2021. It is now read-only.

Same values, but with different types, was considered different. #156

Closed
ezidio opened this issue May 25, 2017 · 6 comments
Closed

Same values, but with different types, was considered different. #156

ezidio opened this issue May 25, 2017 · 6 comments
Assignees
Milestone

Comments

@ezidio
Copy link

ezidio commented May 25, 2017

@Test
public void test_equality() throws Exception {
ComparableQuantity<Length> value = Quantities.getQuantity(20.0, Units.METRE);
ComparableQuantity<Length> anotherValue = Quantities.getQuantity(20, Units.METRE);
assertEquals(value, anotherValue);
}

Result

java.lang.AssertionError: Expected :20.0 m Actual :20 m

@otaviojava otaviojava self-assigned this May 26, 2017
@otaviojava
Copy link
Member

Thanks for giving us feedback about the API, WIP to fix it.

@otaviojava
Copy link
Member

from JavaDoc:

/**

  • Compares this measure against the specified object for strict equality (same unit and same amount).
  • Similarly to the {@link BigDecimal#equals} method which consider 2.0 and 2.00 as different objects because of different internal scales,
  • quantities such as Quantities.getQuantity(3.0, KILOGRAM) Quantities.getQuantity(3, KILOGRAM) and
  • Quantities.getQuantity("3 kg") might not be considered equals because of possible differences in their implementations.
  • To compare measures stated using different units or using different amount implementations the {@link #compareTo compareTo} or
  • {@link #equals(javax.measure.Quantity, double, javax.measure.unit.Unit) equals(Quantity, epsilon, epsilonUnit)} methods should be used.
  • @param obj
  •      the object to compare with.
    
  • @return this.getUnit.equals(obj.getUnit())
  •     && this.getValue().equals(obj.getValue())</code>
    

*/

@keilw
Copy link
Member

keilw commented May 26, 2017

Java is rather strict on that, it is not something a JSR 363 implementation introduced or causes.

BigInteger.ONE.equals(Integer.valueOf(1)

to be false as well.

In some particular subtypes like DoubleQuantity a method like doubleValue() could be used to compare it to a given Number instead of applying equals() on that number.

@keilw
Copy link
Member

keilw commented May 26, 2017

In fact the RI must behave exactly the same here, so any PR or improvement should be available to both.

@otaviojava
Copy link
Member

@keilw I can do the same on RI if this change make sense:
#157

@otaviojava
Copy link
Member

FWI do it on RI is easier than on SE

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants