Introduction to Google Guava's Splitter class08 Oct 2011
Splitter class takes a common, and what should not have to be verbose, exercise and makes it easy and readable. Let us start with a scenario, you have a list of key value pairs that you need to parse into a map. We are also going to assume the input could be malformed with empty entries and extra spaces. For the sake of this example, let us use names and user ids.
"dave:123, john:314,, matt:989"
Now, how this is typically handled:
This ends up being rather verbose and also error prone. We will see how the
Splitter class makes this much easier and readable.
Starting with the basics
The simplest example of the
Splitter is parsing a standard comma separated list.
Pretty straightforward. In addition to a string separator, the
Splitter.on function can also use a
CharSequence, or a
Pattern. The API provides a convenience method
Splitter.onPattern that will turn a
String into a java
Utilities to sanitize the input
We saw in the initial scenario a poorly formatted string that required us to check for
null and trim at various points to clean up the input. The
Splitter class comes with the utility functions
trimResults to take away some of that boilerplate code.
Another benefit of using the utility functions to clean up the split string is readability. When someone is reading your code, it is much easier to understand what is happening when seeing
trimResults versus checks for
null, checks for empty string, and various trim calls.
Using the MapSplitter (new in Guava 10.0)
The MapSplitter allows you to take a list of key value pairs and easily turn it into a Java Map. Well, that sounds great for solving our initial scenario, let us see it in action.
That looks much better. We removed the loop, the input was sanitized, we threw out any empty strings, and we broke up the separated values into key/value pairs. It also reads a lot better:
Split a string on comma, omit empty strings, trim those results, then use colon as a key value separator. With these rules, split
This is much easier to read than our initial version. You can read the full documentation on the
Splitter class here.