Archive: June 2018

Groovy Goodness: Where Is My Class?

Posted on by  
Hubert Klein Ikkink

Groovy 2.5.0 makes it possible to get the location of a Class file by adding the method getLocation to the Class class. If the Class is part of the JDK the location returned is null, but otherwise we get the location of the JAR file or source file (if available) with the Class file.

In the following example we get the location for the internal JDK String class and the Groovy utility class ConfigSlurper:

Continue reading →

Groovy Goodness: Calculate MD5 And SHA Hash Values

Posted on by  
Hubert Klein Ikkink

Groovy adds a lot of useful methods to the String class. Since Groovy 2.5.0 we can even calculate MD5 and SHA hash values using the methods md5 and digest. The md5 method create a hash value using the MD5 algorithm. The digest method accepts the name of the algorithm as value. These values are dependent on the available algorithms on our Java platform. For example the algorithms MD2, MD5, SHA-1, SHA-256, SHA-384 and SHA-512 are by default available.

In the next example we use the md5 and digest methods on a String value:

Continue reading →

Groovy Goodness: Java 8 Stream Enhancements

Posted on by  
Hubert Klein Ikkink

Groovy 2.5.0 adds several methods to make working with Java 8 Streams more Groovy. First of all the methods toList and toSet are added to the Stream class. These methods will convert the stream to a List and Set using the Stream.collect method with Collectors.toList and Collectors.toSet as argument. Furthermore we can convert any array object to a Stream using the stream method that is added to all array objects.

In the following example we use the support of converting an array to a Stream and then getting a List and Set from the stream:

Continue reading →

Groovy Goodness: Using String Values In Ranges

Posted on by  
Hubert Klein Ikkink

We can use ranges in Groovy using an easy syntax where the start and end values of the range are separated by .. for an inclusive range and ..< for an exclusive range as we have seen in a previous post. The values of the range are mostly numbers or enum values. But we can also use String values to define a range. Groovy will check if the String values are the same length and if the values, except for the last character, are the same. Then the natural ordering of the last character of the String value, based on the character’s int value, is used to create the range values.

In the following example we define several ranges using String values. We can even define a reverse range using String values.

Continue reading →

Groovy Goodness: Use Range By Method To Set Steps Between Numbers

Posted on by  
Hubert Klein Ikkink

Groovy has support for defining ranges in the language. When we define a range of numbers the steps between the values in the range is 1 by default. We can change the step size using the step method. This method accepts a int value with a new step size. The result is a List object with the values. Since Groovy 2.5.0 the by method is added to ranges with numbers. The by method accepts also decimal numbers and the result of the method is a NumberRange object instead of a List.

In the following example Groovy script we first define a range with int values. We use the by method to change the step size using both an int value and BigDecimal value. We also use the by method for a range of BigDecimal numbers:

Continue reading →

PlantUML Pleasantness: Using AWS, Font Awesome, Devicons and Google Material Icons

Posted on by  
Hubert Klein Ikkink

In a previous post we learned how to use the built-in icons from the OpenIconic set. In the newer PlantUML versions several icon sets have been added as standard library. These sets include AWS icons, Font Awesome icons, Devicons and Google Material icons.

To use an icon from the icon set we must include the icon using the !include statement. In our PlantUML definition we reference the icon with the syntax <$iconName>. In the following example PlantUML definition we include 2 icons from the Font Awesome and Devicons icon sets:

Continue reading →

PlantUML Pleasantness: Setting Arrow Directions

Posted on by  
Hubert Klein Ikkink

PlantUML mostly does a good job organizing elements and arrows in the resulting diagram. But we can help PlantUML by defining the arrow direction in our PlantUML definition. We can use the keywords up, down, left and right inside the arrow definition.

In the following example we have five rectangles connected with arrows. We define the arrow direction for each arrow.

Continue reading →

Gradle Goodness: Enable Task Based On Offline Command Line Argument

Posted on by  
Hubert Klein Ikkink

One of the command line options of Gradle is --offline. With this option we run Gradle in offline mode to indicate we are not connected to network resources like the internet. This could be useful for example if we have defined dependencies in our build script that come from a remote repository, but we cannot access the remote repository, and we still want to run the build. Gradle will use the locally cached dependencies, without checking the remote repository. New dependencies, not already cached, cannot be downloaded of course, so in that scenario we still need a network connection.

We can check in our build script if the --offline command line argument is used. We can use this to disable tasks that depend on network resources so the build will not fail. To see if we invoked our build with the --offline option we can access the property gradle.startParameter.offline. The value is true if the command line argument --offline is used and false if the command line argument is not used.

Continue reading →

shadow-left