Adept Open Source Library

This is a blog to provide in-depth information on the Adept open source library - the core of which is a Java object database component. In addition there is lots of Java code and solutions for many development problems.

http://marringtons.com

Wednesday, April 20, 2005

String Utilities

The Strings class in Java is very special. Not only can instances be created by putting double-quotes around text, but it is allowed to use special overloaded operators (such as plus-sign) that the reset of us can't implement without reverting to C++. Hmmm, I sound bitter. Well, only slightly - after all we sometimes have to live with the tools we are given.

The String class is also final - which means you cannot add functionality. This is a shame because it doesn't do everything it could to make life easy. The Adept library contains a com.marringtons.string.Strings class of static methods for providing tools to make strings a little easier to work with.

There are methods to make string tests easier - even if the reference is null. There are methods to split strings and others to join them again. There's a method to create an array of strings from a collection of objects and another to create the array from a regular expression matcher. Lastly, there is a method to order strings by length (Strings.sortDescendingByLength).

Null Strings

It's not uncommon to have a empty string as either a null reference or a reference to a string with no content. This is not a problem with Java 1.5 and above, but in earlier code the tests all looked unwieldy and there was a risk of NullPointerException occurring - unchecked to the interface.
if (name != null  &&  name.length() > 0)
  name = askForName();

 

Strings provides a more readable alternative:
if (Strings.isEmpty( name))
  name = askForName();

if (Strings.isNotEmpty( name))
  sayHello( name);

 

The other related problem is the good old toString() object. When it is called explicitly it gets very upset if the object is null. Strings to the rescue:
Object response = getResponse();

if (response != null)
  name = response.toString();
else
  name = "";

// ... is the same as ...

name = Strings.toString( response);

 

Splitting Strings

Java.util.regex.Pattern has a split method that separates a string into an array of strings based of a separator pattern. Strings provides some convenience methods for comma separated and OO named lists. The adept library uses comma separated variables (csv) from the properties file and from HTTP forms. The OO version is often used to map form data to classes as part of form processing.
String[] parts = Strings.split( "ab, cd, ef");
parts = Strings.splitJava( "Java.util.regex.Pattern");

 

Joining Strings

Another common function is to create a string from component parts - joining them.
String[] parts = { "a", "b", "c", "d" }
string = Strings.join( parts); // a, b, c, d
string = Strings.join( parts, "."); // a.b.c.d
string = Strings.join( parts, ".", 1); // b.c.d
string = Strings.join( parts, ".", 1, 3); // b.c

 

There is also a version of the second method that takes a list for when the string array is not yet an array.

String Arrays

It's not uncommon to collect a list of strings - without knowing how many there will be - and then return an array of them.
public String[] listArchive() throws IOException
  {
    ArrayList files = new ArrayList();
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements())
      files.add( ((ZipEntry) enumeration.nextElement()).getName());
    return Strings.toArray( files);
  }

 

There is also a method for generating a string array given a string and regular expression.
Pattern pattern = Pattern.compile( "(\\d+)\\D+(\\d+)");
Matcher matcher = pattern.matcher( "123<==>456");
check( matcher.matches());
String[] strings = Strings.toArray( matcher);
check( strings.length == 2);
check( strings[0].equals( "123"));
check( strings[1].equals( "456"));

 

0 Comments:

Post a Comment

<< Home