Posts tagged Groovy 1.8
Groovy Goodness: Parse Date.toString() Value
Nov 10th
With Groovy 1.8.4 we can parse the output of the Date.toString() method back to a Date. For example we get the string value of a Date from an external source and want to parse it to a Date object. The format of the string must have the pattern “EEE MMM dd HH:mm:ss zzz yyyy” with the US Locale. This is used by the toString() method of the Date class.
Groovy Goodness: Add AST Transformations Transparently to Scripts
Jun 3rd
With Groovy 1.8 we can add compilation customizers when for example we want to run a Groovy script from our application code. Cedric Champeau has a nice blog post about this feature. And we already learned about the import customizer in a previous Groovy Goodness blog post.
Groovy Goodness: Add Imports Transparently to Scripts with ImportCustomizer
Jun 1st
Since Groovy 1.8.0 we can easily setup import statements for a Groovy compilation unit (for example a Groovy script file) without adding the imports to the script source. For example we have created domain classes in our own package. The script authors shouldn’t know about the packages and just pretend the classes are accessible from the ‘default’ package.
Groovy Goodness: Apply Read and Write Locking
May 28th
When we write applications with concurrency needs we can use the ReentrantReadWriteLock class to specify a lock on specific methods. Normally we have to add a field to our class of the type ReentrantReadWriteLock and then write code to get a read lock or write lock, do our stuff and finally release the lock again. Since Groovy 1.8 we can use the AST transformation annotations duo WithReadLock and WithWriteLock. Groovy will add a lock field and adds the code to acquire and release the locks again in our compiled class file. We don’t have to write this code ourselves anymore.
Groovy Goodness: Command Chain Expressions for Fluid DSLs
May 27th
Groovy 1.8 introduces command chain expression to further the support for DSLs. We already could leave out parenthesis when invoking top-level methods. But now we can also leave out punctuation when we chain methods calls, so we don’t have to type the dots anymore. This results in a DSL that looks and reads like a natural language.
Groovy Goodness: Canonical Annotation to Create Mutable Class
May 24th
With Groovy 1.8 we get a lot of AST transformations. We can combine @ToString, @EqualsAndHashCode and @TupleConstructor with the @Canonical annotation. This annotation will do all the transformations at once. If we want to customize one of the AST transformations, we add the annotation with configuration parameters extra after @Canonical.
Groovy Goodness: Cache Closure Results with Memoization
May 2nd
Closures are very powerful in Groovy. Groovy 1.8 introduces closure memoization. This means we can cache the result of a closure, so the next time we invoke the closure the result is returned immediately. This is very useful for time consuming computations in a closure.
Groovy Goodness: Recursion with Closure Trampoline Capability
Apr 28th
When we write recursive code we might get a stack overflow exception, because calls are placed on the stack to be resolved. Since Groovy 1.8 we can use the trampoline capability of closures to overcome this problem.
Groovy Goodness: New Dollar Slashy Strings
Apr 27th
Groovy already has a lot of ways to define a String value, and with Groovy 1.8 we have another one: the dollar slashy String. This is closely related to the slashy String definition we already knew (which also can be multi-line by the way, added in Groovy 1.8), but with different escaping rules. We don’t have to escape a slash if we use the dollar slashy String format, which we would have to do otherwise.
Groovy Goodness: Chain Closures Together with Closure Composition
Apr 27th
There are a lot of new features in Groovy 1.8. One of them is the possibility to compose a new closure by chaining two other closures together. We use the leftShift and rightShift operators (<< and >>) to combine multiple closures to create a new closure.