Groovy: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 16: Line 16:
=Data Types=
=Data Types=


=<span id='String_Programming'></span>Strings=
==<span id='String_Programming'></span><span id='Strings'></span>String==


==Single-Quoted vs. Double-Quoted Strings==
===Single-Quoted vs. Double-Quoted Strings===


Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation:
Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation:
Line 27: Line 27:
</syntaxhighlight>
</syntaxhighlight>


==Rules for String Interpolation==
===Rules for String Interpolation===


Groovy supports declaring a string with either single quotes or double quotes.  
Groovy supports declaring a string with either single quotes or double quotes.  
Line 53: Line 53:
</syntaxhighlight>
</syntaxhighlight>


==Multi-Line Strings==
===Multi-Line Strings===


Multi-line strings can be represented in Groovy by enclosing them in <code>"""</code> or <code>&#39;&#39;'</code>:
Multi-line strings can be represented in Groovy by enclosing them in <code>"""</code> or <code>&#39;&#39;'</code>:
Line 109: Line 109:
</syntaxhighlight>
</syntaxhighlight>


==String Comparison==
===String Comparison===


Operators == and != work with strings. <font color=darkgray>Explain why.</font>
Operators == and != work with strings. <font color=darkgray>Explain why.</font>
==Boolean==
A boolean variable evaluates to true if assigned a different type variable that is not null or not empty, and false otherwise.
The following expressions assign "true" to b:
<syntaxhighlight lang='groovy'>
boolean b
b = "something"
HashSet s = new HashSet()
s.add("something")
b = s
</syntaxhighlight>
The following expressions assign "false" to b:
<syntaxhighlight lang='groovy'>
boolean b
b = ""
b = new ArrayList()
b = new HashSet()
</syntaxhighlight>


=Variables=
=Variables=

Revision as of 23:40, 25 March 2021

External

TODO

  • Tutorial: www.tutorialspoint.com/groovy/index.htm

TODO Groovy basics: https://docs.gradle.org/current/userguide/writing_build_scripts.html#groovy-dsl-basics

Overview

To experiment with syntax, use the IntelliJ Groovy Console:

IntelliJ Groovy Console

Data Types

String

Single-Quoted vs. Double-Quoted Strings

Groovy has both double-quoted and single-quoted String literals. The main difference is that double-quoted String literals support String interpolation:

def x = 10
println "result is $x" // prints: result is 10

Rules for String Interpolation

Groovy supports declaring a string with either single quotes or double quotes.

If the string is single-quoted, the variable interpolation behavior is turned off, similarly to how bash handles single-quoted strings.

def user="Elemental"
echo 'Hello ${user}'

will output:

Hello ${user}

Double-quoted string support dollar-sign based interpolation:

def user="Elemental"
echo "Hello ${user}"

will output:

Hello Elemental

Multi-Line Strings

Multi-line strings can be represented in Groovy by enclosing them in """ or ''':

print """
  this is 
  a multi-line
  string
""".stripIndent()
print '''
  this is 
  a multi-line
  string
'''.stripIndent()

The difference between """ or ''' is in how variable interpolation is handled when variables are declared inside the quoted string. The rules describe here apply: Rules for String Interpolation. As such, the first example below will interpolate the variable, while the second will not:

def v="multi-line"
print """
  this is 
  a ${v}
  string
""".stripIndent()

will display:

this is 
a multi-line
string

while this

def v="multi-line"
print '''
  this is 
  a ${v}
  string
'''.stripIndent()

will display:

this is
a ${v}
string

String Comparison

Operators == and != work with strings. Explain why.

Boolean

A boolean variable evaluates to true if assigned a different type variable that is not null or not empty, and false otherwise.

The following expressions assign "true" to b:

boolean b
b = "something"

HashSet s = new HashSet()
s.add("something")
b = s

The following expressions assign "false" to b:

boolean b
b = ""
b = new ArrayList()
b = new HashSet()

Variables

Variables can be defined using either their type (String) or with the keywords def, which is equivalent with var

String a
def b
var c

def/var act as a replacement for the type name when the actual type is not provided (either because you don't care about it or you're relying on type inference). You can think about them as an alias to Object.

For scripts, undeclared variables are assumed to come from the Script binding.


Working with Closures

http://groovy-lang.org/closures.html

Defining a Closure

def myClosure = { e -> println "Clicked on $e.source" }

Implicit Paramenter

When a closure does not explicitly define a parameter using the '->' syntax, the closure 'always defines an implicit parameter named "it".

Passing Closures to Methods

If the closure is the last argument for a method, it can be passed outside the argument list.

https://mrhaki.blogspot.com/2009/11/groovy-goodness-passing-closures-to.html

Passing a Method as Closure

def withTestSetup(Closure test) {
  performTestSetup()
  test()
}

Invocation - the test setup is prior performed on the stack and then doTestLogic1() and doTestLogic2() methods are invoked, wrapped in a closure:

withTestSetup({
  doTestLogic1()
  doTestLogic2()
})

Template Engines

Groovy Template Engines

Files

filename = 'example.txt'
File f = new File(filename)
def lines = f.readLines()
for (line in lines) {
    // ...
}
filename = 'example.txt'
File f = new File(filename)
f.eachLine({
 if (!it.startsWith("#") && !it.trim().isEmpty()) {
   ...
  }
})

Data Structures

List

def list = ["A", "B", "C"]

Internally is stored as an java.util.ArrayList.

Map

def artifacts = [

        "chart-A": [
                "watchFor": ["src/charts/chart-A"],
                "chartName": "a",
        ],
        "chart-B": [
                "watchFor": ["src/charts/chart-B"],
                "chartName": "b",
        ],
        "script": [
                "watchFor": ["src/bin/run", "src/ansible", "script/bin/lib/a.shlib"],
        ]
]

Empty map:

Map testingBranches = [:]

Dynamic Keywords

@Field

https://docs.groovy-lang.org/latest/html/gapi/groovy/transform/Field.html

Variable annotation used for changing the scope of a variable within a script from being within the run method of the script to being at the class level for the script. The annotated variable will become a private field of the script class.

Script

Groovy is automatically wrapping every program in a class called Script.

Method Parameters with Default Values

Default values can be assigned to a parameter in a method:

def say(msg = 'Hello', name = 'world') {
  "$msg $name!"
}

Control Flow

if/else

A null condition evaluates to false:

def a = null
if (a) {
  println "blue"
}
else {
  println "red"
}

will display "red".