java - Apache Commons equals/hashCode builder -


i'm curious know, people here think using org.apache.commons.lang.builder equalsbuilder/hashcodebuilder implementing equals/hashcode? better practice writing own? play hibernate? what's opinion?

the commons/lang builders great , have been using them years without noticeable performance overhead (with , without hibernate). alain writes, guava way nicer:

here's sample bean:

public class bean{      private string name;     private int length;     private list<bean> children;  } 

here's equals() , hashcode() implemented commons/lang:

@override public int hashcode(){     return new hashcodebuilder()         .append(name)         .append(length)         .append(children)         .tohashcode(); }  @override public boolean equals(final object obj){     if(obj instanceof bean){         final bean other = (bean) obj;         return new equalsbuilder()             .append(name, other.name)             .append(length, other.length)             .append(children, other.children)             .isequals();     } else{         return false;     } } 

and here guava:

@override public int hashcode(){     return objects.hashcode(name, length, children); }  @override public boolean equals(final object obj){     if(obj instanceof bean){         final bean other = (bean) obj;         return objects.equal(name, other.name)             && length == other.length // special handling primitives             && objects.equal(children, other.children);     } else{         return false;     } } 

as can see guava version shorter , avoids superfluous helper objects. in case of equals, allows short-circuiting evaluation if earlier object.equal() call returns false (to fair: commons / lang has objectutils.equals(obj1, obj2) method identical semantics used instead of equalsbuilder allow short-circuiting above).

so: yes, commons lang builders preferable on manually constructed equals() , hashcode() methods (or awful monsters eclipse generate you), guava versions better.

and note hibernate:

be careful using lazy collections in equals(), hashcode() , tostring() implementations. fail miserably if don't have open session.


note (about equals()):

a) in both versions of equals() above, might want use 1 or both of these shortcuts also:

@override public boolean equals(final object obj){     if(obj == this) return true;  // test reference equality     if(obj == null) return false; // test null     // continue above 

b) depending on interpretation of equals() contract, might change line(s)

    if(obj instanceof bean){ 

to

    // make sure run null check before     if(obj.getclass() == getclass()){  

if use second version, want call super(equals()) inside equals() method. opinions differ here, topic discussed in question:

right way incorporate superclass guava objects.hashcode() implementation?

(although it's hashcode(), same applies equals())


note (inspired comment kayahr)

objects.hashcode(..) (just underlying arrays.hashcode(...)) might perform badly if have many primitive fields. in such cases, equalsbuilder may better solution.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -