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

com.yahoo.config.codegen.ConfiggenUtil Maven / Gradle / Ivy

Go to download

Config java code generation from defintion files for Java Vespa components.

There is a newer version: 8.458.13
Show newest version
package com.yahoo.config.codegen;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author gjoranv
 */
public class ConfiggenUtil {

    /**
     * Create class name from def name
     * @param defName The file name without the '.def' suffix
     */
    public static String createClassName(String defName) {
        String className =  Arrays.stream(defName.split("-"))
                .map(ConfiggenUtil::capitalize)
                .collect(Collectors.joining())
                + "Config";

        if (! isLegalJavaIdentifier(className))
            throw new CodegenRuntimeException("Illegal config definition file name '" + defName +
                                                      "'. Must be a legal Java identifier.");

        return className;
    }

    private static String capitalize(String in) {
        StringBuilder sb = new StringBuilder(in);
        sb.setCharAt(0, Character.toTitleCase(in.charAt(0)));
        return sb.toString();
    }

    private static boolean isLegalJavaIdentifier(String name) {
        if (name.isEmpty()) return false;
        if (! Character.isJavaIdentifierStart(name.charAt(0))) return false;

        for (char c : name.substring(1).toCharArray()) {
            if (! Character.isJavaIdentifierPart(c)) return false;
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy