Power Groovy DSL Features
[ 168 ]
id: 1001
street: 1 Rock Road
surname: Flintstone
Customer has following properties
city: Bedrock
firstName: Barney
id: 1002
street: 2 Rock Road
surname: Rubble""" == output()
Java libraries are full of classes that have been declared final. The library designers
in their wisdom have decided that the methods they have added are all that we will
ever need. Unfortunately, that is almost never the case in practice. Take the Java
String class, for example. There are plenty of useful string manipulation features
that we might like to have in the String class. Java has added methods progressively
to this class over time, for instance, match and split in Java 1.4, with replace and
format being added in Java 1.5.
If we needed these style methods before Sun got around to adding them, we could
not do it ourselves because of the final modifier. So the only option has been to use
classes from add-on libraries such as commons StringUtils. The Apache Commons
Lang component class contains a slew of useful classes that augment the basic
capabilities of Java classes, including BooleanUtils, StringUtils, DateUtils, and
so on. All of the util class methods are implemented as static, taking String as the
first parameter. This is the typical pattern used in Java when we need to mix in extra
functionality to an existing class:
@Grab(group='org.apache.commons', module='commons-lang3',
version='3.0')
import org.apache.commons.lang3.StringUtils;
public class StringSplitter {
public static void main(String[] args) {
if (!args) {
System.out.println("USAGE : StringSplitter string
seperator");
System.exit(0);
}
String [] splits = StringUtils.split(args[0], args[1]);
for (int i = 0; i < splits.length; i++) {
System.out.println("token : " + splits[i]);
}
}
}
http://www.ebook3000.com