Class ValueNode<T>

java.lang.Object
com.northconcepts.datapipeline.core.Node
com.northconcepts.datapipeline.core.ValueNode<T>
All Implemented Interfaces:
Session, Serializable, Cloneable, Comparable<ValueNode<T>>
Direct Known Subclasses:
ArrayValue, Record, SingleValue

public abstract class ValueNode<T> extends Node implements Comparable<ValueNode<T>>, Cloneable
ValueNode is the base class for all persistent values held in fields and arrays. Values can be:
  • Single values (boolean, date, string, etc. -- FieldType)
  • Records
  • Array of single values, records, and arrays
See Also:
  • Constructor Details

    • ValueNode

      protected ValueNode()
  • Method Details

    • getType

      public abstract FieldType getType()
      Returns the FieldType of this value node. For SingleValue, this is the type of the contained value. For ArrayValue, the type is derived from its elements or its default type. For Record, the type is always FieldType.RECORD.
      Returns:
      the field type of this value node, never null
    • getParentNode

      public Node getParentNode()
      Returns the parent node that this node belongs to, or null if this is a root node.
      Specified by:
      getParentNode in class Node
      Returns:
      the parent Node that this value node belongs to, or null if this is a root node
    • setParentNode

      protected Node setParentNode(Node parentNode)
      Sets the parent node of this value node. If a parent was previously assigned and differs from the new parent, this node is first removed from the old parent. Circular references are detected and will cause a DataException.
      Parameters:
      parentNode - the new parent node, or null to detach from the current parent
      Returns:
      this value node
    • isValueNode

      public boolean isValueNode()
      Returns true if this node is a ValueNode (Record, ArrayValue, or SingleValue).

      Always returns true for all ValueNode subclasses (SingleValue, Record, and ArrayValue).

      Overrides:
      isValueNode in class Node
      Returns:
      true
    • isArray

      public boolean isArray()
      Returns true if this value node is an ArrayValue.
      Returns:
      true if this is an array, false otherwise
      See Also:
    • isNotArray

      public boolean isNotArray()
      Returns true if this value node is not an ArrayValue.
      Returns:
      true if this is not an array, false otherwise
      See Also:
    • isRecord

      public boolean isRecord()
      Returns true if this value node is a Record.
      Returns:
      true if this is a record, false otherwise
      See Also:
    • isNotRecord

      public boolean isNotRecord()
      Returns true if this value node is not a Record.
      Returns:
      true if this is not a record, false otherwise
      See Also:
    • isSingleValue

      public boolean isSingleValue()
      Returns true if this value node is a SingleValue.
      Returns:
      true if this is a single value, false otherwise
      See Also:
    • isNotSingleValue

      public boolean isNotSingleValue()
      Returns true if this value node is not a SingleValue.
      Returns:
      true if this is not a single value, false otherwise
      See Also:
    • clone

      public abstract ValueNode<T> clone()
      Returns a deep copy of this value node.
      Overrides:
      clone in class Object
      Returns:
      a clone of this value node
    • getValue

      public abstract T getValue()
      Returns the underlying value held by this node. For SingleValue, this is the wrapped object. For ArrayValue, this returns the array itself. For Record, this returns the record itself.
      Returns:
      the value, or null if this node holds a null value
    • getValueAsString

      public abstract String getValueAsString()
      Returns a string representation of the value held by this node.
      Returns:
      the value as a string, or "null" if the value is null
    • isNull

      public boolean isNull()
      Returns true if this node contains a null value. In concrete terms, only Field and SingleValue can contain nulls while not being null themselves. Both Record and ArrayValue will return false even if they have no values or null only values.

      Returns true if getValue() returns null.

      Specified by:
      isNull in class Node
    • isNotNull

      public boolean isNotNull()
      Returns true if this node does not contain a null value. In concrete terms, only Field and SingleValue can contain nulls while not being null themselves. Both Record and ArrayValue will return true even if they have no values or null only values.

      Returns true if getValue() does not return null.

      Specified by:
      isNotNull in class Node
    • getSizeInBytes

      public abstract long getSizeInBytes()
      Returns the estimated size of this value node in bytes, including object overhead.
      Returns:
      the size in bytes
    • nullValue

      public static SingleValue nullValue()
      Creates a new SingleValue containing a null value with an FieldType.UNDEFINED type.
      Returns:
      a new null-valued SingleValue
    • nullValue

      public static SingleValue nullValue(FieldType type)
      Creates a new SingleValue containing a null value with the specified FieldType.
      Parameters:
      type - the field type to assign to the null value
      Returns:
      a new null-valued SingleValue of the given type
    • from

      public static ValueNode<?> from(Object value)
      Converts the given object to the appropriate ValueNode subtype. The conversion rules are:
      Parameters:
      value - the object to convert, may be null
      Returns:
      the corresponding ValueNode, never null
      See Also:
    • from

      public static ValueNode<?> from(FieldType type, Object value)
      Converts the given object to the appropriate ValueNode subtype with an explicit FieldType hint. The hint can be null to rely on automatic type detection. The conversion rules are:
      • null returns a null-valued SingleValue of the given type
      • ValueNode instances are returned as-is
      • Iterable instances are wrapped in an ArrayValue with the given default type
      • Java arrays (except byte[]) are wrapped in an ArrayValue with the given default type
      • Moment, InputStream, Reader, LocalDateTime, LocalDate, LocalTime, and Instant values are converted before being wrapped in a SingleValue
      • All other objects are wrapped in a SingleValue
      Parameters:
      type - the explicit field type hint, or null to infer the type
      value - the object to convert, may be null
      Returns:
      the corresponding ValueNode, never null
      Throws:
      DataException - if the value cannot be converted
    • asArray

      public static ArrayValue asArray(FieldType type, Object value)
      Wraps the given value as an ArrayValue. If the value is already an ArrayValue, it is returned as-is. If it is a different ValueNode, its underlying value is extracted first. A null value produces a single-element array containing a typed null.
      Parameters:
      type - the default field type for the resulting array
      value - the value to wrap, may be null
      Returns:
      an ArrayValue containing the given value
    • asSingleValue

      public SingleValue asSingleValue()
      Casts this value node to a SingleValue. This is a convenience method that avoids explicit casting in client code.
      Returns:
      this value node cast to SingleValue
      Throws:
      ClassCastException - if this value node is not a SingleValue
      See Also:
    • asRecord

      public Record asRecord()
      Casts this value node to a Record. This is a convenience method that avoids explicit casting in client code.
      Returns:
      this value node cast to Record
      Throws:
      ClassCastException - if this value node is not a Record
      See Also:
    • asArrayValue

      public ArrayValue asArrayValue()
      Casts this value node to an ArrayValue. This is a convenience method that avoids explicit casting in client code.
      Returns:
      this value node cast to ArrayValue
      Throws:
      ClassCastException - if this value node is not an ArrayValue
      See Also:
    • toJson

      public abstract String toJson()
      Serializes this value node to a JSON string.
      Returns:
      the JSON representation
    • toXml

      public abstract String toXml()
      Serializes this value node to an XML string.
      Returns:
      the XML representation