Sunday, June 12, 2011

Benefits of code reuse with Apache Commons Lang classes


Learn the benefits of code reuse with these four Apache Commons Lang classes
EqualsBuilder, HashCodeBuilder, ToStringBuilder, CompareToBuilder
The Commons Lang library comes with a handy set of classes, collectively known as builders.


All Java classes implicitly inherit from java.lang.Object. And as you probably already know, the Object class has three methods that are generally meant to be overridden:
      equals
      hashCode
      toString

Building equals
The equals and hashCode methods are special in that other aspects of the Java platform, such as collections and even persistence frameworks (including Hibernate), depend on these two methods to be properly implemented.

When it comes to implementing the equals method, the Commons Lang EqualsBuilder is useful. This class is simple to grasp. It essentially has two methods to know about: append and isEquals. The append method takes two properties: one of the underlying object and the same one of the object being compared. Because the append method returns an instance of the EqualsBuilder, you can chain successive calls and compare all desired properties of an object. And finish the chain by calling the isEquals method.

Reusing EqualsBuilder
return new EqualsBuilder().append(this.id, account.id)
  .append(this.firstName, account.firstName)
   .append(this.lastName, account.lastName)
    .append(this.emailAddress, account.emailAddress)
     .append(this.creationDate, account.creationDate)
      .isEquals();


Note too that if want to streamline equals method and write as little code as possible (which means less code to maintain), you can leverage the power of reflection and write the code as below:

Using
EqualsBuilder's reflection API
public boolean equals(Object obj) {
 return EqualsBuilder.reflectionEquals(this, obj);
}


Building hashCode
The Commons Lang library provides a HashCodeBuilder class that operates almost identically to the EqualsBuilder. But instead of comparing two properties, it appends a single property to generate an integer -

Implementing a hashCode method with HashCodeBuilder
public int hashCode() {
 return new HashCodeBuilder(11, 21).append(this.id)
  .append(this.firstName)
   .append(this.lastName)
    .append(this.emailAddress)
     .append(this.creationDate)
      .toHashCode();
}


Like the EqualsBuilder, the HashCodeBuilder has another API that leverages reflection. With it, you don't need to add each property of the underlying object manually with an append method, resulting in a hashCode method like the one in Listing below:

Using the
HashCodeBuilder's reflection API
public int hashCode() {
 return HashCodeBuilder.reflectionHashCode(this);
}


Building toString
The default implementation of Object's toString method returns the fully qualified name of the object followed by a @ character and then the value of the object's hash code.

The ToStringBuilder acts the same way as the other three classes we've covered. Need to create an instance of it, append some properties, and call toString. That's it.

Using the ToStringBuilder
public String toString() {
 return new ToStringBuilder(this).append("id", this.id).
  .append("firstName", this.firstName)
   .append("lastName", this.lastName)
    .append("emailAddress", this.emailAddress)
     .append("creationDate", this.creationDate)
      .toString();
}

Want to also leverage reflection, use this :

Using
ToStringBuilder's reflection API
public String toString() {
 return ToStringBuilder.reflectionToString(this);
}


Building compareTo
Commons Lang provides an aptly named CompareToBuilder that functions almost identically to the EqualsBuilder. It includes a chainable append method, and you can ultimately return an int via the toComparison method.
Using the CompareToBuilder
public int compareTo(Object obj) {
 Account account = (Account) obj;
 return new CompareToBuilder().append(this.id, account.id)
  .append(this.firstName, account.firstName)
   .append(this.lastName, account.lastName)
    .append(this.emailAddress, account.emailAddress)
     .append(this.creationDate, account.creationDate)
      .toComparison();
}

If you really want to cut down on your coding, you can always leverage the reflection-style CompareToBuilder API :

Using
CompareToBuilder's reflection API
public int compareTo(Object obj) {
 return CompareToBuilder.reflectionCompare(this, obj);
}


Wants to know more, follow this url -

No comments:

Post a Comment