All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.joinedworkz.common.builder.ParameterBuilder.xtend Maven / Gradle / Ivy

There is a newer version: 1.3.51
Show newest version
package org.joinedworkz.common.builder

class ParameterBuilder {

    final StringBuilder buffer = new StringBuilder
    final String separator
    boolean empty = true

    new() {
        this(', ')
    }

    new(String separator) {
        this.separator = separator
    }

    def ParameterBuilder annotationClass(String c) {
        buffer.append(c)
        return this
    }

    def appendPrefix(String parameterText) {
        buffer.append(parameterText)
    }
    
    def append(Object parameterObject) {
        if (parameterObject !== null) {
            return appendWhenTrue(true, parameterObject.toString)
        }
    }

    def append(String parameterText) {
        return appendWhenTrue(true, parameterText)
    }

    def next() {
        appendCommaIfNotEmpty
    }
    
    def appendPart(Object parameterText) {
        if (parameterText !== null) {
            if (empty) {
                empty = false
            }
            buffer.append(parameterText.toString)
        }
        return this
    }

    def appendPart(String parameterText) {
        if (empty) {
            empty = false
        }
        buffer.append(parameterText)
        return this
    }

    def appendWhenNotNull(Object conditionObject, String parameterText) {
        return appendWhenTrue(conditionObject !== null, parameterText)
    }

    def appendWhenTrue(boolean resultOfCondition, String parameterText) {
        if (resultOfCondition) {
            appendCommaIfNotEmpty()
            buffer.append(parameterText)
        }
        return this
    }

    def appendPartWhenTrue(boolean resultOfCondition, String parameterText) {
        if (resultOfCondition) {
            appendPart(parameterText)
        }
        return this
    }

    protected def appendCommaIfNotEmpty() {
        if (empty) {
            empty = false
        } else {
            buffer.append(separator)
        }
    }
    
    def isEmpty() {
        return empty
    }

    def build() {
        return buffer.toString
    }

    override toString() {
        return build()
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy