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.
In the following example we use the ===
and !==
operator to check if objects refer to the same instance or not:
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.
In the following example we use the old way to check if object is not an instance of a type and the new !instanceof
operator:
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
.
In the following example we see several examples of setting or getting values using indices or keys:
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.
In the following example code we use the elvis assignment operator:
Continue reading →
I’m a developer and I like Scrum.
Not every developer does.
A complaint I sometimes hear is the following:
We spend so much time in meetings that I don’t get around to writing code!
— A frustrated developer
If you have - or are confronted with - such a complaint, I have some tips for you to take into consideration
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.
In the following example we use a default parameter value for a lambda expression.
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.
In the following example we use !in
:
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.
In the next example we use both methods to randomly order lists:
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.
In the next example we have a sample YAML as string that we parse using the parseText
method of YamlSlurper
:
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.
In the following example we use YamlBuilder
to create YAML:
Continue reading →
Groovy 3 adds the average
method to collections to calculate the average of the items in the collections. When the items are numbers simply the average is calculated. But we can also use a closure as argument to transform an item into a number value and then the average on that number value is calculated.
In the following example code we use the average method on a list of numbers and strings. And we use a closure to first transform an element before calculating the average:
Continue reading →
Spring Boot Admin describes several ways of connecting clients to the admin server.
If your Spring Boot applications are running on kubernetes or openshift then Spring Boot Admin can use
the Spring Cloud Kubernetes discovery client. In this blogpost i’ll show you how.
Continue reading →