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.

Henri Yandell made changes - 09/Jan/08 09:28 AM
Field Original Value New Value
Fix Version/s 2.4 [ 12312481 ]
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 made changes - 11/Jan/08 06:15 AM
Attachment LANG-393.patch [ 12372959 ]
Repository Revision Date User Message
ASF #611543 Sun Jan 13 07:00:22 UTC 2008 bayard Applying my patch from LANG-393; EqualsBuilder uses compareTo for BigDecimal and not equals
Files Changed
MODIFY /commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java
MODIFY /commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java

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.


Henri Yandell made changes - 13/Jan/08 07:00 AM
Resolution Fixed [ 1 ]
Status Open [ 1 ] Closed [ 6 ]
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.

Joerg Schaible made changes - 27/Oct/08 03:57 PM
Link This issue duplicates LANG-467 [ LANG-467 ]
David Jones made changes - 27/Oct/08 04:50 PM
Link This issue duplicates LANG-467 [ LANG-467 ]
Henri Yandell made changes - 05/Nov/08 03:58 PM
Link This issue is related to LANG-468 [ LANG-468 ]
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.

Repository Revision Date User Message
ASF #836149 Sat Nov 14 10:10:19 UTC 2009 bayard Rolling back r611543 for LANG-393, and removing the special handling of BigDecimal to use compareTo instead of equals because it creates an inequality with HashCodeBuilder [reported in LANG-467 by David Jones]
Files Changed
MODIFY /commons/proper/lang/trunk/src/test/org/apache/commons/lang/builder/EqualsBuilderTest.java
MODIFY /commons/proper/lang/trunk/src/java/org/apache/commons/lang/builder/EqualsBuilder.java

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.