Tuesday, August 21, 2007

Usable stuff from commons-lang

As Java evolves, some parts of commons-lang are not that usable anymore. For instance its enum support. Others have never really appeared that helpful to me. But there are also other parts, that are always written again and again in each project, when we could just use commons-lang.

Here is what I find useful in commons-lang:
Hey, that is not a lot :-)

What a shame that we cannot add methods on existing classes in Java. Even though StringUtils has lots of methods, it seems to always lack some. In Ruby, I could just open the class definition and add new methods. I am not sure I would like that in Java (and it will never be able to, I know).

But C# in .Net 3.0 has something more controlled and type-safe. They have extension methods.

2 comments:

Iwan Memruk said...

ToStringBuilder and HashCodeBuilder are pretty useful from my experience (unlike EqualsBuilder). Also ObjectUtils for comparing two objects both of which might possibly be null.

Tech Per said...

@Iwan: Thanks for the comment Iwan.

With respect to ToStringBuilder, I used it a while back, but got burned by it automatically taking all properties into the toString. This meant it massaged my lazy-loaded relations when toString() was called, which tended to happen outside the open session (Hibernate specific problem). I know you can tell it which specific properties you like to include, but then I had to update it (which I forget).

With respect to HashCodeBuilder, I let IDEA auto-generate this, together with a equals that match.

The ObjectUtils one have completely slipped past me. I will have a look at it. Thanks for the tip.