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.

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.

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.

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.

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.

Continue reading →

Groovy Goodness: Create YAML With YamlBuilder

Posted on by  
Hubert Klein Ikkink

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 →

shadow-left