aboutsummaryrefslogtreecommitdiffstats
path: root/config-lib/src/main/java/com/yahoo/config/ReferenceNode.java
blob: 956ff4f76e5bb8fb936455e67032273dbc2217ec (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

/**
 * A ReferenceNode class represents a reference (that is a config-id)
 * in a {@link ConfigInstance}.
 */
public class ReferenceNode extends LeafNode<String> {

    public ReferenceNode() {
    }

    /**
     * Creates a new ReferenceNode with the given value.
     *
     * @param value the value of this ReferenceNode
     */
    public ReferenceNode(String value) {
        super(true);
        this.value = stripQuotes(value);
    }

    /**
     * Returns the value of this reference node. Same as {@link
     * #toString()} since the value of a ReferenceNode is a String (but
     * implementations in other {@link LeafNode} sub-classes differ).
     *
     * @return the string representation of this ReferenceNode.
     */
    public String value() {
        return value;
    }

    @Override
    public String getValue() {
        return value();
    }

    @Override
    public String toString() {
        return (value == null) ? "(null)" : getValue();
    }

    @Override
    protected boolean doSetValue(String value) {
        this.value = stripQuotes(value);
        return true;
    }

    /**
     * Overrides {@link Node#postInitialize(String)}
     * Checks for ":parent:" values, which will be replaced by the configId.
     *
     * @param configId  the configId of the ConfigInstance that owns (or is) this node
     */
    @Override
    public void postInitialize(String configId) {
        super.postInitialize(configId);
        if (":parent:".equals(value()))  {
            doSetValue(configId);
        }
    }

    /**
     * Strips the quotes before or after the value, if present.
     */
    static String stripQuotes(String value) {
        if (value == null) {
            return value;
        }
        StringBuilder buffer = new StringBuilder(value.trim());
        if (buffer.length() > 0 && buffer.charAt(0) == '"') {
            buffer.deleteCharAt(0);
        }
        if (buffer.length() > 0 && buffer.charAt(buffer.length() - 1) == '"') {
            buffer.setLength(buffer.length() - 1);
        }
        return buffer.toString();
    }

}