aboutsummaryrefslogtreecommitdiffstats
path: root/configgen/src/main/java/com/yahoo/config/codegen/LeafCNode.java
blob: 82232cc0a28e15a6cd822e50ec96a4a85e186fbb (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.codegen;

/**
 * @author gjoranv
 */
public abstract class LeafCNode extends CNode {

    private boolean isInitialized = false;
    private DefaultValue defaultValue = null;
    private boolean restart = false;

    /** Constructor for the leaf nodes */
    protected LeafCNode(InnerCNode parent, String name) {
        super(parent, name);
    }

    public static LeafCNode newInstance(DefLine.Type type, InnerCNode parent, String name) {
        try {
            return switch (type.name) {
                case "int" -> new IntegerLeaf(parent, name);
                case "long" -> new LongLeaf(parent, name);
                case "double" -> new DoubleLeaf(parent, name);
                case "bool" -> new BooleanLeaf(parent, name);
                case "string" -> new StringLeaf(parent, name);
                case "reference" -> new ReferenceLeaf(parent, name);
                case "file" -> new FileLeaf(parent, name);
                case "path" -> new PathLeaf(parent, name);
                case "optionalPath" -> new OptionalPathLeaf(parent, name);
                case "enum" -> new EnumLeaf(parent, name, type.enumArray);
                case "url" -> new UrlLeaf(parent, name);
                case "model" -> new ModelLeaf(parent, name);
                default -> null;
            };
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static LeafCNode newInstance(DefLine.Type type, InnerCNode parent, String name, String defVal) {
        LeafCNode ret = newInstance(type, parent, name);
        if (defVal!=null) {
            DefaultValue def = new DefaultValue(defVal, type);
            ret.setDefaultValue(def);
        }
        return ret;
    }

    public abstract String getType();

    @Override
    public CNode[] getChildren() {
        return new CNode[0];
    }

    @Override
    public CNode getChild(String name) {
        return null;
    }

    public DefaultValue getDefaultValue() {
        return defaultValue;
    }

    public LeafCNode setDefaultValue(DefaultValue defaultValue) {
        this.defaultValue = defaultValue;
        return this;
    }

    /**
     * @param defaultValue the value to check.
     * @throws IllegalArgumentException if the value is illegal according to the node type.
     */
    public void checkDefaultValue(DefaultValue defaultValue) throws IllegalArgumentException {
    }

    @Override
    protected void setLeaf(String name, DefLine defLine, String comment) throws IllegalArgumentException {
        DefLine.Type type = defLine.getType();
        // TODO: why the !is... conditions?
        if (!isMap && !isArray && isInitialized) {
            throw new IllegalArgumentException(name + " is already defined");
        }
        isInitialized = true;
        checkMyName(name);
        if (!type.name.equalsIgnoreCase(getType())) {
            throw new IllegalArgumentException("Type " + type.name + " does not match " + getType());
        }
        setValue(defLine.getDefault());
        setComment(comment);
        restart |= defLine.getRestart();
    }

    @Override
    public boolean needRestart() {
        return restart;
    }

    public final void setValue(DefaultValue defaultValue) throws IllegalArgumentException {
        try {
            checkDefaultValue(defaultValue);
            setDefaultValue(defaultValue);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid default value", e);
        }
    }

    /**
     * Superclass for leaf nodes that should not generate class.
     */
    public static abstract class NoClassLeafCNode extends LeafCNode {
        protected NoClassLeafCNode(InnerCNode parent, String name) {
            super(parent, name);
        }
    }

    /**
     * Superclass for no-class leaf nodes that cannot have a default.
     */
    public static abstract class NoClassNoDefaultLeafCNode extends LeafCNode {
        protected NoClassNoDefaultLeafCNode(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public LeafCNode setDefaultValue(DefaultValue defaultValue) {
            if (defaultValue != null)
                throw new IllegalArgumentException("Parameters of type '" + getType() + "' cannot have a default value.");
            return this;
        }
    }

    public static class IntegerLeaf extends NoClassLeafCNode {
        protected IntegerLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "int";
        }
    }

    public static class LongLeaf extends NoClassLeafCNode {
        protected LongLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "long";
        }
    }

    public static class DoubleLeaf extends NoClassLeafCNode {
        protected DoubleLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "double";
        }
    }

    public static class BooleanLeaf extends NoClassLeafCNode {
        protected BooleanLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "bool";
        }
    }

    public static class StringLeaf extends NoClassLeafCNode {
        protected StringLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "string";
        }
    }

    public static class ReferenceLeaf extends StringLeaf {
        ReferenceLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "reference";
        }
    }

    public static class FileLeaf extends NoClassNoDefaultLeafCNode {
        FileLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "file";
        }
    }

    public static class PathLeaf extends NoClassNoDefaultLeafCNode {
        PathLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "path";
        }
    }

    public static class OptionalPathLeaf extends NoClassLeafCNode {
        OptionalPathLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "optionalPath";
        }
    }

    public static class UrlLeaf extends NoClassLeafCNode {
        UrlLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "url";
        }
    }

    public static class ModelLeaf extends NoClassLeafCNode {
        ModelLeaf(InnerCNode parent, String name) {
            super(parent, name);
        }

        @Override
        public String getType() {
            return "model";
        }
    }

    public static class EnumLeaf extends LeafCNode {

        private final String[] legalValues;

        protected EnumLeaf(InnerCNode parent, String name, String[] valArray) {
            super(parent, name);
            this.legalValues = valArray;
        }

        @Override
        public String getType() {
            return "enum";
        }

        /** Returns this enum's legal values. */
        public String[] getLegalValues() {
            return legalValues;
        }

        @Override
        public void checkDefaultValue(DefaultValue defaultValue) throws IllegalArgumentException {
            if ((defaultValue != null) && (defaultValue.getValue() != null))  {
                String defaultString = null;
                String value = defaultValue.getValue();
                for (String val : legalValues) {
                    if (value.equals(val)) {
                        defaultString = val;
                    }
                }
                if (defaultString == null)
                    throw new IllegalArgumentException("Could not initialize enum with: " + value);
            }
        }
    }

}