aboutsummaryrefslogtreecommitdiffstats
path: root/configgen/src/main/java/com/yahoo/config/codegen/DefaultValue.java
blob: c057105181a1d774776cdc4c78d4d141be103e61 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.codegen;

/**
 * An immutable class representing a default value of a config variable
 *
 * @author bratseth
 */
public class DefaultValue {

    private String value = null;

     // The variable type. Always set UNLESS the value is null.
    private DefLine.Type type = null;

    /** Null value. */
    public DefaultValue() {
    }

    /** A default value with the given value and type. */
    public DefaultValue(String value, DefLine.Type type) {
        this.value = value;
        this.type = type;
    }

    /** Returns the toString of the default value. */
    public String getValue() {
        return value;
    }

    /** Returns the string representation of this value. */
    public String getStringRepresentation() {
        if (value == null)
            return "null";
        else if ("bool".equals(type.getName()))
            return value;
        else if ("int".equals(type.getName()))
            return value;
        else if ("long".equals(type.getName()))
            return value;
        else if ("double".equals(type.getName()))
            return value;
        else if ("enum".equals(type.getName()))
            return value;
        else {
            // building a string, do unicode-escaping
            StringBuilder sb = new StringBuilder();
            for (char c : value.toCharArray()) {
                if (c > '\u007f') {
                    sb.append(String.format("\\u%04X", (int) c));
                } else {
                    sb.append(c);
                }
            }
            return "\"" + sb + "\"";
        }
    }

}