Saturday, May 14, 2011

Implement equals method using commons-lang

Here we use EqualsBuilder to see whether the 2 objects are equal or not. Take the example of person:

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;

import java.io.Serializable;

public class Person implements Serializable {
private Long id;

private String name;

private String sirname;

public boolean equals(Object object) {
if (!(object instanceof Main)) {
return false;
}

if (object == this) {
return true;
}

Person person = (Person) object;
return new EqualsBuilder().append(this.id, person.id).append(this.name, person.name)
.append(this.sirname, person.sirname).isEquals();

// return EqualsBuilder.reflectionEquals(this, person);

}
}

No comments:

Post a Comment

Chitika