Issue Details (XML | Word | Printable)

Key: LANG-393
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Luis Henriques
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Commons Lang

EqualsBuilder don't compare BigDecimals correctly

Created: 03/Jan/08 01:46 PM   Updated: 14/Nov/09 10:11 AM
Return to search
Component/s: None
Affects Version/s: 2.3
Fix Version/s: 2.4

Time Tracking:
Not Specified

File Attachments:
  Size
Text File Licensed for inclusion in ASF works LANG-393.patch 2008-01-11 06:15 AM Henri Yandell 2 kB
Issue Links:
Reference
 

Resolution Date: 13/Jan/08 07:00 AM


 Description  « Hide
When comparing a BigDecimal, the comparing is made using equals, not compareTo, which is more appropriate in the case of BigDecimal.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Henri Yandell added a comment - 09/Jan/08 09:28 AM
From the BigDecimal javadoc:

"Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). "

So I can definitely see that in BigDecimal's case, most users would want compareTo to be used.


Henri Yandell added a comment - 09/Jan/08 09:28 AM
Need to create unit test/fix.

Gary Gregory added a comment - 09/Jan/08 06:06 PM
Hm... is there a way to refactor our code so that the Equals and CompareTo be mixed and matched? At least, it sounds we need some kind of configurable map to tell us which class should use compareTo.

Henri Yandell added a comment - 10/Jan/08 04:16 AM
Do we need it?

This is our first compareTo instance. Looks like we need an if instanceof BigDecimal inside the if(!lhsClass.isArray()) block of append(Object, Object).


Gary Gregory added a comment - 10/Jan/08 07:59 AM
Strictly XP-speaking, no. You are correct, we should start with the required case and refactor and generalize only if needed.

Henri Yandell added a comment - 11/Jan/08 06:15 AM - edited
Suggested fix attached. How does it look?

Henri Yandell added a comment - 13/Jan/08 07:00 AM
svn ci -m "Applying my patch from LANG-393; EqualsBuilder uses compareTo for BigDecimal and not equals" src

Sending src/java/org/apache/commons/lang/builder/EqualsBuilder.java
Sending src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java
Transmitting file data ..
Committed revision 611543.


Nicolas Mommaerts added a comment - 01/Oct/08 01:22 PM - edited
The following simple test case shows that the applied patch throws a ClassCastException:
public static void main(String[] args) {
class Test { public Object o; }

BigDecimal d = new BigDecimal(1000);
Long l = new Long(1000);

Test t1 = new Test();
Test t2 = new Test();

t1.o = d;
t2.o = l;

System.out.println(EqualsBuilder.reflectionEquals(t1, t2));
}


Stephen Colebourne added a comment - 01/Oct/08 09:41 PM
I hadn't spotted this one. While I understand the motivation, I do think that EqualsBuilder should use .equals(). There are people who will consider that to be the only valid equals comparison (all the state of the object being equal).

Matt Benson added a comment - 01/Oct/08 09:48 PM
@Stephen: Might be late now; @Nicolas: The compilation error only happens with Java 5, so yes, when [lang] moves to Java 5 it must be corrected.

Nicolas Mommaerts added a comment - 02/Oct/08 05:57 AM
@Matt: You are right, I should have mentioned that (java5). But don't you think it should be fixed now? It is not a compilation error but a runtime error, and it occurs for everyone using Java 5 in their project while using this library.

Henri Yandell added a comment - 07/Nov/08 12:04 AM
Btw - this is fixed in trunk now. Separate issue is whether this change needs to be reverted or not.

Henri Yandell added a comment - 14/Nov/09 10:11 AM
Note that due to inconsistency with HashCodeBuilder and hashCode in general, this fix has been reverted and will go back to its old behaviour of using equals() in Lang 3.0.