Hi Laurent,
I have written the following test case, and everything seems to be working
fine.
(EqualsBuilder uses equals(), not '==').
I used commons-lang 2.3.
public class TesteEqualsBuilder {
@Test
public void testEqualsBuider() {
Long a = new Long(1);
Long b = new Long(1);
assertTrue(a.equals(b));
assertFalse(a == b);
assertTrue(new EqualsBuilder().append(a, b).isEquals());
}
}
Your class UiTheme extends Object, implicitly, since it does not extend
anything else.
Object.equals() uses equality of objects so, when you call appendSuper(
super.equals(object)),
you said you defined equality in your objects by being the same object.
In other words, you should not use this call, and there is nothing wrong
with Long equality.
The class' javadoc says the call appendSuper(super.equals(object)) would be
the standard way to use the class, but it seems to me it is usually not
desired, specially when you're overriding Object.equals() method.
a+
Rodrigo
2007/12/17, Laurent Perez [EMAIL PROTECTED]:
Well... I of course agree my manual == instead of equals() was
wrong, but I guess nobody else has ever used EqualsBuilder with Long
fields... hard to believe, but hey.
Still, I find EqualsBuilder Javadoc and implementation misleading,
http://commons.apache.org/lang/api/org/apache/commons/lang/builder/EqualsBuilder.html#append(java.lang.Object,%20java.lang.Object)
says it will use equals() method on objects, but when objects are
Long, it does not.
Or maybe this is a known pitfall of EqualsBuilder, and I just went in,
but this is still very confusing.
laurent
2007/12/17, Shawn Garner [EMAIL PROTECTED]:
I know there are tools/plugins to catch it.
Personally I don't make those mistakes (I make different ones) anymore.
It tends to be those who are new to Java or switch programming languages
frequently.
They are also not likely to use something like Findbugs.
Nothing beats a compiler warning ;)
Thanks,
Shawn D. Garner
Date: Mon, 17 Dec 2007 12:58:22 +
From: [EMAIL PROTECTED]
To: user@commons.apache.org
Subject: Re: [lang] equalsBuilder unexplained wrong equality with
java.lang.Longs ?
On 17/12/2007, Shawn Garner [EMAIL PROTECTED] wrote:
This is the number one programming error I've seen (not the only one
though).
Most of the time it is controlling logic that never gets executed.
I'm always curious as to why it is never caught and what it will do
when fixed.
Try using Findbugs - that will report the error...
The problem is other programming languages do compare this way. As
well as some expression languages.
IMO Java should treat == as object.equals on objects and
introduce another operator for object pointer comparison like .=
I don't think we'll ever see it though because it affects a lot of
existing source code making it not backwards compatible.
Shawn D. Garner
From: [EMAIL PROTECTED]
To: user@commons.apache.org; [EMAIL PROTECTED]
Date: Mon, 17 Dec 2007 04:32:44 -0500
Subject: RE: [lang] equalsBuilder unexplained wrong equality with
java.lang.Longs ?
Hello Laurent:
This behavior is 'normal'. The concepts of object equality (the
equals() method) and object identity (==) are different. The method '
equals()' defined in Object can be overridden by subclasses and defines if
an object is 'equal' to another. Long overrides the equals method to check
to see if the object passed in wraps the same primitive long value. OTOH,
the == operator compares two object identities, that is, is the object on
the left-hand side the same object as the one on the right-hand side. Even
though two objects instances may be semantically equal (as in the example
below), they are not the same object so fails the == comparison.
For example:
public class TestLongEquals extends TestCase {
public void testname() throws Exception {
Long long1a = new Long(1);
Long long1b = new Long(1);
Assert.assertFalse(long1a == long1b);
Assert.assertTrue(long1a.equals(long1b));
}
}
Gary
Seagull Software
-Original Message-
From: Laurent Perez [mailto:[EMAIL PROTECTED]
Sent: Sunday, December 16, 2007 3:17 PM
To: Jakarta Commons Users List
Subject: [lang] equalsBuilder unexplained wrong equality with
java.lang.Longs ?
Hi !
I'm struggling with a problem I don't understand, my
equalsBuilder's
isEquals seem to return false when dealing with Longs :
I have a bean class UiTheme, with a property private Long id.
The following equals builder will return false when this.id = 1
and
rhs.id = 1 :
public boolean equals(Object object) {
if (!(object instanceof UiTheme