commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Peter Henderson <peter.hender...@starjar.com>
Subject [lang] EqualsBuilder
Date Sat, 31 Jan 2009 17:23:30 GMT
Hi Folks, hopefully this question is appropriate for the dev list.


I have been benchmarking EqualsBuilder and run into what must be a common gotcha.


The docs for EqualsBuilder suggests that one construct the equals method:-

     @Override
     public boolean equals(Object obj) {
         if (obj instanceof MyClass == false) {
             return false;
         }
         if (this == obj) {
             return true;
         }
         MyClass rhs = MyClass obj;
         return new EqualsBuilder().
                 appendSuper(super.equals(obj)).
                 append(field1, rhs.field1).
                 append(fieldN, rhs.fieldN).
                 isEquals();
     }

The trouble with this is it will never work as expected if the object's parent does not implement

its own equals method, E.g. a simple pojo derived from Object.

The reason it fails to work as expected is that Object.equals() returns true if and only if
this and 
obj refer to the same object.


At the very least the javadoc needs to be modified to high light this. (Don't call 
appendSuper(super.equals(obj)) unless you know the parents implement equals.)

Would a better solution be to depricate appendSuper(boolean) and perhaps implement an 
appendSuper(Object lhs, Object rhs) which detects if super is Object and handles it appropriately.


Or am I barking up the wrong tree?

Peter.






---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message