SlideShare a Scribd company logo
1 of 20
Download to read offline
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 Groovy for Java devs: history, typing, collections & more

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 timeDavide Cerbo
 
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 DevelopersMiles 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 7Brian Hogan
 
Scala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistScala and jvm_languages_praveen_technologist
Scala and jvm_languages_praveen_technologistpmanvi
 
JRuby e DSL
JRuby e DSLJRuby e DSL
JRuby e DSLjodosha
 
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
 
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 2015Andrea 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 frameworkRiccardo Sirigu
 
Rails 2010 Workshop
Rails 2010 WorkshopRails 2010 Workshop
Rails 2010 Workshopdtsadok
 

Similar to Groovy for Java devs: history, typing, collections & more (20)

Unit 1
Unit 1Unit 1
Unit 1
 
Hello java
Hello java   Hello java
Hello java
 
Hello Java-First Level
Hello Java-First LevelHello Java-First Level
Hello Java-First Level
 
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

Facebook dev circle - cloud 101
Facebook dev circle - cloud 101Facebook dev circle - cloud 101
Facebook dev circle - cloud 101Daniel Fernandes
 
The frontend and the automated tests
The frontend and the automated testsThe frontend and the automated tests
The frontend and the automated testsDaniel 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 devopsDaniel 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

Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Romil Mishra
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdfAkritiPradhan2
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfalene1
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Communityprachaibot
 
Theory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdfTheory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdfShreyas Pandit
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfDrew Moseley
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书rnrncn29
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsResearcher Researcher
 
STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectSTATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectGayathriM270621
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfBalamuruganV28
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating SystemRashmi Bhat
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxStephen Sitton
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfManish Kumar
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHSneha Padhiar
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodManicka Mamallan Andavar
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
Secure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech LabsSecure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech Labsamber724300
 

Recently uploaded (20)

Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________Gravity concentration_MI20612MI_________
Gravity concentration_MI20612MI_________
 
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdfDEVICE DRIVERS AND INTERRUPTS  SERVICE MECHANISM.pdf
DEVICE DRIVERS AND INTERRUPTS SERVICE MECHANISM.pdf
 
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdfComprehensive energy systems.pdf Comprehensive energy systems.pdf
Comprehensive energy systems.pdf Comprehensive energy systems.pdf
 
Prach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism CommunityPrach: A Feature-Rich Platform Empowering the Autism Community
Prach: A Feature-Rich Platform Empowering the Autism Community
 
Theory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdfTheory of Machine Notes / Lecture Material .pdf
Theory of Machine Notes / Lecture Material .pdf
 
Immutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdfImmutable Image-Based Operating Systems - EW2024.pdf
Immutable Image-Based Operating Systems - EW2024.pdf
 
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
『澳洲文凭』买麦考瑞大学毕业证书成绩单办理澳洲Macquarie文凭学位证书
 
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
Stork Webinar | APM Transformational planning, Tool Selection & Performance T...
 
Novel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending ActuatorsNovel 3D-Printed Soft Linear and Bending Actuators
Novel 3D-Printed Soft Linear and Bending Actuators
 
STATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subjectSTATE TRANSITION DIAGRAM in psoc subject
STATE TRANSITION DIAGRAM in psoc subject
 
CS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdfCS 3251 Programming in c all unit notes pdf
CS 3251 Programming in c all unit notes pdf
 
Input Output Management in Operating System
Input Output Management in Operating SystemInput Output Management in Operating System
Input Output Management in Operating System
 
Turn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptxTurn leadership mistakes into a better future.pptx
Turn leadership mistakes into a better future.pptx
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdfModule-1-(Building Acoustics) Noise Control (Unit-3). pdf
Module-1-(Building Acoustics) Noise Control (Unit-3). pdf
 
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACHTEST CASE GENERATION GENERATION BLOCK BOX APPROACH
TEST CASE GENERATION GENERATION BLOCK BOX APPROACH
 
Levelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument methodLevelling - Rise and fall - Height of instrument method
Levelling - Rise and fall - Height of instrument method
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
Secure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech LabsSecure Key Crypto - Tech Paper JET Tech Labs
Secure Key Crypto - Tech Paper JET Tech Labs
 

Groovy for Java devs: history, typing, collections & more

  • 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"