Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Extend the JDK Classes with Jakarta Commons, Part I : Page 6

Explore the components in the Jakarta Commons set of reusable classes and you'll be convinced that most of them should be part of the JDK. Learn which ones you should use in your projects.


advertisement

Lang

This Lang API is an extension to the java.lang package. I hope Commons Lang becomes part of the JDK in the future. This package contains many utility methods that you can use in Java projects. (A comprehensive user guide is available at the Jakarta Web site, and the javadoc contains simple usage of most of the classes.) This article explains only the classes that I like the most and strongly feel that any developer would be interested to try. The source files for examples are provided in the in.co.narayanan.commons.lang package.

The StringUtils, WordUtils, StringEscapeUtils, and RandomStringUtils classes provide useful string-manipulation methods. The following listings show the usage of some of these methods, which frequently are required.

The following abbreviated method displays parts of the message if the display area in the GUI is limited (a JTable cell, for instance):



String message = "Please check the logs for an error."; message = StringUtils.abbreviate(message, 25);

The following method is very useful in desktop applications developed with Java. For instance, a Java Swing application that requires a feature to compose and send emails:

String message = "jakarta commons"; message = StringUtils.capitalize(message);

The following methods from the WordUtils classes are variants in capitalizing a string:

String message = "jakarta commons"; message = WordUtils.capitalize(message);

String message = "jakarta COMMONS"; message = WordUtils.capitalizeFully(message);

The javadoc contains explains the usage further.

This method is useful for positioning a message by filling lead and trail spaces:

String message = "java"; message = StringUtils.center(message, 10);

You no longer need to write code to tokenize a string for processing. The split method does it for you by returning a String array. You can create a new string from a String array using the join method:

String message = "hello how are you"; String messageTokens[] = StringUtils.split(message, ' '); String newMessage = StringUtils.join(messageTokens, '$');

Making a password in a String becomes easy with the overlay method. The replace method is intelligent enough to search for a word and replace it. You don't have to worry about character positions:

String message = "This is a demo for replace and overlay feature"; message = StringUtils.replace(message, "demo", "illustration"); message = "Your password is: somepassword"; message = StringUtils.overlay(message, "*****", 18, message.length());

Methods are available in the StringEscapeUtils class to escape and un-escape HTML, Java strings, JavaScript strings, SQL, and XML:

String html = "<h1>This will be displayed in the <i>HTML</i> page.
Hence the <b>tags</b> will be escaped.<h1>"; String escapedHtml = StringEscapeUtils.escapeHtml(html);

This method can be used to generate random passwords:

String randomPassword = RandomStringUtils.randomAlphanumeric(8); System.out.println("Random Password:" + randomPassword);

A brief note on each package is given below:

org.apache.commons.lang
The ArrayUtils, BooleanUtils, CharRange, CharSet, CharUtils, SystemUtils, and Validate classes are interesting and worth investigating in the javadoc.

org.apache.commons.lang.builder
The CompareToBuilder, EqualsBuilder, HashCodeBuilder, and ToStringBuilder classes assist in implementing the compareTo, equals, hashCode, and toString methods, respectively. Almost all Java projects require these classes.

org.apache.commons.lang.enums
Enum support is available in Java 1.5. Hence, this package can be useful for pre-JDK 1.5 projects. The package's javadoc contains an example that illustrates its use.

org.apache.commons.lang.exception
The NestableException class provides support to link the cause for an exception. Why is this useful? The stack trace of all exceptions can be handy for quickly determining the root cause of an error. This can be used for pre-JDK1.4 projects. Since 1.4, Java has included support for nested exceptions.

org.apache.commons.lang.math
The IntRange, Fraction, NumberUtils, and RandomUtils classes are interesting and the worth investigating in the javadoc.

org.apache.commons.lang.mutable
The wrapper for primitive types in the java.lang package is immutable. This package contains wrapper classes, which are mutable. The javadoc provides further explanation.

org.apache.commons.lang.time
Some of this package's useful classes are DateUtils, DataFormatUtils, FastDateFormat, and StopWatch. The javadoc provides further explanation.

So What Have You Learned?

Now that you've completed this first installment of the Jakarta Commons series, you should have learned the following:
  • The rich features provided by various Commons components
  • Where you can use a particular component or method from a utility class in a real-world Java project
  • The design patterns various APIs use
  • A high-level understanding of the packages and classes in various APIs

Now, during Java application design or development, you'll be in a position to pick a useful class and use it appropriately. Parts II and III of this series will discuss more interesting components. By the end, you too will be convinced that most of the classes in the Jakarta Commons components should be part of JDK.



Narayanan A.R. is a passionate advocate of test-driven development, agile methodologies, Java technology, and design patterns. He has several years of experience in software design and development using Java technologies.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap