Why re-invent the wheel? Apache Commons – Lang
While coding what we do most? I think checking the Object variable if it is not null before using it. There are various things which we do usually in our code like iterating an Array for deleting the item, Escaping Strings for a particular use, Adding days to the current date, Adding an item in Array, reversing the Array, remove a part of String etc. We can not avoid doing these things in our code and i think these are main reason of petty bugs in the application. Apache commons solve this problem in a very nice way. It has a set of projects dedicated to specific types of utilities. You can check the details on the prject page (http://commons.apache.org/) it self.
Here I am trying to list some of the very basic utilities of commons lang project which can save our application from petty issues and reduce the code size.
1. ArrayUtils: These functions can also be used for primitive types directly. Check the API page (http://commons.apache.org/lang/api-release/index.html)
ArrayUtils.add(Object[] array, Object element) //adds object to the array ArrayUtils.add(Object[] array, index, Object element) //adds object to the array at specified index ArrayUtils.addAll(Object[] array1, Object[] array2) //add all objects to the array and create combined array ArrayUtils.contains(Object[] array, Object element) //checks if object is part of array or not ArrayUtils.indexOf(Object[] array, Object elementToFind) //finds index of object in array ArrayUtils.remove(Object[] array, Object elementToremove) //removes object from the array
2. StringUtils: I find this really useful while handling Strings. Go through the API of this class. I am sure you can use this class and save a lot of time & bugs.
StringUtils.abbreviate(String str, int maxwidth); //use this while showing long strings on UI. it will use ellipse at the end. StringUtils.isBlank(String str); //Checks null and content and returns true in case only blanks are there is string StringUtils.isEmpty(String str);//Check null and returns true in case string is empty i.e. no blanks also. StringUtils.isNumeric(String str);// Checks if String is numeric StringUtils.trim(String str); // Trims the string to only the content and removes blanks StringUtils.isAlphnumeric(String str); //Checks if string is alphanumeric StringUtils.countMatches(String str, Sting sub);// Count number of matches of sub string in str.
3. StringEscapeUtils: Another possible source of error. Use this to avoid issues and confusions. Keep your string as it is and use these escape functions before using them accordingly.
StringEscapeUtils.escapeCsv(); // Escapes string for CSV StringEscapeUtils.escapeJava();// Escapes string for Java StringEscapeUtils.escapeSql();// Escapes string for Sql StringEscapeUtils.escapeXml();// Escapes string for XML StringEscapeUtils.unescapeCsv(); // Yes you can un-escape also![]()
4. DateUtils : A Utility wrapper on Date and Calendar objects and does provide many functions on it.
DateUtils.addDays(Date date, int amount); // Add days to the current date, see more in API for similar functions DateUtils.isSameDay(Calendar cal1, Calendar cal2); // Checks if cal1 and cal2 are same day
5. SystemUtils : Set of functions to find System properties
SystemUtils.IS_JAVA_1_4; SystemUtils.IS_OS_LINUX; SystemUtils.USER_TIMEZONE; SystemUtils.getJavaHome();
I am sure you can save time, code size and a lot of petty issues in code if you use commons. It is widely used by many open source projects.
Most Commented Posts
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments
No comments yet.
Leave a comment