The best things of groovy
-
Upload
daniel-fernandes -
Category
Engineering
-
view
12 -
download
0
Transcript of The best things of groovy
![Page 1: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/1.jpg)
Groovy for Java developers
![Page 2: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/2.jpg)
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.
![Page 3: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/3.jpg)
Ok, what’s the
Groovy history?
![Page 4: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/4.jpg)
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
![Page 5: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/5.jpg)
Try some Groovy scripts onhttp://www.groovyconsole.appspot.com/
![Page 6: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/6.jpg)
Integerx=10defy=10
printlnx.classprintlny.class
Typing
![Page 7: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/7.jpg)
defx=[]
x.add10x.add5x.add3x.add0.5x.add11.3
printlnx.sort()
Collections are friends of mine
![Page 8: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/8.jpg)
Collections are friends of mine
defx=[:]
x.putAt('name','Daniel')
printlnx
![Page 9: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/9.jpg)
Magic methods
defx=['Daniel','Juliana','Alice','Davi']
x.each{printlnit}
![Page 10: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/10.jpg)
Magic methodsdefmembers=['Daniel','Juliana','Alice','Davi']
deffamilyPositions=[:]
members.each{member->switch(member){case'Alice':case'Davi':familyPositions[member]='child'break
case'Daniel':case'Juliana':familyPositions[member]='parents'break}}
printlnfamilyPositions
![Page 11: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/11.jpg)
defstudent=false
assert!student
Wonderful operators
![Page 12: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/12.jpg)
defstudent=[[name:'Daniel',age:26],[name:'Juliana',age:26]]
printlnstudent*.nameassertstudent*.age==[26,26]
Wonderful operators
![Page 13: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/13.jpg)
classStudent{Stringnameintmark}Studentdaniel=newStudent(name:'Daniel',mark:100)assertdanielasStudent
printlndaniel.markprintlndaniel.name
daniel.setName('DanielFernandes')
printlndaniel.name
Wonderful operators
![Page 14: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/14.jpg)
classStudent{Stringnameintmark
defisApproved(){mark>=70?:false}}Studentdaniel=newStudent(name:'Daniel',mark:100)daniel.isApproved()
Wonderful operators
![Page 15: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/15.jpg)
classStudent{Stringnameintmark
defisApproved(){mark>=70?:false}}
List<Student>students=[newStudent(name:'DanielFernandes',mark:97),newStudent(name:'JulianaFernandes',mark:100)]
//Studentdaniel=students.find{it.name=='Daniel'}defdaniel=students.find{it.name=='Daniel'}printlndaniel?.mark
defdanielFernandes=students.find{it.name=='DanielFernandes'}printlndanielFernandes?.mark
Wonderful operators
![Page 16: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/16.jpg)
classStudent{Stringnameintmark}
List<Student>students=[newStudent(name:'DanielFernandes'),newStudent(name:'JulianaFernandes',mark:100)]
assert'DanielFernandes'instudents.nameassert'JoãoBatista'instudents.name
Wonderful operators
![Page 17: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/17.jpg)
defbrasil=newXmlParser().parseText(newFile('/home/daniel/Dropbox/Public/brasil.xml').text)
defestados=brasil.estados.estadoassertestados.size()==27
defcidades=estados*.cidades*.cidadeassertcidades*.size().sum()==5564
XML isn’t a PITA anymore
![Page 18: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/18.jpg)
JSON for the clever guysimportgroovy.json.JsonSlurper
defpersons=newJsonSlurper().parseText('[{"name":"Daniel"},{"name":"Juliana"}]')
printlnpersons*.name
![Page 19: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/19.jpg)
Anyone.hasQuestions()
![Page 20: The best things of groovy](https://reader035.fdocuments.us/reader035/viewer/2022070521/58f9ad331a28ab416c8b45b7/html5/thumbnails/20.jpg)
Thank.to(“you”).to(“listen me”)
def contact = Person.find { it.name == ‘Daniel Fernandes’ }.contact
assert contact.mail == ‘[email protected]’assert contact.skype == ‘daniel.pedro_fernandes’