“Every problem becomes childish when once it is explained to you”

Wednesday, June 29, 2011

Common Uses of Apache Commons-Part2(Builders)

The toString() method is an easy way to debug java objects. The toString code can be generated easily using IDEs. Thanks to the IDE. But when using agile methodology, it is a common thing to add/remove/rename the fields in Classes.This requires a rewrite of toString method. This can be avoided by using a reflection based toString builder from apache commons.The equals,hashcode and compareTo can be done in similar way. Due to some permission & performance issues, it is a good practice to use these builders without reflection.
It produces clean and maintainable code for you. Following examples explains the usage of such builders.

ToStringBuilder

ReflectionBased:-

public String toString() {
return ReflectionToStringBuilder.toString(this);
}


Without using Reflection:-

  public String toString() {
return new ToStringBuilder(this).
append("Name", name).
append("Age", age).toString();
}


EqualsBuilder

Using Reflection:-

public boolean equals(Object that) {
return EqualsBuilder.reflectionEquals(this, that);
}


WithoutReflection:-

public boolean equals(Object that) {
EqualsBuilder builder = new EqualsBuilder();
return builder.append(this.field1, that.field1)
.append(this.field2, that.field2)
.isEquals();
}


CompareToBuilder

Using Reflection:-

 public int compareTo(Object rhs) {
return CompareToBuilder.reflectionCompare(this, rhs);
}


WithoutReflection:-

public int compare(Object this, Object rhs) {
CompareToBuilder builder = new CompareToBuilder();
return builder.append(this.name, rhs.name).toComparison();
}


HashCodeBuilder

With Reflection:-

public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}


Without Reflection:-

public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
return builder.append(this.value1)
.append(this.value2)
.toHashCode();
}

8 comments:

  1. Great post dude, Can you also tell me how are you highlighting your code ?

    Javin
    10 example of find command in Unix

    ReplyDelete
  2. Which commons library contains these builders?

    ReplyDelete
  3. Apache Commons-Lang. The problem with the Apache libraries is that they haven't adopted Java 5 generics yet. Commons-Collections is especially frustrating that way. Google Guava has its own Objects class with a hashCode method and a toStringHelper method. Unfortunately, this itself clashes with the new Java 7 java.lang.Objects class, making imports tricky. Guava Objects.hashCode and 7's Objects.hash do the same thing.

    One could wish the base language and these libraries could all get along

    ReplyDelete
  4. @Javin Paul http://heisencoder.net/2009/01/adding-syntax-highlighting-to-blogger.html

    ReplyDelete
  5. @Eric Jablow I too wish that.Already very late.

    ReplyDelete
  6. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Apache Spark MLIB, kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor led training on Apache Spark MLIB. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us:
    Name : Arunkumar U
    Email : arun@maxmunus.com
    Skype id: training_maxmunus
    Contact No.-+91-9738507310
    Company Website –http://www.maxmunus.com


    ReplyDelete
  7. I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Apache Spark TECHNOLOGY , kindly contact us http://www.maxmunus.com/contact
    MaxMunus Offer World Class Virtual Instructor-led training on TECHNOLOGY. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ pieces of training in India, USA, UK, Australia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
    For Demo Contact us.
    Pratik Shekhar
    MaxMunus
    E-mail: pratik@maxmunus.com
    Ph:(0) +91 9066268701
    http://www.maxmunus.com/

    ReplyDelete