Avoiding NullpointerException with Java Objects to Kotlin
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…
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…
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.
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 |
|
|
|
|
|
|
|
|
|
|
|
|
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.
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.
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.
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.
Groovy 5 introduced some extra overloaded methods for the collectEntries method, that can be used to transform an iterable object into a Map.
You can now pass a closure or function as arguments to transform the original iterable element into the key and value for the resulting Map. It is now also possible to pass a so-called collector Map that will be used to extend with new key/value pairs.
Besides extra overloaded method signatures for collectEntries Groovy 5 also adds the new methods withCollectedKeys and withCollectedValues. With the method withCollectedKeys a closure or function can passed to create the keys for the new Map based on the elements from the iterable. The value of the key/value pair is the unchanged element. You use the method withCollectedValues to pass a closure or function to create the value for the new key/value pair in the resulting Map. The key will be the original element from the iterable.
The Nushell command enumerate adds an index value to each item in a list. The index value is stored in an extra column named index. You can use this column to filter data based on the index value.