Getting groovy (ODP)
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Getting groovy (ODP)

on

  • 462 views

Why Groovy is a nicer Java

Why Groovy is a nicer Java

Statistics

Views

Total Views
462
Views on SlideShare
462
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Perl and Java really don'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'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) Presentation Transcript

  • 1. Getting Groovy
  • 2. Two Worlds Collide Java Perl
  • 3. Two Worlds Collide Java Perl
  • 4. Two Worlds Collide Java Perl
  • 5. Two Worlds Collide Java Perl
  • 6. JVM Languages Java JRuby Jython
  • 7. JVM Languages Java JRuby Jython
  • 8. JVM Languages Java JRuby Jython Groovy
  • 9. What ’ s wrong with Java anyway?
  • 10. Slurping files in Perl
  • 11. Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>;
  • 12. Slurping files in Perl my $fh = open '<', $filename; local $/ = undef; my $contents = <$fh>; Slurping in Ruby
  • 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. Java?
  • 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. Java’s True Character
  • 17. Java’s True Character
  • 18. What would Groovy do?
  • 19. What would Groovy do? def contents = new File(filename) …
  • 20. What would Groovy do? def contents = new File(filename) .text
  • 21. XML Parsing <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs>
  • 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. 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. XML Parsing in Groovy <langs type=&quot;current&quot;> <language>Perl</language> <language>Java</language> <language>Groovy</language> </langs>
  • 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. 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. Closures (1..10).each {|i| print i, &quot;n&quot; } (1..10).each {i -> println i } Ruby Groovy
  • 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. 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. More short-hand def result = a ? a : b The Elvis operator def result = a ?: b
  • 31. More short-hand new Date().format('yyyy-MM-dd') Date formatting
  • 32. ExpandoMetaClass Number.metaClass.getProperty = { propertyName -> if (propertyName == 'percent') { delegate / 100 } else { delegate } } println 20.percent // 0.20 println 17.5.percent // 0.175