Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Groovy Goodness: @Builder Definition with Extra Type Checks

DZone's Guide to

Groovy Goodness: @Builder Definition with Extra Type Checks

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

We have seen some features of the @Builder AST transformation in previous and other blog post. We can use another strategy to let the AST transformation generate a class where the class only has a constructor that accepts a builder class. And with @CompileStatic we can even make sure that all required properties must have a value set by the builder before the constructor accepts the argument. We use the builderStrategyannotation parameter and set it to InitializerStrategy:

import groovy.transform.builder.Builder
import groovy.transform.builder.InitializerStrategy

@Builder(builderStrategy = InitializerStrategy)
class Message {
    String from, to, subject, body
}

def message = Message.createInitializer()
        .from('mrhaki@mrhaki.com')
        .subject('Groovy 2.3 is released')

// Returned object is not Message, but
// internal class Message$MessageInitializer
assert !(message instanceof Message)

// Now we can use the initializer in the
// only constructor of Message.
def messageInstance = new Message(message)

assert messageInstance instanceof Message
assert messageInstance.from == 'mrhaki@mrhaki.com'
assert messageInstance.subject == 'Groovy 2.3 is released'

We can customize which properties need to be set with the includes and/or excludes annotation parameter. We can also customize the name of the method that create the builder instance with the parameter builderMethodName:

package com.mrhaki.blog.groovy

import groovy.transform.CompileStatic
import groovy.transform.builder.Builder
import groovy.transform.builder.InitializerStrategy

@Builder(builderStrategy = InitializerStrategy,
        excludes = 'body',
        builderMethodName = 'creator')
class Message {
    String from, to, subject, body
}

// With @CompileStatic the compiler will check
// that all properties defined in the @Builder
// configuration are set. This way we can
// implement required properties for a class
// that need to be set before the object can
// be created.
@CompileStatic
def createMessage() {
    def messageInit = Message.creator()
            .from('mrhaki@mrhaki.com')
            .to('mail@host.nl')
            .subject('Groovy 2.3 is released')

    def message = new Message(messageInit)
    message
}

final Message message = createMessage()

assert message.from == 'mrhaki@mrhaki.com'
assert message.subject == 'Groovy 2.3 is released'

Code written with Groovy 2.3.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:

Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}