Getting Groovy
Two Worlds Collide Java Perl
Two Worlds Collide Java Perl
Two Worlds Collide Java Perl
Two Worlds Collide Java Perl
JVM Languages Java JRuby Jython
JVM Languages Java JRuby Jython
JVM Languages Java JRuby Jython Groovy
What ’ s wrong with Java anyway?
Slurping files in Perl
Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>;
Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>; Slurping in Ruby
Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>; file = File.open(filename, &q...
Java?
Java? StringBuilder contents = new StringBuilder(); try { BufferedReader input = new BufferedReader(new FileReader(aFile))...
Java’s True Character
Java’s True Character
What would Groovy do?
What would Groovy do? def contents = new File(filename) …
What would Groovy do? def contents = new File(filename)  .text
XML Parsing <langs type=&quot;current&quot;>  <language>Perl</language>  <language>Java</language>  <language>Groovy</lang...
XML Parsing <langs type=&quot;current&quot;>  <language>Perl</language>  <language>Java</language>  <language>Groovy</lang...
XML Parsing <langs type=&quot;current&quot;>  <language>Perl</language>  <language>Java</language>  <language>Groovy</lang...
XML Parsing in Groovy <langs type=&quot;current&quot;>  <language>Perl</language>  <language>Java</language>  <language>Gr...
XML Parsing in Groovy <langs type=&quot;current&quot;>  <language>Perl</language>  <language>Java</language>  <language>Gr...
XML Parsing in Groovy <langs type=&quot;current&quot;>  <language>Perl</language>  <language>Java</language>  <language>Gr...
Closures (1..10).each {|i| print i, &quot;n&quot; } (1..10).each {i -> println i } Ruby Groovy
Configuration def config = new ConfigSlurper().parse(...) def config = new ConfigSlurper() .parse(new File(&quot;config.gr...
Runtime Environments foo.bar = &quot;baz&quot; something.else = 99 environments  {   development  {   port = 8181   db = &...
More short-hand def result = a ? a : b The Elvis operator def result = a ?: b
More short-hand new Date().format('yyyy-MM-dd') Date formatting
ExpandoMetaClass Number.metaClass.getProperty = { propertyName -> if (propertyName == 'percent') { delegate / 100 } else {...
Upcoming SlideShare
Loading in …5
×

Getting groovy (ODP)

419 views

Published on

Why Groovy is a nicer Java

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
419
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
3
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Perl and Java really don&apos;t get on
  • Like Leeds United, Perl used to be incredibly popular, faded a bit, suddenly looked promising about a decade ago, and fans have been waiting ever since then for a big comeback. In the meantime, some of the star players have moved on to better things.
  • So no JVM Perl yet.
  • Integration between Java and most other JVM languages is usually in only one direction. It&apos;s easy to use Java libraries in jython/jruby, but harder to use java/jython code elsewhere in Java.
  • Groovy generates Java classes, which can be packaged and used in Java alongside other jars.
  • Wait – that’s too verbose…
  • “ it ” is shorthand – a default parameter, like Perl’s $ _
  • “ it ” is shorthand – a default parameter, like Perl’s $ _
  • Getting groovy (ODP)

    1. 1. Getting Groovy
    2. 2. Two Worlds Collide Java Perl
    3. 3. Two Worlds Collide Java Perl
    4. 4. Two Worlds Collide Java Perl
    5. 5. Two Worlds Collide Java Perl
    6. 6. JVM Languages Java JRuby Jython
    7. 7. JVM Languages Java JRuby Jython
    8. 8. JVM Languages Java JRuby Jython Groovy
    9. 9. What ’ s wrong with Java anyway?
    10. 10. Slurping files in Perl
    11. 11. Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>;
    12. 12. Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>; Slurping in Ruby
    13. 13. Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>; file = File.open(filename, &quot;rb&quot;) contents = file.read Slurping in Ruby
    14. 14. Java?
    15. 15. Java? StringBuilder contents = new StringBuilder(); try { BufferedReader input = new BufferedReader(new FileReader(aFile)); try { String line = null; while ( (line = input.readLine()) != null) { contents.append(line); contents.append(lineSeparator); } } finally { input.close(); } } catch (IOException ex) { ex.printStackTrace(); } ... = contents.toString();
    16. 16. Java’s True Character
    17. 17. Java’s True Character
    18. 18. What would Groovy do?
    19. 19. What would Groovy do? def contents = new File(filename) …
    20. 20. What would Groovy do? def contents = new File(filename) .text
    21. 21. XML Parsing <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs>
    22. 22. XML Parsing <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs> import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; import java.io.IOException;
    23. 23. XML Parsing <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs> public class ParseXml { public static void main(String[] args) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(“langs.xml&quot;); Element langs = doc.getDocumentElement(); System.out.println(&quot;type = &quot; + langs.getAttribute(&quot;type&quot;)); NodeList list = langs.getElementsByTagName(&quot;language&quot;); for(int i=0; i < list.getLength(); i++) { Element language = (Element) list.item(i); System.out.println(language.getTextContent()); } } catch(ParserConfigurationException pce) { pce.printStackTrace(); } catch(SAXException se) { se.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } } } }
    24. 24. XML Parsing in Groovy <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs>
    25. 25. XML Parsing in Groovy <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs> def langs = new XmlSlurper().parse(&quot;langs.xml&quot;) println langs.@type langs.language.each { elem -> println elem }
    26. 26. XML Parsing in Groovy <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs> def langs = new XmlSlurper().parse(&quot;langs.xml&quot;) println langs.@type langs.language.each { println it }
    27. 27. Closures (1..10).each {|i| print i, &quot;n&quot; } (1..10).each {i -> println i } Ruby Groovy
    28. 28. Configuration def config = new ConfigSlurper().parse(...) def config = new ConfigSlurper() .parse(new File(&quot;config.groovy&quot;).toURL()) assert config.foo.port == 80 assert config.foo.bar.baz.some.thing == &quot;burp&quot; foo { port = 80 bar { baz { some.thing = &quot;burp&quot; } } }
    29. 29. Runtime Environments foo.bar = &quot;baz&quot; something.else = 99 environments { development { port = 8181 db = &quot;devdb&quot; } test { port = 8080 db = &quot;testdb&quot; } production { port = 80 db = &quot;proddb&quot; } } ConfigSlurper(&quot;production&quot;)
    30. 30. More short-hand def result = a ? a : b The Elvis operator def result = a ?: b
    31. 31. More short-hand new Date().format('yyyy-MM-dd') Date formatting
    32. 32. ExpandoMetaClass Number.metaClass.getProperty = { propertyName -> if (propertyName == 'percent') { delegate / 100 } else { delegate } } println 20.percent // 0.20 println 17.5.percent // 0.175

    ×