- Groovy is an agile and dynamic language for the Java Virtual Machine that builds on Java's strengths with additional features from languages like Python, Ruby and Smalltalk. It provides features to Java developers with almost no learning curve.
- Groovy supports dynamic and static typing, collections, magic methods, operators that simplify operations like finding and filtering, and processing XML and JSON easily.
- Examples demonstrate how Groovy can be used to process collections, define classes and methods, parse XML and JSON, and more.
2. Agenda:
- A short history;
- Typing;
- Collections are friends of mine;
- Magic methods;
- Wonderful operators;
- XML isn’t a PITA anymore;
- JSON for the clever guys.
4. Groovy...
is an agile and dynamic language for the Java Virtual Machine
builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk
makes modern programming features available to Java developers with almost-zero learning curve
provides the ability to statically type check and statically compile your code for robustness and performance
supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain
makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL
increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications
simplifies testing by supporting unit testing and mocking out-of-the-box
seamlessly integrates with all existing Java classes and libraries
compiles straight to Java bytecode so you can use it anywhere you can use Java
5. Try some Groovy scripts on
http://www.groovyconsole.appspot.com/
6. Integer x = 10
def y = 10
println x.class
println y.class
Typing
7. def x = []
x.add 10
x.add 5
x.add 3
x.add 0.5
x.add 11.3
println x.sort()
Collections are friends of mine
13. class Student {
String name
int mark
}
Student daniel = new Student(name: 'Daniel', mark: 100)
assert daniel as Student
println daniel.mark
println daniel.name
daniel.setName('Daniel Fernandes')
println daniel.name
Wonderful operators
14. class Student {
String name
int mark
def isApproved() {
mark >= 70 ?: false
}
}
Student daniel = new Student(name: 'Daniel', mark: 100)
daniel.isApproved()
Wonderful operators
15. class Student {
String name
int mark
def isApproved() {
mark >= 70 ?: false
}
}
List<Student> students = [
new Student(name : 'Daniel Fernandes', mark : 97),
new Student(name : 'Juliana Fernandes', mark : 100)
]
//Student daniel = students.find{ it.name == 'Daniel' }
def daniel = students.find{ it.name == 'Daniel' }
println daniel?.mark
def danielFernandes = students.find{ it.name == 'Daniel Fernandes' }
println danielFernandes?.mark
Wonderful operators
16. class Student {
String name
int mark
}
List<Student> students = [
new Student(name : 'Daniel Fernandes'),
new Student(name : 'Juliana Fernandes',
mark : 100)
]
assert 'Daniel Fernandes' in students.name
assert 'João Batista' in students.name
Wonderful operators
17. def brasil = new XmlParser().parseText(new
File('/home/daniel/Dropbox/Public/brasil.xml').text)
def estados = brasil.estados.estado
assert estados.size() == 27
def cidades = estados*.cidades*.cidade
assert cidades*.size().sum() == 5564
XML isn’t a PITA anymore
18. JSON for the clever guys
import groovy.json.JsonSlurper
def persons = new JsonSlurper().parseText('[{"name":
"Daniel"},{"name": "Juliana"}]')
println persons*.name
James Strachan and his wife were waiting for a late plane.
While she went shopping, he visited an Internet café and spontaneously decided to go to the Python web site and study the language. In the course of this activity, he became more and more intrigued. Being a seasoned Java programmer, he recognized that his home language lacked many of the interesting and useful features Python
had invented, such as native language support for common datatypes in an expressive syntax and, more important, dynamic behavior. The idea was born to bring such features to Java.
Don’t worry! You do not need to know everything about Groovy solutions because you’ll live Groovy after right now.
or
def x = [10, 5, 3, 0.5, 11.3]
println x.sort()
or
def x = [:]
x['name'] = 'Daniel'
x['age'] = 26
println x
or
def x = [:]
x['name'] = 'Daniel'
x['age'] = 26
println x
def members = ['Daniel', 'Juliana', 'Alice', 'Davi']
def familyPositions = [:]
members.each { member ->
switch(member) {
case 'Alice':
case 'Davi':
familyPositions[member] = 'child'
break
case 'Daniel':
case 'Juliana':
familyPositions[member] = 'parents'
break
}
}
int kidsCount = 0
familyPositions.findAll { member ->
//println member.class
if(member.value == 'child'){
kidsCount++
}
}
println kidsCount
Verifications
Arrays
Classes
Get the opportunity to talk about the data bind and “is”
class Student {
String name
int mark
}
def thinkThatIsAFormSubmition = [name : 'Juliana Fernandes', mark: 100]
Student juliana = new Student()
juliana.setName('Juliana Fernandes')
juliana.setMark(100)
Student julianaDataBind = new Student(thinkThatIsAFormSubmition)
//assert julianaDataBind.is(juliana)
//assert julianaDataBind.name.is(julianaDataBind.name)
//assert juliana != julianaDataBind
//assert juliana.name == julianaDataBind.name
Elvis operator
Safe navigator
in == contains
XML com estados e cidades do Brasil ⇒ https://www.dropbox.com/s/8mrvlsya7ur94kd/brasil.xml
SQL com os estados e cidades do Brasil usados para geração deste XML ⇒ http://samus.com.br/web/site/artigo-todas_as_cidades_do_brasil_atualizado_e_com_acentos
think out the box
import groovy.json.JsonSlurper
class Person {
String name
int age
}
def json = new JsonSlurper().parseText('{"name": "Daniel", "age":26}')
Person daniel = new Person(json)
println "My name is $daniel.name and I'm $daniel.age years old"