Groovy supports operator overloading since the start. Operator overloading is implemented by an actual method signature that maps to an operator. For example an object with a plus method can be used with the + operator. There is a list of methods and operators available on the Groovy website.
As long as an object has a method with a name that Groovy understands the corresponding operator can be used in Groovy code. This is even true for Java objects. Since Groovy 5 you can use the groovy.transform.OperatorRename annotation on classes, methods or constructors to map other method names to the Groovy operator overloading method names. This is very useful for third-party classes that you cannot change, but still want to use simple operators in Groovy code. You can reassign a method name to the following methods so an operator can be used: plus, minus, multiply, div, remainder, power, leftShift, rightShift, rightShiftUnassigned, and, or, xor, compareTo. Suppose you use a class with an add method and want to use the + operator for this method. The following annotation can be used @OperatorRename(plus = 'add') for a method and inside the method you can use the + operator instead of the add method of the class.
Continue reading →
You’re coding late at night. The lights are low.
Your favorite playlist is playing, maybe something only you would call “focus music.”
Your IDE is set to a theme that feels just right.
You’re not rushing.
You’re not distracted. You’re in the zone.
Continue reading →
Kotlin is null-safe, a quote I hear often from Kotlin developers.
And yet, I still managed to get a NullPointerException while converting a java.util.Optional to a nullable Kotlin value…
Continue reading →
Early on in their career, most software developers develop a muscle memory for writing efficient code and avoiding code duplication.
It’s unfortunate that in modular architectures, this practice can seep through into data modelling without context awareness, leading to tight coupling and constraining the software’s ability to be changed.
Continue reading →
AsciiDoc and UML are two well known technologies for software design.
A very nice way of creating beautiful PDF documents is to combine both by embedding PlantUML in your AsciiDoc.
Generating the PDF can be a bit cumbersome, but with the following Maven setup it is quick and easy.
Continue reading →
Since Groovy 1.8.3 Groovy has an implies() method for Boolean types. Groovy 5 adds an operator ==> for this method so you have a shorter way to express a logical implication. A logical implication is a logical function that can be expressed as P ==> Q. You can read this as P implies Q or if P than Q. This expression is true in every case except when P is true, but Q is false. The following truth table shows the interpretaton of the logical implication operator:
P |
Q |
P ==> Q |
false
|
true
|
true
|
false
|
false
|
true
|
true
|
true
|
true
|
true
|
false
|
false
|
Continue reading →
Groovy 5 adds support for using an index variable in a for-in loop. You must define an extra variable as first argument of the for loop. This variable will be used as index variable. For each iteration of the code in the for-in loop the value of this index variable is incremented by 1. You can use this value in the for loop.
Continue reading →
Groovy 5 adds a new utility method to create an ascii bar chart. You can use the bar method in the org.codehaus.groovy.util.StringUtil class. You can pass a value, a minimum and maximum value and optinally specify the width of the bar chart. The result is a String value consisting of a number of "blocks". A block could be whole, but also 1/8 eights of the block are used to get a nice looking bar chart. How many of these values are needed is based on the input arguments. With this method you have a nice way to format number values on a command-line.
Continue reading →
Protobuf provides an easier and more performant approach to serialising and deserialising data.
These performance capabilities and the ability to provide a common schema for data transfer objects, coupled with a
performant message brokering service such as Kafka seems to be a match made in heaven.
Continue reading →
Groovy is known for exending standard Java classes with extra methods or extra arguments for existing methods. Since Groovy 5 you can use a range as argument for the List.subList method. The range is used to determine the begin and end index of the original List instance to return.
Continue reading →