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
Exact Quantity.equals(…) comparison is inconsistent with Quantity.toString() #284
Comments
Are you sure you're testing a current version of Salespoint? Asking because AssertJ outputs |
Looks like AssertJ is printing the object hash in case the |
Quantity.isEqualTo(…) allows to compare quantities without looking at the precision of the underlying amount, i.e. 1 is treated equally to 1.0 (in contrast to the ….equals(…) method).
We now forward the precision of the underlying amount to the formatter used in Quantity.toString(). This allows a quantity of 1 to be differentiated from 1.0 for plain ….toString() renderings.
We now use var for local variables. Whitespace.
Quantity.isEqualTo(…) allows to compare quantities without looking at the precision of the underlying amount, i.e. 1 is treated equally to 1.0 (in contrast to the ….equals(…) method).
We now forward the precision of the underlying amount to the formatter used in Quantity.toString(). This allows a quantity of 1 to be differentiated from 1.0 for plain ….toString() renderings.
We now use var for local variables. Whitespace.
Let's say I have two
Quantity
objectsq1
andq2
, both withmetric == Metric.UNIT
andq1.amount
equal tonew BigDecimal("1.0")
, andq2.amount
equal tonew BigDecimal("1")
.(Note that the two
BigDecimal
objects are not equal as perBigDecimal.equals()
.)Now I compare
q1
andq2
usingassertThat(q1).isEqualTo(q2)
. This fails with the following error message:The misleading identical output of actual vs. expected is because the current
toString()
always strips trailing zeroes. (By the way, the objects are considered unequal because they use an exact comparison ofamount
, instead ofBigDecimal.compareTo()
.)I suggest to:
amount
(as returned byBigDecimal.toString()
), making it easier to spot the difference, andcompareTo()
-like feature set ofQuantity
by addingisEqualTo()
to the already existingisGreaterThan()
,isLessThan()
, etc.Using
BigDecimal.compareTo()
instead ofBigDecimal.equals()
inQuantity.equals()
is probably not an option because of inconsistencies withhashCode()
.The text was updated successfully, but these errors were encountered: