java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to implement equals method using commons-lang?

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

import java.io.Serializable;

public class BookEqualsExample implements Serializable
{
    private Long id;
    private String title;
    private String author;

    //~ Impelments geter/setter here

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

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

        BookEqualsExample book = (BookEqualsExample) object;
        return new EqualsBuilder()
                .append(this.id, book.id)
                .append(this.title, book.title)
                .append(this.author, book.author)
                .isEquals();

        // You can also use reflection of the EqualsBuilder class.
        // return EqualsBuilder.reflectionEquals(this, book);

    }
}