Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Only minor spelling mistakes


When using such POGOs from Java, the getter and setter are indeed theirthere, and can be used as usual, of course.


Code Block
def server = new Server(name: "Obelix", cluster: aClosureaCluster)

Using with() for repeated operations on the same bean


Instead of:

Code Block
status != null && status.equals(ControlConstants.STATUS_COMPLETED)


Code Block
throw new PluginException("""Failed to execute command list-applications:
     The group with name ${parameterMap.groupname[0]}
     is not compatible group of type ${SERVER_TYPE_NAME)}""")


Code Block
def list = [1, 4, 6, 9]

// by default, keys are Strings, no need to quote them
// you can wrap keys with () like [(variableStateAcronym)]: stateName] to insert a variable or object as a key.
def map = [CA: 'California', MI: 'Michigan']

def range = 10..20
def pattern = ~/fo*/

// equivalent to add()
list << 5

// call contains()
assert 4 in list
assert 5 in list
assert 15 in range

// subscript notation
assert list[1] == 4

// add a new key value pair
map << [WA: 'Washington']
// subscript notation
assert map['CA'] == 'California'
// property notation
assert map.WA == 'Washington'

// matches() strings against patterns
assert 'foo' =~ pattern


The power of switch

Groovy's switch are is much more powerful than in C-ish languages which usually only accept primitives and assimilated. Groovy's switch accepts pretty much any kind of type.

Code Block
def x = 1.23
def result = ""
switch (x) {
    case "foo": result = "found foo"
    // lets fall through
    case "bar": result += "bar"
    case [4, 5, 6, 'inList']:
        result = "list"
    case 12..30:
        result = "range"
    case Integer:
        result = "integer"
    case Number:
        result = "number"
    default: result = "default"
assert result == "number"


Code Block
import java.util.List as juList
import java.awt.List as aList

import java.awt.WindowConstants as WC


Code Block
def check(String name) {
    // name non-null and non-empty according to Groovy Truth
    assert name
    // safe navigation + Groovy Truth to check
     assert name?.size() > 3