Jakarta Commons Lang Builders

I have mentioned different Jakarta libraries before. I described the Jakarta Common BeanUtils library in detail, describing how it is useful when accessing dynamic JavaBeans at runtime. I also scratched at the surface of Jakarta Commons IO with some code samples of FileUtils. Now I want to go over three classes made available from the Jakarta Commons Lang library.

The Java base class Object has three methods (toString, equals, hashCode) that programmers often override in their own classes. Just about anyone can safely override toString. As a general rule of thumb, it is recommended that if you override the equals method you also override the hashCode. These three methods are just too common, repetitive, and boring to implement and Jakarta Commons Lang library has builder classes to help you quickly and safely implement them.

For the following code samples, lets assume we have defined a simple Person class with a name and age field of type String and int respectively. We’ll also assume that the methods are defined inside said class.

Implementing toString
The ToStringBuilder class allows for a consist implementation of the toString method. To use it, just instantiate a object of ToStringBuilder and append the displayable field name and value. This method might remind you of StringBuilder. Here is a code example of how to use ToStringBuilder, modified from the Jakarta Commons Lang documentation.

[source:java]
public Strign toString() {
return new ToStringBuilder(this)
.append(“name”, name)
.append(“age”, age)
.toString();
}
}
[/source]

If an instance of our Person class would model someone named John who is 20, then the the toString mehthod would produce the following.

techknow.Person@61de33[name=John,age=20]

In my mind, that still looks geeky but fortunately for use the string builder accepts a ToStringStyle object as a parameter. Just subclass ToStringStyle and override a few methods to tweak the toString output.

Implementing equals
The EqualsBuilder class is helpful to implement the basic equals method. Continuing with our example of a basic Person POJO object with a name and age field, we could do something as follows.

[source:java]
public boolean equals(Object obj) {
if (obj instanceof Person == false) {
// Not even the same class
return false;
}
if (this == obj) {
// The same object reference
return true;
}
Person rhs = (Person)obj;
return new EqualsBuilder()
.appendSuper(super.equals(obj))
.append(name, rhs.name)
.append(age, rhs.age)
.isEquals();
}
[/source]

Implemeting hashCode
As you can gather by now, we can use the HashCodeBuilder class to quickly implement the hashCode method. Again, if for a given class you override the equals method you also need to override hashCode. Following our example, for a simple Person class, we can code something like the following.

[source:java]
public int hashCode() {
// Pick a hard-coded, randomly chosen, non-zero,
// odd number ideally different for each class
return new HashCodeBuilder(17, 37)
.append(name)
.append(age)
.toHashCode();
}
[/source]

One note, the code in these examples is based on version Commons Lang 2.2. All of the code example above are based on the Commons Lang API documentation. Consult your documentation, use as directed.

Technorati Tags: , , , , , , , , ,


One Response to “Jakarta Commons Lang Builders”

  • Lance Finney Says:

    I often use the following boilerplate in my classes (I don’t know how formatting on this blog will turn out):

    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }
    
      return EqualsBuilder.reflectionEquals(this, o);
    }
    
    public int hashCode() {
      return HashCodeBuilder.reflectionHashCode(this);
    }
    
    public String toString() {
      return ToStringBuilder.reflectionToString(this);
    }
    

    The convenience static methods here are very nice. I use them first, and then fall back to the more basic API you mention.

Leave a Reply