Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Java to Groovy, the easy way

Eyal LEZMY
December 22, 2014

From Java to Groovy, the easy way

This short presentation introduces to the Groovy language, coming from Java.

Eyal LEZMY

December 22, 2014
Tweet

More Decks by Eyal LEZMY

Other Decks in Programming

Transcript

  1. public class HelloWorld { private String name; public void setName(String

    name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } From JAVA
  2. public class HelloWorld { private String name; public void setName(String

    name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } To GROOVY
  3. public class HelloWorld { private String name; public void setName(String

    name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } To GROOVY
  4. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello " + name } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  5. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello " + name } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  6. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello ${name}" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  7. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello ${name}" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY GString => double quotes (“ ”)
  8. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  9. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  10. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY Private field
  11. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY Getter & Setter Private field
  12. class HelloWorld { private String name void setName(String name) {

    this.name = name } String getName() { return name } String greet() { return "Hello $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  13. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  14. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  15. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.setName("Groovy") System.out.println( helloWorld.greet() ) } } To GROOVY
  16. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" System.out.println( helloWorld.greet() ) } } To GROOVY
  17. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" System.out.println( helloWorld.greet() ) } } To GROOVY
  18. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" System.out.println( helloWorld.greet() ) } } To GROOVY
  19. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println( helloWorld.greet() ) } } To GROOVY
  20. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println( helloWorld.greet() ) } } To GROOVY
  21. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
  22. class HelloWorld { String name String greet() { return "Hello

    $name" } static void main(String[] args) { HelloWorld helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
  23. class HelloWorld { def name def greet() { return "Hello

    $name" } static main(args) { def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
  24. class HelloWorld { def name def greet() { return "Hello

    $name" } static main(args) { def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
  25. class HelloWorld { def name def greet() { return "Hello

    $name" } static main(args) { def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() } } To GROOVY
  26. class HelloWorld { def name def greet() { return "Hello

    $name" } } def helloWorld = new HelloWorld() helloWorld.name = "Groovy" println helloWorld.greet() To GROOVY
  27. public class HelloWorld { private String name; public void setName(String

    name) { this.name = name; } public String getName() { return name; } public String greet() { return "Hello " + name; } public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); helloWorld.setName("Groovy"); System.out.println( helloWorld.greet() ); } } From JAVA
  28. def createGenyboy(gender = “male”) { … if(gender == “male”) name

    = “Genyboy” … [name, color, size] } def(String name, Color color, int size) = createGenyboy(gender:“female”) Groovy Methods
  29. static def cmd(def command, Closure c){ Process p = command.execute()

    p.text.eachLine {line, count -> c(line, count) } p.exitValue() } a = “hello” cmd(“gmtool admin list”) {line, count -> print a + line } Groovy Closures
  30. def array = [] array = [“one”, “two”, “three”] tab.findAll(){item

    -> item.size == 3 print it } [1, 2, 3].each(){ print it } def map = [:] map = [one:1, two:2, three:3] assert map.one == map[“one”] def range = 2..5 assert range == [2, 3, 4, 5] Groovy Collections
  31. static noNull(Closure c){ String nullLabel = null.toString() NullObject.metaClass.toString = {return

    ''} c() //set as defaut NullObject.metaClass.toString = {return nullLabel} } cmd([“gmtool”, “admin”, “create” “template”, “device”, “--density=”+density]) >> gmtool admin create template device --density=null noNull{ cmd([“gmtool”, “admin”, “create” “template”, “device”, “--density=”+density]) } >> gmtool admin create template device --density= Groovy Metaclass
  32. And others Native support for Regex ~/myRegex/ Powerful switch statement

    switch awesomness { case 1..10: “yeah!” } Operator Overloading 1 + 1 = 1
  33. Conclusion You keep focus on business logic Like magic So

    dynamic, you have to test ! JUnit & Co. So dynamic, so easy to mock JSON-likery
  34. References From Java to Groovy http://groovy.dzone.com/news/java-groovy-few-easy-steps By Guillaume Laforge Differences

    from Java http://groovy.codehaus.org/Differences+from+Java Official documentation http://groovy.codehaus.org/