aboutsummaryrefslogtreecommitdiffstats
path: root/configgen/src/main/java/com/yahoo/config/codegen/ConfiggenUtil.java
blob: c611da0c38bf2dfde4349040f356f9f182ee086d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
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;
    }

    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;
    }

}