alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (codegen.groovy)

This example Java source code file (codegen.groovy) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

byte, character, hash_code, integer, key_number_method, long, short, string

The codegen.groovy Java example source code

String[] templateDirs = [properties["collection.template.dir"],
                         properties["collection.template.test.dir"]]
String[] outputDirs = [properties["collection.src.dir"],
                       properties["collection.testsrc.dir"]]

templateDirs.eachWithIndex { templateDir, i ->
    convertSources templateDir, outputDirs[i]
}

void convertSources(String templateDir, String outputDir) {
    String[] keyPrimitives = ["byte", "char", "short", "int", "long"]
    String[] keyObjects = ["Byte", "Character", "Short", "Integer", "Long"]
    String[] keyNumberMethod = ["byteValue", "charValue", "shortValue", "intValue", "longValue"]

    keyPrimitives.eachWithIndex { keyPrimitive, i ->
        convertTemplates templateDir, outputDir, keyPrimitive, keyObjects[i], keyNumberMethod[i]
    }
}

void convertTemplates(String templateDir,
                      String outputDir,
                      String keyPrimitive,
                      String keyObject,
                      String keyNumberMethod) {
    def keyName = keyPrimitive.capitalize()
    def replaceFrom = "(^.*)K([^.]+)\\.template\$"
    def replaceTo = "\\1" + keyName + "\\2.java"
    def hashCodeFn = keyPrimitive.equals("long") ? "(int) (key ^ (key >>> 32))" : "(int) key"
    ant.copy(todir: outputDir) {
        fileset(dir: templateDir) {
            include(name: "**/*.template")
        }
        filterset() {
            filter(token: "K", value: keyName)
            filter(token: "k", value: keyPrimitive)
            filter(token: "O", value: keyObject)
            filter(token: "KEY_NUMBER_METHOD", value: keyNumberMethod)
            filter(token: "HASH_CODE", value: hashCodeFn)
        }
        regexpmapper(from: replaceFrom, to: replaceTo)
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java codegen.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.