hazelcast-master/hazelcast/src/main/java/com/hazelcast/internal/config/override/ConfigNode.java
/*
 * Copyright (c) 2008-2023, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.internal.config.override;

import javax.annotation.Nonnull;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * A representation of a single generic configuration node.
 * <p>
 * Used while overriding configuration with environment variables/system properties.
 */
class ConfigNode {
    private final ConfigNode parent;
    private final String name;
    private final Map<String, ConfigNode> children = new LinkedHashMap<>();
    private String value;
    private boolean isRead;

    ConfigNode(@Nonnull String name) {
        this(name, null);
    }

    ConfigNode(String name, ConfigNode parent) {
        Objects.requireNonNull(name);
        this.name = name;
        this.parent = parent;
    }

    boolean isRead() {
        return isRead;
    }

    String getName() {
        return name;
    }

    boolean hasValue() {
        return value != null;
    }

    String getValue() {
        isRead = true;
        return value;
    }

    void setValue(String value) {
        this.value = value;
    }

    Map<String, ConfigNode> getChildren() {
        return children;
    }

    Optional<ConfigNode> getParent() {
        return Optional.ofNullable(parent);
    }

    @Override
    public String toString() {
        return String.format("ConfigNode{name='%s', children=%s, value='%s'}", name, children, value);
    }
}
