Apache Commons logo Commons Lang

What's new in Commons Lang 2.5?

Commons Lang 2.5 is out, and the obvious question is: "So what? What's changed?".

This article aims to briefly cover the changes and save you from having to dig through each JIRA issue to see what went on in the two years of development between Lang 2.4 and 2.5.

Two years?!? Yes, it's true. The reason is that 2.5 represents the backwards compatible changes in the nearly complete Java-5 focused Lang 3.0.


There were no new deprecations in 2.5.

The build

2.5 was built using Sun's 1.6.0_17 JVM, but targets Java 1.3.

New classes

A new org.apache.commons.lang.reflect package was added, accumulating common high-level uses of the java.lang.reflect APIs. The classes, hopefully self-evident in nature, were pulled together from the existing BeanUtils and the unreleased Reflect components. The classes are:

  • ConstructorUtils - primarily creating new instances of classes
  • FieldUtils - primarily reading and writing to Object/Class fields
  • MethodUtils - primarily methods to make invoking methods simpler

You can read more about the classes in their javadoc.

New fields

With both Java 7 and Windows 7 becoming a reality, SystemUtils was updated to provide boolean fields for both versions.

New methods

There were 66 new methods added to existing Commons Lang classes.

The ArrayUtils class received two new types of methods. Firstly, a boolean isNotEmpty(array) set of methods, identifying whether the particular array is null or an empty sized array. This makes it the inverse of the existing isEmpty(array) methods. Secondly, an array nullToEmpty(array) set of methods that converts null or empty arrays to a singleton empty array already available from the ArrayUtils class. Non null/empty arrays are left untouched.

The constructor for the CharRange class is somewhat confusing. It takes a boolean parameter that when set to true means the CharRange is negated. To make code easier to read, the following static helper methods were added:

  • public org.apache.commons.lang.CharRange is(char)
  • public org.apache.commons.lang.CharRange isIn(char, char)
  • public org.apache.commons.lang.CharRange isNot(char)
  • public org.apache.commons.lang.CharRange isNotIn(char, char)

An iterator() method was also added to provide another way of walking the range.

The EqualsBuilder obtained a new reset() method to allow for reuse, while the HashCodeBuilder received a hashCode() method that returns the built hash code instead of the natural hash code of the builder object itself. It doesn't really matter what the builder chooses to use as a hash code and this stops accidental use of the hashCode() instead of toHashCode() method from causing lots of pain.

Helper isFalse(), isTrue() and toBoolean() methods were added to MutableBoolean, while the other mutable classes received String argument constructors.

Lastly, the DateUtils class received a new ceiling set of methods to truncate upwards, and a parseDateStrictly method to parse a Date with the supplied DateFormat classes leniency set to false.

StringUtils methods

As with 2.4, the StringUtils class has grown and we cover its new methods in its own section.

  • abbreviateMiddle(String, String, int);String - This method turns aRatherLongNameSuchAsAFileName into 'aRatherLo...AFileName'. This is often desirable when you want to restrict the length of a name, but you can afford to have quite long names.
  • indexOfIgnoreCase(String, String);int - An indexOf method that ignores the case of what it's matching. Matching lastIndexOfIgnoreCase and 'start at index' variants were also added.
  • lastOrdinalIndexOf(String, String, int);int - A matching variant for the already exisitng ordinalIndexOf method - they support finding the Nth indexOf instead of the first time the search term is found.
  • isAllLowerCase(String);boolean - Is the String all lower case.
  • isAllUpperCase(String);boolean - Is the String all upper case.
  • lowerCase(String, Locale);String - Null protected toLowerCase methods for the platform independent inclined.
  • upperCase(String, Locale);String - Null protected toUpperCase methods for the platform independent inclined.
  • repeat(String, String, int);String - Repeat option that includes an optional separator.
  • startsWithAny(String, String[]);boolean - Does the specified String start with any of the supplied values.

What's fixed in Lang 2.5?

Per the release notes there are 32 bugs fixed in Lang 2.5. Some highlights are:

  • LANG-477 - fixing an OutOfMemoryError in ExtendedMessageFormat.
  • LANG-76 - EnumUtils.getEnum() doesn't work well in 1.5.
  • LANG-204 and LANG-506 - Multithread improvements to the package private Entities class, used behind the scenes by StringEscapeUtils.
  • LANG-511 - Improve performance by deferring LocaleUtils initialization.
  • LANG-523 - Two orders of magnitude performance improvement in StrBuilder.
  • LANG-467 - Reverted the change to EqualsBuilder in Lang 2.4 to specially handle BigDecimal. While useful, it put things out of sync with HashCodeBuilder.
  • LANG-586 - Use of a ThreadLocal in HashCodeBuilder and ToStringStyle meant that containers could end up with memory leaks. This was rewritten to avoid this.
  • LANG-472 - RandomUtils.nextLong() was returning only even numbers. Fans of Java-based roulette wheels can breathe a sigh of relief.

So long, farewell...

Hopefully that was all of interest. Don't forget to download Lang 2.5, or, for the Maven repository users, upgrade your <version> tag to 2.5. Please feel free to raise any questions you might have on the mailing lists, and report bugs or enhancements in the issue tracker.