diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2016-06-15 23:09:44 +0200 |
commit | 72231250ed81e10d66bfe70701e64fa5fe50f712 (patch) | |
tree | 2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /config-lib/src/main/java/com/yahoo/config/LeafNode.java |
Publish
Diffstat (limited to 'config-lib/src/main/java/com/yahoo/config/LeafNode.java')
-rw-r--r-- | config-lib/src/main/java/com/yahoo/config/LeafNode.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/config-lib/src/main/java/com/yahoo/config/LeafNode.java b/config-lib/src/main/java/com/yahoo/config/LeafNode.java new file mode 100644 index 00000000000..24ec534e222 --- /dev/null +++ b/config-lib/src/main/java/com/yahoo/config/LeafNode.java @@ -0,0 +1,120 @@ +// Copyright 2016 Yahoo Inc. 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()); + } +} |