The Tragedy of the Commons-Lang

Increasingly, I’ve come to believe that the Jakarta commons-lang package is one of the most undervalued Jakarta packages out there.

Here are some of my favourite commons-lang classes:

The ArrayUtils class can perform add and remove operations on arrays. Take this ugly example:

String[] original = getStrings();
String newString = "One more string";

String[] newArray = new String[original.length + 1];
System.arraycopy(original, 0, newArray, 0, original.length);
newArray[newArray.length - 1] = newString;

Using ArrayUtils, that becomes:

String[] newArray = (String[]) ArrayUtils.add(
    getStrings(), "One more string");

Other great classes include the EqualsBuilder and HashCodeBuilder utilities for implementing those annoying equals() and hashCode() methods.

There’s a StopWatch class that you can use in place of nasty snippets like this:

long startTime = System.currentTimeMillis();
invokeMethodThatWeWantToTime();
long duration = System.currentTimeMillis() - startTime;
System.out.println("Took: " + duration + " milliseconds");

The SystemUtils class provides a convenient placeholder for all those standard system properties, such as the line.separator and user.home directory. And the Java temporary directory (as used by the File.createTempFile() method).

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply