// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

import edu.umd.cs.findbugs.annotations.NonNull;

/**
 * Superclass for all leaf nodes in a {@link ConfigInstance}.
 * <p>
 * Subclasses represents 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;
    }

    /**
     * Try to initialize this node with the given value. Returns true
     * on success, false otherwise.
     *
     * @param value     the string represention of the desired node value.
     * @return true on success, false otherwise.
     */
    final boolean initialize(String value) {
        boolean success = setValue(value);
        initialized |= success;
        return success;
    }

    /**
     * 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(@NonNull 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 Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            throw new ConfigurationRuntimeException(e);
        }
    }

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

        LeafNode<?> other = (LeafNode)o;
        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());
    }
}

@@ -955,412 +955,8 @@
  %7D%0A%0A
-    /**%0A     * Try to initialize this node with the given value. Returns true%0A     * on success, false otherwise.%0A     *%0A     * @param value     the string represention of the desired node value.%0A     * @return true on success, false otherwise.%0A     */%0A    final boolean initialize(String value) %7B%0A        boolean success = setValue(value);%0A        initialized %7C= success;%0A        return success;%0A    %7D%0A%0A
     

Remove LeafNode.initialize().