commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Duncan Jones (JIRA)" <>
Subject [jira] [Commented] (LANG-809) Encapsulate trival equal test into a method to avoid repeating over and over.....
Date Sat, 22 Sep 2012 18:20:07 GMT


Duncan Jones commented on LANG-809:

Having given this some thought, I'm not sure there is an easy solution. Firstly, if we create
a method as you've suggested:

boolean possiblyEquals(Object lhs, Object rhs) {
   if (rhs == null) { return false; }
   if (lhs == rhs) { return true; } // definitely equal, but how to know?
   return (lhs.getClass() == rhs.getClass()); // possibly equal

We must express three possible results (they are equal, they are not equal, we are not sure
yet) with a binary value. Not only is this confusing, it also means we will continue to evaluate
the EqualsBuilder even if we already know the object is equal.

I then considered if we could add a new appendXXX method, but the best we can do is as follows:

public boolean equals(Object obj) {
   // Have to check this, otherwise we cannot cast
   if (obj.getClass() != getClass()) {
     return false;

   // Have to do this, in order to get access to fields
   MyClass rhs = (MyClass) obj;

   // Have to do this, otherwise NullPointerException when accessing fields
   if (rhs == null) {
     return false;

   return new EqualsBuilder()
                 // this method can then only do a basic (this == obj) check
                 .appendBasic(this, obj)
                 .append(field1, rhs.field1)
                 .append(field2, rhs.field2)
                 .append(field3, rhs.field3)

If you can think of an alternative, please comment! Otherwise, this might need to be closed
as won't fix (i.e. can't improve).
> Encapsulate trival equal test into a method to avoid repeating over and over.....
> ---------------------------------------------------------------------------------
>                 Key: LANG-809
>                 URL:
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.builder.*
>    Affects Versions: 3.1
>         Environment: Windows 7
>            Reporter: Colbert Philippe
>            Priority: Minor
>              Labels: Encapsulate, EqualsBuilder, in, test, trivial
> In class EqualsBuilder, the documentation gives sample code on how to use EqualsBuilder.
 The proper usage of class EqualsBuilder is a bit long.  My suggestion is to encapsulate the
following trivial test into a method inside the class EqualsBuilder.  You call it the new
method trivalTest(Object obj1, Object obj2).
>    // This is the code that should be put in a method to avoid repeating....
>    if (obj == null) { return false; }
>    if (obj == this) { return true; }
>    if (obj.getClass() != getClass()) {
>      return false;
>    }

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:

View raw message