the best things of groovy

20
Groovy for Java developers

Upload: daniel-fernandes

Post on 21-Apr-2017

13 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: The best things of groovy

Groovy for Java developers

Page 2: The best things of groovy

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

Ok, what’s the

Groovy history?

Page 4: The best things of groovy

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

Try some Groovy scripts onhttp://www.groovyconsole.appspot.com/

Page 6: The best things of groovy

Integerx=10defy=10

printlnx.classprintlny.class

Typing

Page 7: The best things of groovy

defx=[]

x.add10x.add5x.add3x.add0.5x.add11.3

printlnx.sort()

Collections are friends of mine

Page 8: The best things of groovy

Collections are friends of mine

defx=[:]

x.putAt('name','Daniel')

printlnx

Page 9: The best things of groovy

Magic methods

defx=['Daniel','Juliana','Alice','Davi']

x.each{printlnit}

Page 10: The best things of groovy

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

defstudent=false

assert!student

Wonderful operators

Page 12: The best things of groovy

defstudent=[[name:'Daniel',age:26],[name:'Juliana',age:26]]

printlnstudent*.nameassertstudent*.age==[26,26]

Wonderful operators

Page 13: The best things of groovy

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

classStudent{Stringnameintmark

defisApproved(){mark>=70?:false}}Studentdaniel=newStudent(name:'Daniel',mark:100)daniel.isApproved()

Wonderful operators

Page 15: The best things of groovy

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

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

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

JSON for the clever guysimportgroovy.json.JsonSlurper

defpersons=newJsonSlurper().parseText('[{"name":"Daniel"},{"name":"Juliana"}]')

printlnpersons*.name

Page 19: The best things of groovy

Anyone.hasQuestions()

Page 20: The best things of groovy

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’