Guava Splitter

Guava Splitter is a utility class that is offering roughly speaking the opposite of what the Joiner utility class does.

You can have a read at my previous quick article about Guava Joiner.

Splitter is offering similar look-and-feel as Joiner: the on method serves as a static factory method, subsequent method calls are following the builder pattern returning a wrapped this (therefore need to be packed prior the final split method call). Finally it doesn’t try to be complicated returning an Iterable of Strings. It is aspiring in replacing the usage of String.split method.

Its usage when we want to get back an Iterable of Strings looks like:

 
Splitter.on(",").trimResults().split("my,string,to,split")

When we want to perform the operation retrieving a Map out of its String representation:

Splitter.on(",").trimResults().omitEmptyStrings().withKeyValueSeparator("->").split("one->1,two->2,three->3")

In this instance the call to withKeyValueSeparator is yielding a Splitter.MapSplitter inner class that is adding the map context to the splitter.

Advertisements

Guava Joiner

Guava Joiner is a nice utility class that is String-ing data structure contents, useful for debugging, logging, reporting or toString-ing activities in a flexible, builder pattern-esque and fluent way.

What we used to achieve with this conventional code:

public static final String joinIterable(final Iterable iterable, String delimiter){
   if(iterable == null)
      throw new IllegalArgumentException();
   StringBuilder result = new StringBuilder("Conventional iterable join: ");
   for(T elem : iterable){
      result.append(elem).append(delimiter);
   }
   result.setLength(result.length() - 1);
   return result.toString();
}

public static final String join(final List list, String delimiter){
   return joinIterable(list, delimiter);
}

can now be achieved with:

Joiner.on(",").useForNull("null").join(list)

The Joiner class is a utility class and the on method represents a static factory method that returns a new Joiner. The useForNull method follows the builder pattern returning a wrapped version of this (therefore need to be packed prior the final join method call). Be carefull that this call is needed otherwise null elements will throw a NPE. The join method is passing the Iterable in this case data structure.

Similarly for maps, what we used to achieve with this:

    public static final <K,V> String join(final Map<K,V> map, String delimiter){
        if(map == null)
            throw new IllegalArgumentException();
        StringBuilder result = new StringBuilder("Conventional map join: ");
        for(K key : map.keySet()){
            result.append(key).append("->").append(map.get(key)).append(delimiter);
        }
        result.setLength(result.length() - 1);
        return result.toString();
    }

can now be achieved by:

Joiner.on(",").withKeyValueSeparator("->").useForNull("null").join(map)

In this instance, the withKeyValueSeparator method call is returning a Joiner.MapJoiner that is a static class that is adding the map context to the Joiner class.