Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Kenneth A. Kousen - Making Java Groovy - 2014.pdf
Скачиваний:
50
Добавлен:
19.03.2016
Размер:
15.36 Mб
Скачать

302 APPENDIX B Groovy by feature

Sex at noon taxes

Flee to me, remote elf!

Doc, note: I dissent. A fast never prevents a fatness. I diet on cod.

'''

palindromes.eachLine {

String str = it.trim().replaceAll(/\W/,'').toLowerCase() assert str.reverse() == str

}

Once again, a little Groovy code packs a lot of power. The method eachLine has been added to the String class to break multiline strings at line breaks. It takes a closure as an argument. In this case, no dummy variables were used in the closure, so each string is assigned to the default variable called it.

THE IT VARIABLE In a closure, if no dummy name is specified the term it is used by default.

The trim method is applied to the line to remove any leading and trailing spaces. Then the replaceAll method is used to replace all non-word characters with an empty string. Finally, the string is converted to lowercase.

The assert test uses another method added by Groovy to String, called reverse. Java has a reverse method in StringBuffer, but not String. Groovy adds the reverse method to String for convenience.

Groovy adds lots of methods to the Java standard libraries. Collectively these are known as the Groovy JDK and are one of the best features of Groovy. The Groovy documentation includes GroovyDocs for both the Groovy standard library and the Groovy JDK.

THE GROOVY JDK Through its metaprogramming capabilities, Groovy adds many convenient methods to the standard Java libraries. These additional methods are known as the Groovy JDK.

In summary, Groovy uses numbers and objects and has both regular and parameterized strings with additional methods. Another area where Groovy greatly simplifies Java is collections.

B.3 Plain Old Groovy Objects

Java classes with getters and setters for the attributes are often known as POJOs, or Plain Old Java Objects. In Groovy, the same classes are Plain Old Groovy Objects, or POGOs.7 POGOs have additional characteristics that are discussed in this section.

Consider the following Person class in Groovy:

7Python occasionally uses the term POPOs, which sounds vaguely disgusting. If you really want to annoy a Ruby developer, refer to POROs. Ruby people hate anything that sounds like Java.

www.it-ebooks.info

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]