First Look at Commons Lang 3.0 Beta
Join the DZone community and get the full member experience.
Join For FreeApache Commons Lang 3.0 Beta was released on 4th August and the project has finally moved to Java 5. The API is now generified, varags are used where applicable, and some features which are now supported by Java itself have been removed. All of this means that Lang 3.0 is not backwards compatible with the 2.x versions. To make it workable with the previous versions, the package name has been changed to 'apache.commons.lang3', allowing Lang 3.0 to sit side-by-side with previous version of Lang without any side effects. You can download the Commons Lang 3.0 beta from this link.
In this article, I will walk you through some of the features that have been added or improved in 3.0 release.
ArrayUtils
The ArrayUtils class allows you to do operations on arrays, primitive arrays, and primitive wrapper arrays. This class exists since version 2.0 and in rhw 3.0 release it has been generified and a new method called toArray has been added. Lets take a look at its features:
- toArray method
This is a new method added in release 3.0 which lets you create a type-safe generic array.This method is very useful because arrays cannot be created from a generic type. For more information on why arrays can't be created for a generic type, refer to this link. This method uses the fact that varags are implemented as arrays, so this methods take a varags of T type and returns an array of T.String[] array = ArrayUtils.toArray("shekhar", "gulati");
Another use of toArray method is to create generic empty array. - toMap method
This method exists since the 2.x days and has been generified in 3.0 release.The result of generifying this method is that you get type safe maps, hence you avoid ClassCastExceptions. For example, in 2.x versionsMap map = ArrayUtils.toMap(new String[][]{{"shekhar","gulati"}});
- Other methods like subarray,addAll, add, etc are also generified.Hence they are now typ-safe.
String[] array = ArrayUtils.<String> toArray();
But in 3.0 version,
Map<String, Integer> map2 = ArrayUtils.<String,Integer>toMap(new Object[][]{{"shekhar",1}});
CharSequenceUtils
This class is a new class, added in the 3.0 version. This class allows you to do Null-safe operations on CharSequence. It has two methods:
- length method
This method gives you the length of any CharSequence.CharSequenceUtils.length(CharBuffer.wrap("shekhar")); // return 7
CharSequenceUtils.length("shekhar"); // return 7
CharSequenceUtils.length(new StringBuilder("shekhar").append("gulati"));//return 13 - subSequence method
This method returns a new CharSequence starting with char specified at the index.CharSequenceUtils.subSequence("test", 1);
will return est
EnumUtils
The package 'org.apache.commons.lang.enums' has been removed from release 3.0 because from version 5 of Java, Enums are provided out of the box. A new class EnumUtils has been added which provides helper methods for Java enums. Lets take a look at some of its methods:
- getEnumMap method
This method gives you a map of enums names and enums.enum ReportType{
REPORT1,REPORT2
}Map<String, ReportType> enumMap = EnumUtils.getEnumMap(ReportType.class);
- getEnumList method
This method gives you a list of enums for a given enum class.List<ReportType> enumList = EnumUtils.getEnumList(ReportType.class);
- isValidEnum method
This method checks if the given enum name is valid for given enum class.EnumUtils.isValidEnum(ReportType.class, "REPORT1");// will return true
EnumUtils.isValidEnum(ReportType.class, "REPORT3"); // return false
ObjectUtils
ObjectUtils class methods like min,max, clone etc are generified which makes them type-safe. A new method firstNonNull(T...) is added. This method returns the first value in the array which is not null.
ObjectUtils.firstNonNull(null, null,null,null, "abc", null,"def");//will return "abc"
StringUtils
StringUtils does not need any introduction. I think every Java developer knows it. Some method signatures have changed to support CharSequence instead of String. For example, isEmpty(String) is changed to isEmpty(CharSequence). Some new methods have also been added :
- stripAccents method
This method removes the accents from the string. This method works with JDK 1.6 method, and will fail on 1.5 version.StringUtils.stripAccents("&ecute;clair") ; //will return "eclair"
- normalizeSpace method
This function returns the input string with whitespace normalized by using trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.StringUtils.normalizeSpace(" a b c ") ; // will return "a b c"
Opinions expressed by DZone contributors are their own.
Comments