Groovy sample code
satya - 7/19/2018, 2:39:27 PM
Printing a collection objects in groovy
Printing a collection objects in groovy
satya - 7/19/2018, 3:24:09 PM
Examples
//from a list
def list = ["A", "B", "C"]
for (item in list) {
   println item
}
//another
for (number in 1..3 ) {
    println number
}
//a map
def map = [a1:'b1', a2:'b2']
for ( item in map ) {
    println item.key 
}
//each syntax using closures
def list = ["A", "B"]
list.each {
	println it
}
//syntactic sugar
(1..3).each {
	println it
}
//with indexed closures
def list = ["A", "B", "C"]
list.eachWithIndex { val, idx ->
   println "${idx}. ${val}"
}
satya - 7/23/2018, 12:49:00 PM
working with maps in groovy
working with maps in groovy
satya - 7/23/2018, 1:08:54 PM
Here is an example messing with dynamic properties
package com.ai.groovy.learn.test1
class GroovyTest2 {
   def propMap = [:]
   
   def String get(String name)
   {
      propMap[name];
   }
   def String set(String name, String value)
   {
      propMap[name] = value;
      println "set method called ${name}:${value}"
   }
   
   static void main(String... args)
   {
      println "hello world"
      
      GroovyTest2 gc = new GroovyTest2();
      gc.p1 = "hello"
      gc.p2 = "why"
      
      println "done ${gc.p2}"
   }
}
satya - 7/23/2018, 1:09:45 PM
How are get and set method are resolved dynamically in groovy?
How are get and set method are resolved dynamically in groovy?
Search for: How are get and set method are resolved dynamically in groovy?
satya - 7/23/2018, 1:12:22 PM
what is def in groovy?
what is def in groovy?
satya - 7/23/2018, 1:19:46 PM
Native syntax for data structures
def mylist = []
def mymap = [:]
By default keys are strings
Strings don't have to be quoted as a key
def map = [CA: 'California', MI: 'Michigan']
satya - 7/23/2018, 1:21:17 PM
def as a type signature of a property
def var1;
var1 = "string";
var1 = 5
//similar to
Object var1;
satya - 7/23/2018, 1:22:48 PM
In the code above the def is redundant!!!
In the code above the def is redundant!!!
satya - 7/23/2018, 1:23:54 PM
So this code is better
package com.ai.groovy.learn.test1
class GroovyTest2 {
   private def propMap = [:]
   
   String get(String name)
   {
      propMap[name];
   }
   
   String set(String name, String value)
   {
      propMap[name] = value;
      println "set method called ${name}:${value}"
   }
   
   static void main(String... args)
   {
      println "hello world"
      
      GroovyTest2 gc = new GroovyTest2();
      gc.p1 = "hello"
      gc.p2 = "why"
      
      println "done ${gc.p2}"
   }
}
satya - 7/23/2018, 1:29:52 PM
Take a look
abstract class Abstract {         
    String name
    abstract def abstractMethod() 
    def concreteMethod() {
        println 'concrete'
    }
}
satya - 7/23/2018, 1:32:42 PM
Notes
1. The "def" means the method returns an object of any type
2. the last line apparently returns without a return
satya - 7/23/2018, 1:32:59 PM
return type of println in groovy
return type of println in groovy
satya - 7/23/2018, 1:35:36 PM
Methods in groovy are explained here as part of object orientation
Methods in groovy are explained here as part of object orientation
satya - 7/23/2018, 1:39:41 PM
println returns void
println returns void
satya - 7/23/2018, 1:40:04 PM
So the following code returns a null when defined with def
package com.ai.groovy.learn.test1
class GroovyTest2 {
   private def propMap = [:]
   
   String get(String name)
   {
      propMap[name];
   }
   
   String set(String name, String value)
   {
      propMap[name] = value;
      println "set method called ${name}:${value}"
   }
   
   def f1()
   {
      println "f1 called"
   }
   static void main(String... args)
   {
      println "hello world"
      
      GroovyTest2 gc = new GroovyTest2();
      gc.p1 = "hello"
      gc.p2 = "why"
      
      println "done ${gc.p2}"
      
      //it seem to return a null
      println gc.f1()
   }
}
satya - 7/23/2018, 1:55:00 PM
How are gets and sets resolved in Groovy?
How are gets and sets resolved in Groovy?
satya - 10/31/2019, 12:30:20 PM
Method call conventions and omitting parentheses is explained in the style guide. Here is the link again
satya - 10/31/2019, 12:37:20 PM
Few key notes from there: No parentheses
//Same
println ("hello")
println "hello"
method(a,b)
method a, b
satya - 10/31/2019, 12:37:58 PM
If the last argument is a closure
list.each( { println it } )
list.each(){ println it }
list.each  { println it }
satya - 10/31/2019, 12:40:36 PM
getters and setters are automatic
class Person {
    String name
}
satya - 10/31/2019, 12:42:36 PM
Easy constructors
class Server {
    String name
    Cluster cluster
}
def server = new Server(name: "Obelix", 
                cluster: aCluster)
satya - 10/31/2019, 12:43:24 PM
This is nice: with
server.name = application.name
server.status = status
server.sessionCount = 3
server.start()
server.stop()
vs:
server.with {
    name = application.name
    status = status
    sessionCount = 3
    start()
    stop()
}
satya - 10/31/2019, 2:07:48 PM
String surprises: Single quotes will not expand the string and behaves like Java
x = "xstring"
//wrong: will print abc $(x)
//because it is not in double quotes
print 'abc $(x)'
satya - 10/31/2019, 2:09:04 PM
What you want is double quotes to expand $ sign
print "abc $(x)"
//still wrong because you need to use {} and not ()
//correct one
print "abc ${x}"
//not that will print correctly
abc xstring
