[
https://issues.apache.org/jira/browse/LANG-843?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13595630#comment-13595630
]
Premysl Maly commented on LANG-843:
-----------------------------------
Hello Adrian,
what you say is correct, you always can achieve something by using different
commands. Sometime, though, a utility that handles the problem more complexly
becomes handy. In our projects we found many times the need to compare two
BigDecimal numbers, while we didn't have any knowledge of their values, nor
whether they ate null or not null. While using the code you suggested we would
have to do this:
boolean isEqual = first != null ? first.compareTo(second) == 0 : false;
which is IMHO the time, when the utility like:
boolean isEqual = BigDecimalUtils.equals(first, second)
comes handy. The first way is perfectly OK, but somehow needs you to think
twice, when you see the code, what does it actually do.
Also we sometime need to compare two numbers and we want the outcome to be true
even if the numbers are both the same value, as well as both are null, since it
is perfectly ok in the scenario we have. In the old way I would write someting
like:
boolean isEqual = first == null ? ( second == null ? true ; false ) :
first.compareTo(second) == 0 : false;
Well, this code gives me a headache even the second i look at it just after i
have written it :-) Of course, it could be written nicely in two or three rows,
with comments and so on. But when you need this type of comparison in more than
two places in your code, it is a time to make an utility to handle it for you:
boolean isEqual = BigDecimalUtils.equals(first, second, NullsEquality.EQUALS)
When you read this code, it is plain to see, that you are comparing first and
second and you expect them either to be both null or the same value.
If you look through the code of the patch I hope wou will understand, why we
did it this way and why we decided to share our piece of code with the rest of
the Java community.
I hope I defended the purpose of my suggestion a bit.
Best regards
Premysl Maly
> Providing new class BigDecimalUtils
> -----------------------------------
>
> Key: LANG-843
> URL: https://issues.apache.org/jira/browse/LANG-843
> Project: Commons Lang
> Issue Type: Improvement
> Components: lang.*
> Affects Versions: 3.1
> Reporter: Premysl Maly
> Priority: Minor
> Labels: patch
> Attachments: equalsUtils.patch
>
> Original Estimate: 0h
> Remaining Estimate: 0h
>
> Creating Utils class BigDecimalUtils and offering a new implementation of
> method equals() in ObjectUtils.
> Quoting the javaDoc from BigDecimalUtils:
> Provides some extra functionality over the BigDecimal class type Comparing
> two different BigDecimals may be tricky, because yet the value is same, the
> format may differ and then the equals method implemented on BigDecimal does
> not return expected results.
> BigDecimal first = new BigDecimal("0.10");
> BigDecimal second = new BigDecimal("0.100");
>
> first.equals(second); will return false, because those two numbers are not
> using the same format, even though the values are both "0.1".
> Usage examples:
> Compare a number to 1: if(BigDecimalUtils.equals(someNumber,
> BigDecimal.ONE))
> compare two numbers whether they are the same value or both null:
> if(BigDecimalUtils.equals(someNumber, anotherNumber, NullsEquality.EQUALS))
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira