Groovy 3 adds the takeBetween
method to the String
class. With this method we can get all the characters that are enclosed by string values. We can specify one enclosed string value and then all text between the the first occurrence of the string and the second occurrence is returned. If multiple parts are enclosed by the string values we can also specify which occurrence we want. If the text is enclosed by different string values we can use a variant of takeBetween
that takes two string values to indicate the boundaries of the text we want. Also with two different enclosed string values we can use an argument to get the n-th occurrence of the string that is found.
Since Groovy 3 we can also use takeBefore
and takeAfter
to get the string before or after a given string value. All three methods will return an empty string if no text can be found.
Continue reading →
Groovy adds a lot of methods to the Java String
class. For example we can use the take
method to get a certain number of characters from the start of a string value. With the drop
method we remove a given number of characters from the start of the string. In Groovy 3 we can now also take and drop a certain number of characters from the end of a string using the methods takeRight
and dropRight
.
Continue reading →
Groovy has used the ==
operator to check if objects are equal for a long time. To test if object instances are the same we must use the is
method. Groovy 3 adds a new operator for the is
method and that is the ===
operator. And to negate the result of the is
method we can use the !==
operator.
Continue reading →
Groovy 3 adds the !instanceof
operator to check if an object is not an instance of a type. This is a shorthand for using instanceof
and then negate the result. It shows how little changes can make code easier to read.
Continue reading →
Groovy 3 adds the feature of safe index based access for lists, arrays and maps. This means we can use ?[index]
to get or a set a value on a list or array without getting a NullPointerException
when the list or array is not initialised. With maps we can use ?[key]
for getting a value or set a value and when the map object is not initialised we don’t get a NullPointerException
.
Continue reading →
Groovy 3 adds a new operator to the language: elvis assignment operator (?=
). This operator is a shorthand for an assignment where we want to assign a value to a variable or property if the variable or property is null or false (following Groovy truth). If the value of the variable or property is not null or false (again apply Groovy truth rules), the value stays the same.
Continue reading →
I’m a developer and I like Scrum.
Not every developer does.
A complaint I sometimes hear is the following:
Continue reading →
Groovy 3 adds support for Java’s lambda syntax expressions. This way we can write code in Groovy using lambda expressions just like in Java. But Groovy adds an additional feature and that is default parameter values for lambda expressions.
Continue reading →
Groovy contains lots of little gems that just make live easier and our code more expressive. Groovy 3 doesn’t disappoint by adding some nice syntax additions. For example we can now use !in
to check if an item is not in a collection, opposed to in
that checks if an item is in a collection.
Continue reading →
In Java we can use Collections.shuffle
method to randomly reorder items in a list. Groovy 3.0.0 adds the shuffle
and shuffled
methods to a List
or array directly. The implementation delegates to Collections.shuffle
. The shuffle
method will reorder the original list, so there is a side effect using this method. Or we can use the shuffled
method that will return a copy of the original list where the items are randomly ordered.
Continue reading →
In Groovy we have useful classes to parse JSON and XML: JsonSlurper
and XmlSlurper
. Groovy 3 adds the YamlSlurper
class to read in YAML formatted strings. The result of parsing the YAML content is a Map
object.
Continue reading →
Groovy 3 adds the YamlBuilder
class to create YAML output using a Groovy syntax. The YamlBuilder
is closely related to JsonBuilder
that is described in a previous post. We define a hierarchy using a builder syntax where we can use primitive types, strings, collections and objects. Once we have build our structure we can use the toString()
method to get a string representation in YAML format.
Continue reading →