commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rodrigo Canabrava" <canab...@gmail.com>
Subject Re: [lang] equalsBuilder unexplained wrong equality with java.lang.Longs ?
Date Mon, 17 Dec 2007 17:22:36 GMT
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 <hakimm@gmail.com>:
>
> 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 <shawndgarner@hotmail.com>:
> >
> > 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 +0000
> > > From: sebbaz@gmail.com
> > > To: user@commons.apache.org
> > > Subject: Re: [lang] equalsBuilder unexplained wrong equality with
> java.lang.Longs ?
> > >
> > > On 17/12/2007, Shawn Garner <shawndgarner@hotmail.com> 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: GGregory@seagullsoftware.com
> > > > > To: user@commons.apache.org; commons-user@jakarta.apache.org
> > > > > 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:hakimm@gmail.com]
> > > > > > 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)) {
> > > > > >                         return false;
> > > > > >                 }
> > > > > >                 UiTheme rhs = (UiTheme) object;
> > > > > >                 return new
> > > > > > EqualsBuilder().appendSuper(super.equals(object)).append(this.id
> ,
> > > > > > rhs.id).isEquals();
> > > > > >         }
> > > > > >
> > > > > > If I manually do the equality, as in this.id.equals(rhs.id),
> then it
> > > > > > is true (because, well... 1 = 1 !)
> > > > > > If I do an equality like this.id == rhs.id, then it is false
> (being a
> > > > > > Java newbie... maybe it should be false, not sure if doing ==
> instead
> > > > > > of equals(obj) is correct with Longs)
> > > > > >
> > > > > > So I assume (am I wrong ?) that equalsBuilder's isEquals uses
> "=="
> > > > > > instead of "equals(obj)"... but I really, really don't
> understand why.
> > > > > >
> > > > > > Can anyone enlighten me ?
> > > > > >
> > > > > > thanks !
> > > > > > --
> > > > > > <a href="http://in-pocket.blogspot.com">
> http://in-pocket.blogspot.com
> > > > > > - Mobile world, technology and more</a>
> > > > > >
> > > > > >
> ---------------------------------------------------------------------
> > > > > > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > > > > > For additional commands, e-mail: user-help@commons.apache.org
> > > > >
> > > > >
> > > > >
> ---------------------------------------------------------------------
> > > > > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > > > > For additional commands, e-mail: user-help@commons.apache.org
> > > > >
> > > >
> > > > _________________________________________________________________
> > > > Share life as it happens with the new Windows Live.
> > > >
> http://www.windowslive.com/share.html?ocid=TXT_TAGHM_Wave2_sharelife_122007
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > > For additional commands, e-mail: user-help@commons.apache.org
> > >
> >
> > _________________________________________________________________
> > The best games are on Xbox 360.  Click here for a special offer on an
> Xbox 360 Console.
> > http://www.xbox.com/en-US/hardware/wheretobuy/
>
>
> --
> <a href="http://in-pocket.blogspot.com">http://in-pocket.blogspot.com
> - Mobile world, technology and more</a>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message