SlideShare a Scribd company logo
Groovy for Java developers
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.
Ok, what’s the
Groovy history?
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
Try some Groovy scripts on
http://www.groovyconsole.appspot.com/
Integer x = 10
def y = 10
println x.class
println y.class
Typing
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
Collections are friends of mine
def x = [:]
x.putAt('name', 'Daniel')
println x
Magic methods
def x = ['Daniel', 'Juliana', 'Alice', 'Davi']
x.each {
println it
}
Magic methods
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
}
}
println familyPositions
def student = false
assert !student
Wonderful operators
def student = [
[name : 'Daniel',age : 26],
[name : 'Juliana',age : 26]
]
println student*.name
assert student*.age == [26,26]
Wonderful operators
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
class Student {
String name
int mark
def isApproved() {
mark >= 70 ?: false
}
}
Student daniel = new Student(name: 'Daniel', mark: 100)
daniel.isApproved()
Wonderful operators
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
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
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
JSON for the clever guys
import groovy.json.JsonSlurper
def persons = new JsonSlurper().parseText('[{"name":
"Daniel"},{"name": "Juliana"}]')
println persons*.name
Anyone.hasQuestions()
Thank.to(“you”).to(“listen me”)
def contact = Person.find { it.name == ‘Daniel Fernandes’ }.contact
assert contact.mail == ‘danielpsf@gmail.com’
assert contact.skype == ‘daniel.pedro_fernandes’

More Related Content

Similar to The best things of groovy

Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
Dr. Raaid Alubady
 
Hello java
Hello java   Hello java
Hello java
Hello java  Hello java
JavaFX introduction
JavaFX introductionJavaFX introduction
JavaFX introduction
José Maria Silveira Neto
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
Kotlin: maybe it's the right time
Kotlin: maybe it's the right timeKotlin: maybe it's the right time
Kotlin: maybe it's the right time
Davide Cerbo
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
Mike Subelsky
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Codemotion
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
Miles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
Brian Hogan
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
pmanvi
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSL
jodosha
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
Groovy!
Groovy!Groovy!
Groovy!
Petr Giecek
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Andrea Zaza
 
Codeweek 2015 - Reactive Web Applications with Scala and LIFT framework
Codeweek 2015 - Reactive Web Applications with Scala and LIFT frameworkCodeweek 2015 - Reactive Web Applications with Scala and LIFT framework
Codeweek 2015 - Reactive Web Applications with Scala and LIFT framework
Riccardo Sirigu
 
Rails 2010 Workshop
Rails 2010 WorkshopRails 2010 Workshop
Rails 2010 Workshop
dtsadok
 
JavaFX Overview
JavaFX OverviewJavaFX Overview
JavaFX Overview
José Maria Silveira Neto
 

Similar to The best things of groovy (20)

Unit 1
Unit 1Unit 1
Unit 1
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
 
Hello java
Hello java   Hello java
Hello java
 
Hello java
Hello java  Hello java
Hello java
 
JavaFX introduction
JavaFX introductionJavaFX introduction
JavaFX introduction
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
Kotlin: maybe it's the right time
Kotlin: maybe it's the right timeKotlin: maybe it's the right time
Kotlin: maybe it's the right time
 
Ruby For Startups
Ruby For StartupsRuby For Startups
Ruby For Startups
 
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017 Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
Davide Cerbo - Kotlin: forse è la volta buona - Codemotion Milan 2017
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7Intro to Ruby - Twin Cities Code Camp 7
Intro to Ruby - Twin Cities Code Camp 7
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologist
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSL
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
Groovy!
Groovy!Groovy!
Groovy!
 
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
Reactive Web Applications with Scala & Liftweb - CodeWeek 2015
 
Codeweek 2015 - Reactive Web Applications with Scala and LIFT framework
Codeweek 2015 - Reactive Web Applications with Scala and LIFT frameworkCodeweek 2015 - Reactive Web Applications with Scala and LIFT framework
Codeweek 2015 - Reactive Web Applications with Scala and LIFT framework
 
Rails 2010 Workshop
Rails 2010 WorkshopRails 2010 Workshop
Rails 2010 Workshop
 
JavaFX Overview
JavaFX OverviewJavaFX Overview
JavaFX Overview
 

More from Daniel Fernandes

Openstack 101
Openstack 101Openstack 101
Openstack 101
Daniel Fernandes
 
Facebook dev circle - cloud 101
Facebook dev circle - cloud 101Facebook dev circle - cloud 101
Facebook dev circle - cloud 101
Daniel Fernandes
 
The frontend and the automated tests
The frontend and the automated testsThe frontend and the automated tests
The frontend and the automated tests
Daniel Fernandes
 
Continuous integration (light talk)
Continuous integration   (light talk)Continuous integration   (light talk)
Continuous integration (light talk)
Daniel Fernandes
 
Jenkins além da integração contínua - práticas de devops
Jenkins além da integração contínua - práticas de devopsJenkins além da integração contínua - práticas de devops
Jenkins além da integração contínua - práticas de devops
Daniel Fernandes
 
Prototipação
PrototipaçãoPrototipação
Prototipação
Daniel Fernandes
 

More from Daniel Fernandes (6)

Openstack 101
Openstack 101Openstack 101
Openstack 101
 
Facebook dev circle - cloud 101
Facebook dev circle - cloud 101Facebook dev circle - cloud 101
Facebook dev circle - cloud 101
 
The frontend and the automated tests
The frontend and the automated testsThe frontend and the automated tests
The frontend and the automated tests
 
Continuous integration (light talk)
Continuous integration   (light talk)Continuous integration   (light talk)
Continuous integration (light talk)
 
Jenkins além da integração contínua - práticas de devops
Jenkins além da integração contínua - práticas de devopsJenkins além da integração contínua - práticas de devops
Jenkins além da integração contínua - práticas de devops
 
Prototipação
PrototipaçãoPrototipação
Prototipação
 

Recently uploaded

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
BrazilAccount1
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
AhmedHussein950959
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
FluxPrime1
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 

Recently uploaded (20)

Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
English lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdfEnglish lab ppt no titlespecENG PPTt.pdf
English lab ppt no titlespecENG PPTt.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang,  ICLR 2024, MLILAB, KAIST AI.pdfJ.Yang,  ICLR 2024, MLILAB, KAIST AI.pdf
J.Yang, ICLR 2024, MLILAB, KAIST AI.pdf
 
DESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docxDESIGN A COTTON SEED SEPARATION MACHINE.docx
DESIGN A COTTON SEED SEPARATION MACHINE.docx
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 

The best things of groovy

  • 1. Groovy for Java developers
  • 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
  • 8. Collections are friends of mine def x = [:] x.putAt('name', 'Daniel') println x
  • 9. Magic methods def x = ['Daniel', 'Juliana', 'Alice', 'Davi'] x.each { println it }
  • 10. Magic methods 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 } } println familyPositions
  • 11. def student = false assert !student Wonderful operators
  • 12. def student = [ [name : 'Daniel',age : 26], [name : 'Juliana',age : 26] ] println student*.name assert student*.age == [26,26] Wonderful operators
  • 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
  • 20. Thank.to(“you”).to(“listen me”) def contact = Person.find { it.name == ‘Daniel Fernandes’ }.contact assert contact.mail == ‘danielpsf@gmail.com’ assert contact.skype == ‘daniel.pedro_fernandes’

Editor's Notes

  1. 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.
  2. Don’t worry! You do not need to know everything about Groovy solutions because you’ll live Groovy after right now.
  3. or def x = [10, 5, 3, 0.5, 11.3] println x.sort()
  4. or def x = [:] x['name'] = 'Daniel' x['age'] = 26 println x ​
  5. or def x = [:] x['name'] = 'Daniel' x['age'] = 26 println x ​
  6. 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
  7. Verifications
  8. Arrays
  9. 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 juliana​DataBind​.is(juliana) //assert julianaDataBind.name.is(julianaDataBind.name) //assert juliana != julianaDataBind //assert juliana.name == julianaDataBind.name
  10. Elvis operator
  11. Safe navigator
  12. in == contains
  13. 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
  14. 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"