Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Stop Writing JavaStart WritingGroovyEvgenyGoldin
Java => Groovy
Start Writing Groovy
equals vs. ==
Groovy runs Java .. except where it doesn’t.equals() / ==== / is()Works with nullsassert null == nullassertnull.is( null )assertnull.equals( null )
Cleanups
Lose publicLose ; and returnLose .classLose getX() / setX()
Lose publicLose ; and returnLose .classLose getX() / setX()public String className( Class c ) { return c.getName(); }o.setName( className( Map.class ));
Lose publicLose ; and returnLose .classLose getX() / setX()def className( Class c ) { c.name }o.name = className( Map )
Lose publicLose ; and returnLose .classLose getX() / setX()http://codenarc.sourceforge.net/http://plugins.intellij.net/plugin/?idea&id=5925
Lose publicLose ; and returnLose .classLose getX() / setX()def className( Class c ) { c.name }o.name = className( Map )It is a big deal at the end of the day
def j = 4
def j = 4def list = []def list = [1, 2, 3, 4]
def j = 4def list = []def list = [1, 2, 3, 4]def map = [:]def map = [1:2, 3:4]
def j = 4def list = []def list = [1, 2, 3, 4]def map = [:]def map = [1:2, 3:4]def array = [1, 2, 3, 4] as int[]
def j = 4def list = []def list = [1, 2, 3, 4]def map = [:]def map = [1:2, 3:4]def array = [1, 2, 3, 4] as int[]new Thread({ print j } as Runnable).start()
Safe navigation
GroovyTruth
if (( o != null ) && ( o.size() > 0 )) { .. }
if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }
if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }Safe navigation operator : object?.method()
if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }Safe navigation operator : object?.method()Groovy Truth:null is falseEmpty String, Map or Collection is falseZero number is falseif ( list ), if ( string ), if ( map ), if ( o?.size()) ..
But
assert “false”
assert “false”assert “ “
assert “false”assert “ “Object.asBoolean()
assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava      : Boolean.valueOf( String.valueOf( o ))
assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava      : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in Groovy
assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava      : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in GroovyAlways specify if you use Java or Groovy Truth
assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava      : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in GroovyAlways specify if you use Java or Groovy Truth
Elvis Operator
int j = ( o.size() > 0 ) ? o.size() : -1;
int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )
int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?:defaultValueTakes defaultValue if value evaluates to false
int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?:defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Strings
int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?: defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Stringsint j = ( size != null ) ? size : -1int j = size ?: -1
int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?: defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Stringsint j = ( size != null ) ? size : -1 // Accepts zero sizeint j = size ?: -1 // Doesn’t accept zero size
Default parameters
public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }
public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }Overload
public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }
public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }def foo ( int j = 1, intk ) { .. }
public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }def foo ( int j = 1, intk ) { .. }def foo ( intj, int k = 1 ) { .. }
public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }def foo ( int j = 1, intk ) { .. }def foo ( intj, int k = 1 ) { .. }def foo ( int j  = f1(), intk = f2()) { .. }
GroovyBeans
public class Bean () {    private int j;    public intgetJ(){ return this.j; }    public void setJ( int j ){ this.j = j; }}
class Bean {int j}def b = new Bean()println ( b.j ) / println ( b.getJ())b.j = 33 / b.setJ( 33 )N Groovy beans can be kept in the same file
GStrings
def s = ‘aaaaaaa’
def s = ‘aaaaaaa’def s = ’’’aaaaaaaaabbbbbbbb’’’
def s = ‘aaaaaaa’def s = ’’’aaaaaaaaabbbbbbbb’’’def s = “aaaaaaa”def s = ”””aaaaaaaaabbbbbbbb”””
def s = “aaaaaaa${b.j}”def s = ”””aaaa${ o.something() + b.j }aaaaabbbbbbbb”””
def s = “aaaaaaa${b.j}”def s = ”””aaaa${ o.something() + b.j }aaaaabbbbbbbb”””log.info ( String.format( “ .. %s .. ”, val ))log.info ( “ .. ${val} .. ” )
def s = “aaaaaaa${b.j}”def s = ”””aaaa${ o.something() + b.j }aaaaabbbbbbbb”””assert "aaaaa".class == Stringassert "${1+2}".class == org.codehaus.groovy.runtime.GStringImpl
assert
if ( o == null ) {    throw new RuntimeException( “msg” ) }
if ( o == null ) {    throw new RuntimeException( “msg” ) }assert o, “msg”
if ( o == null ) {    throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”
if ( o == null ) {    throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”
if ( o == null ) {    throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”assert false, “Fatal error”
if ( o == null ) {    throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”assert false, “Fatal error”Asserting code samples is a common practice
def j = [1, 2] def k = [3, 4] assert j[0] == k[0]
def j = [1, 2] def k = [3, 4] assert j[0] == k[0] Assertion failed: assert j[0] == k[0]       ||   |  ||       |1   |  |3       |    |  [3, 4]       |    false       [1, 2]
GDK
http://groovy.codehaus.org/groovy-jdk/Java+++
http://groovy.codehaus.org/groovy-jdk/Java+++ObjectStringFileCollection, Map, List, SetInputStream, OutputStreamReader, Writer
Object/Collection/Mapeach()any(), every()find(), findAll(), grep()join()collect()min(), max(), sum()inject()
StringtoURL()execute()eachLine()padLeft(), padRight()tr()
FiledeleteDir()eachLine()eachFileRecurse()getText()write(String)traverse()
Q&A

More Related Content

Start Writing Groovy

  • 1. Stop Writing JavaStart WritingGroovyEvgenyGoldin
  • 5. Groovy runs Java .. except where it doesn’t.equals() / ==== / is()Works with nullsassert null == nullassertnull.is( null )assertnull.equals( null )
  • 7. Lose publicLose ; and returnLose .classLose getX() / setX()
  • 8. Lose publicLose ; and returnLose .classLose getX() / setX()public String className( Class c ) { return c.getName(); }o.setName( className( Map.class ));
  • 9. Lose publicLose ; and returnLose .classLose getX() / setX()def className( Class c ) { c.name }o.name = className( Map )
  • 10. Lose publicLose ; and returnLose .classLose getX() / setX()http://codenarc.sourceforge.net/http://plugins.intellij.net/plugin/?idea&id=5925
  • 11. Lose publicLose ; and returnLose .classLose getX() / setX()def className( Class c ) { c.name }o.name = className( Map )It is a big deal at the end of the day
  • 13. def j = 4def list = []def list = [1, 2, 3, 4]
  • 14. def j = 4def list = []def list = [1, 2, 3, 4]def map = [:]def map = [1:2, 3:4]
  • 15. def j = 4def list = []def list = [1, 2, 3, 4]def map = [:]def map = [1:2, 3:4]def array = [1, 2, 3, 4] as int[]
  • 16. def j = 4def list = []def list = [1, 2, 3, 4]def map = [:]def map = [1:2, 3:4]def array = [1, 2, 3, 4] as int[]new Thread({ print j } as Runnable).start()
  • 19. if (( o != null ) && ( o.size() > 0 )) { .. }
  • 20. if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }
  • 21. if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }Safe navigation operator : object?.method()
  • 22. if (( o != null ) && ( o.size() > 0 )) { .. }if ( o?.size()) { .. }Safe navigation operator : object?.method()Groovy Truth:null is falseEmpty String, Map or Collection is falseZero number is falseif ( list ), if ( string ), if ( map ), if ( o?.size()) ..
  • 23. But
  • 26. assert “false”assert “ “Object.asBoolean()
  • 27. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))
  • 28. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in Groovy
  • 29. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in GroovyAlways specify if you use Java or Groovy Truth
  • 30. assert “false”assert “ “Object.asBoolean()Object => Boolean?Groovy : o asbooleanJava : Boolean.valueOf( String.valueOf( o ))“false”, “null”: false in Java, true in GroovyAlways specify if you use Java or Groovy Truth
  • 32. int j = ( o.size() > 0 ) ? o.size() : -1;
  • 33. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )
  • 34. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?:defaultValueTakes defaultValue if value evaluates to false
  • 35. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?:defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Strings
  • 36. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?: defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Stringsint j = ( size != null ) ? size : -1int j = size ?: -1
  • 37. int j = ( o.size() > 0 ) ? o.size() : -1def j = ( o.size() ?: -1 )Elvis operator: def j = value ?: defaultValueTakes defaultValue if value evaluates to falseBe careful with zero values and empty Stringsint j = ( size != null ) ? size : -1 // Accepts zero sizeint j = size ?: -1 // Doesn’t accept zero size
  • 39. public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }
  • 40. public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }Overload
  • 41. public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }
  • 42. public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }def foo ( int j = 1, intk ) { .. }
  • 43. public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }def foo ( int j = 1, intk ) { .. }def foo ( intj, int k = 1 ) { .. }
  • 44. public String foo( int j, int k ){ …}public String foo( int j ){ foo ( j, 1 ); }public String foo(){ foo ( 1, 1 ); }def foo ( int j = 1, int k = 1 ) { .. }def foo ( int j = 1, intk ) { .. }def foo ( intj, int k = 1 ) { .. }def foo ( int j = f1(), intk = f2()) { .. }
  • 46. public class Bean () { private int j; public intgetJ(){ return this.j; } public void setJ( int j ){ this.j = j; }}
  • 47. class Bean {int j}def b = new Bean()println ( b.j ) / println ( b.getJ())b.j = 33 / b.setJ( 33 )N Groovy beans can be kept in the same file
  • 49. def s = ‘aaaaaaa’
  • 50. def s = ‘aaaaaaa’def s = ’’’aaaaaaaaabbbbbbbb’’’
  • 51. def s = ‘aaaaaaa’def s = ’’’aaaaaaaaabbbbbbbb’’’def s = “aaaaaaa”def s = ”””aaaaaaaaabbbbbbbb”””
  • 52. def s = “aaaaaaa${b.j}”def s = ”””aaaa${ o.something() + b.j }aaaaabbbbbbbb”””
  • 53. def s = “aaaaaaa${b.j}”def s = ”””aaaa${ o.something() + b.j }aaaaabbbbbbbb”””log.info ( String.format( “ .. %s .. ”, val ))log.info ( “ .. ${val} .. ” )
  • 54. def s = “aaaaaaa${b.j}”def s = ”””aaaa${ o.something() + b.j }aaaaabbbbbbbb”””assert "aaaaa".class == Stringassert "${1+2}".class == org.codehaus.groovy.runtime.GStringImpl
  • 56. if ( o == null ) { throw new RuntimeException( “msg” ) }
  • 57. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”
  • 58. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”
  • 59. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”
  • 60. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”assert false, “Fatal error”
  • 61. if ( o == null ) { throw new RuntimeException( “msg” ) }assert o, “msg”assert ( o != null ), “msg”assert o, Long message”assert false, “Fatal error”Asserting code samples is a common practice
  • 62. def j = [1, 2] def k = [3, 4] assert j[0] == k[0]
  • 63. def j = [1, 2] def k = [3, 4] assert j[0] == k[0] Assertion failed: assert j[0] == k[0] || | || |1 | |3 | | [3, 4] | false [1, 2]
  • 64. GDK
  • 67. Object/Collection/Mapeach()any(), every()find(), findAll(), grep()join()collect()min(), max(), sum()inject()
  • 70. Q&A