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

/**
 * Superclass for all leaf nodes in a {@link ConfigInstance}.
 * <p>
 * Subclasses represent leaf nodes with different types. These
 * implementations should implement method value() with return-value
 * corresponding to the actual type.
 *
 */
public abstract class LeafNode<T> extends Node implements Cloneable {

    protected boolean initialized;
    protected T value;

    /**
     * Creates a new, uninitialized LeafNode
     */
    protected LeafNode() {
        initialized = false;
    }

    /**
     * Creates a new LeafNode.
     *
     * @param initialized true if this node is initialized.
     */
    protected LeafNode(boolean initialized) {
        this.initialized = initialized;
    }

    public T value() {
        return value;
    }

    /**
     * Subclasses must implement this, in compliance with the rules given in the return tag.
     *
     * @return the String representation of the node value, or the string "(null)" if the value is null.
     */
    public abstract String toString();

    /**
     * Subclasses must implement this, in compliance with the rules given in the return tag.
     *
     * @return the String representation of the node value, or the 'null' object if the node value is null.
     */
    public abstract String getValue();

    /**
     * Sets the value based on a string representation. Returns false if the value could
     * not be set from the given string.
     * TODO: return void (see doSetValue)
     *
     * @param value the value to set
     * @return true on success, false otherwise
     * @throws IllegalArgumentException when value is null
     */
    protected final boolean setValue(String value) {
        if (value == null)
            throw new IllegalArgumentException("Null value is not allowed");
        return doSetValue(value);
    }

    // TODO: should throw exception instead of return false.
    protected abstract boolean doSetValue(String value);

    /**
     * This method is meant for internal use in the configuration
     * system. Overrides Object.clone().
     *
     * @return a new instance similar to this object.
     */
    @Override
    protected LeafNode<?> clone() {
        try {
            return (LeafNode<?>) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new ConfigurationRuntimeException(e);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (! (o instanceof LeafNode<?> other)) return false;

        return value == null ? other.value == null :  value().equals(other.value);
    }

    @Override
    public int hashCode() {
        return (value == null) ? 0 : value.hashCode();
    }

    void serialize(String name, Serializer serializer) {
        serializer.serialize(name, getValue());
    }

    void serialize(Serializer serializer) {
        serializer.serialize(getValue());
    }

}