Archive: March 2020

Groovy Goodness: Getting Parts Of A String Enclosed By Strings

Posted on by  
Hubert Klein Ikkink

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.

In the following example we use the takeBefore, takeAfter and takeBetween methods with different arguments:

Continue reading →

Groovy Goodness: Taking Or Dropping Number Of Characters From A String

Posted on by  
Hubert Klein Ikkink

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.

In the following example we see how we can use the methods:

Continue reading →

Groovy Goodness: Check Object Instances Are The Same With === Operator

Posted on by  
Hubert Klein Ikkink

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 Goodness: Using !instanceof Operator

Posted on by  
Hubert Klein Ikkink

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 Goodness: Safe Index Based Access For Lists, Arrays and Maps

Posted on by  
Hubert Klein Ikkink

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 Goodness: The Elvis Assignment Operator

Posted on by  
Hubert Klein Ikkink

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 →

So many meetings in Scrum!

Posted on by  
Jasper Bogers

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 Goodness: Lambda Default Parameter Value

Posted on by  
Hubert Klein Ikkink

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 Goodness: Shuffle List or Array

Posted on by  
Hubert Klein Ikkink

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 →

Groovy Goodness: Parse YAML With YamlSlurper

Posted on by  
Hubert Klein Ikkink

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 →

shadow-left