Class SingleValue

All Implemented Interfaces:
Session, Serializable, Cloneable, Comparable<ValueNode<Object>>

public final class SingleValue extends ValueNode<Object>
SingleValue is an immutable ValueNode that holds a single scalar value (or null). The value's type is tracked by a FieldType that is either explicitly specified or inferred from the value's Java class at construction time.
See Also:
  • Constructor Details

    • SingleValue

      protected SingleValue(FieldType type, Object value)
      Creates a value of the given type; a null type is inferred from the value's class (FieldType.UNDEFINED if unknown or null).
    • SingleValue

      protected SingleValue(Object value)
      Creates a value whose type is inferred from its class (FieldType.UNDEFINED if unknown or null).
  • Method Details

    • getType

      public 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.
      Specified by:
      getType in class ValueNode<Object>
      Returns:
      the field type of the contained value
    • getNodeType

      public Node.NodeType getNodeType()
      Returns the concrete Node.NodeType of this node.
      Specified by:
      getNodeType in class Node
      Returns:
      Node.NodeType.SINGLE_VALUE
    • hasChildNodes

      public boolean hasChildNodes()
      Returns true if this node contains any sub nodes.

      Always returns false since a single value has no child nodes.

      Specified by:
      hasChildNodes in class Node
    • hasChildRecords

      public boolean hasChildRecords()
      Returns true if this node contains any descendant, record nodes.

      Always returns false since a single value cannot contain records.

      Specified by:
      hasChildRecords in class Node
    • removeChildNode

      protected void removeChildNode(Node child)
      Description copied from class: Node
      Detaches the given child (matched by identity) from this node; called when the child is moved to another parent.
      Specified by:
      removeChildNode in class Node
    • isArray

      public boolean isArray()
      Returns true if this value node is an ArrayValue.

      Always returns false.

      Overrides:
      isArray in class ValueNode<Object>
      Returns:
      true if this is an array, false otherwise
      See Also:
    • isRecord

      public boolean isRecord()
      Returns true if this value node is a Record.

      Always returns false.

      Overrides:
      isRecord in class ValueNode<Object>
      Returns:
      true if this is a record, false otherwise
      See Also:
    • isSingleValue

      public boolean isSingleValue()
      Returns true if this value node is a SingleValue.

      Always returns true.

      Overrides:
      isSingleValue in class ValueNode<Object>
      Returns:
      true if this is a single value, false otherwise
      See Also:
    • getValue

      public Object 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.
      Specified by:
      getValue in class ValueNode<Object>
      Returns:
      the raw value held by this single value, or null
    • getValueAsString

      public String getValueAsString()
      Returns a string representation of the value held by this node.
      Specified by:
      getValueAsString in class ValueNode<Object>
      Returns:
      the value formatted as a string using the rules of its FieldType
    • getValueAsDatetime

      public Date getValueAsDatetime()
      Returns the value as a Date (datetime).
      Returns:
      the datetime value
      Throws:
      ClassCastException - if the value is not a Date
    • getValueAsDate

      public Date getValueAsDate()
      Returns the value as a Date. If the underlying type is FieldType.DATETIME, the value is converted; otherwise the value is cast directly.
      Returns:
      the date value, or null if the value is null
      Throws:
      ClassCastException - if the value cannot be converted to a Date
    • getValueAsTime

      public Time getValueAsTime()
      Returns the value as a Time. If the underlying type is FieldType.DATETIME, the value is converted; otherwise the value is cast directly.
      Returns:
      the time value, or null if the value is null
      Throws:
      ClassCastException - if the value cannot be converted to a Time
    • getValueAsInteger

      public int getValueAsInteger()
      Returns the value as an int.
      Returns:
      the integer value
      Throws:
      ClassCastException - if the value is not a Number
      NullPointerException - if the value is null
    • getValueAsLong

      public long getValueAsLong()
      Returns the value as a long.
      Returns:
      the long value
      Throws:
      ClassCastException - if the value is not a Number
      NullPointerException - if the value is null
    • getValueAsShort

      public short getValueAsShort()
      Returns the value as a short.
      Returns:
      the short value
      Throws:
      ClassCastException - if the value is not a Number
      NullPointerException - if the value is null
    • getValueAsByte

      public byte getValueAsByte()
      Returns the value as a byte.
      Returns:
      the byte value
      Throws:
      ClassCastException - if the value is not a Number
      NullPointerException - if the value is null
    • getValueAsBoolean

      public boolean getValueAsBoolean()
      Returns the value as a boolean.
      Returns:
      the boolean value
      Throws:
      ClassCastException - if the value is not a Boolean
      NullPointerException - if the value is null
    • getValueAsChar

      public char getValueAsChar()
      Returns the value as a char.
      Returns:
      the character value
      Throws:
      NullPointerException - if the value is null
    • getValueAsDouble

      public double getValueAsDouble()
      Returns the value as a double.
      Returns:
      the double value
      Throws:
      ClassCastException - if the value is not a Number
      NullPointerException - if the value is null
    • getValueAsFloat

      public float getValueAsFloat()
      Returns the value as a float.
      Returns:
      the float value
      Throws:
      ClassCastException - if the value is not a Number
      NullPointerException - if the value is null
    • getValueAsBytes

      public byte[] getValueAsBytes()
      Returns the value as a byte[].
      Returns:
      the byte array value
      Throws:
      ClassCastException - if the value is not a byte[]
    • getValueAsBigDecimal

      public BigDecimal getValueAsBigDecimal()
      Returns the value as a BigDecimal.
      Returns:
      the big decimal value
      Throws:
      ClassCastException - if the value is not a Number
    • getValueAsBigInteger

      public BigInteger getValueAsBigInteger()
      Returns the value as a BigInteger.
      Returns:
      the big integer value
      Throws:
      ClassCastException - if the value is not a Number
    • getValueAsInstant

      public Instant getValueAsInstant()
      Returns the value as an Instant.
      Returns:
      the instant value, or null if the value is null
    • getValueAsLocalDateTime

      public LocalDateTime getValueAsLocalDateTime()
      Returns the value as a LocalDateTime using the system default time zone.
      Returns:
      the local date-time value, or null if the value is null
    • getValueAsLocalDate

      public LocalDate getValueAsLocalDate()
      Returns the value as a LocalDate.
      Returns:
      the local date value, or null if the value is null
    • getValueAsLocalTime

      public LocalTime getValueAsLocalTime()
      Returns the value as a LocalTime.
      Returns:
      the local time value, or null if the value is null
    • getValueAsUuid

      public UUID getValueAsUuid()
      Returns the value as a UUID.
      Returns:
      the UUID value, or null if the value is null
      Throws:
      ClassCastException - if the value is not a UUID
    • getSizeInBytes

      public long getSizeInBytes()
      Returns the estimated size of this value node in bytes, including object overhead.
      Specified by:
      getSizeInBytes in class ValueNode<Object>
      Returns:
      the size in bytes
    • compareTo

      public int compareTo(ValueNode<Object> o)
      Compares this single value to another by their underlying values using natural ordering.
      Parameters:
      o - the other value node to compare to
      Returns:
      a negative integer, zero, or a positive integer as this value is less than, equal to, or greater than the other
    • compareTo

      public int compareTo(ValueNode<Object> o, Collator collator)
      Compares this single value to another by their underlying values, optionally using the specified Collator for string comparisons.
      Parameters:
      o - the other value node to compare to
      collator - the collator to use for string comparisons, or null for natural ordering
      Returns:
      a negative integer, zero, or a positive integer as this value is less than, equal to, or greater than the other
    • equals

      public boolean equals(Object o)
      Indicates whether the given object is a SingleValue with an equal underlying value.
      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with
      Returns:
      true if the objects are equal
    • hashCode

      public int hashCode()
      Returns a hash code based on the underlying value.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code
    • clone

      public SingleValue clone()
      Returns a deep copy of this single value.
      Specified by:
      clone in class ValueNode<Object>
      Returns:
      a new SingleValue with the same type and value
    • toJson

      public String toJson()
      Description copied from class: ValueNode
      Serializes this value node to a JSON string.
      Specified by:
      toJson in class ValueNode<Object>
      Returns:
      the JSON representation
    • toXml

      public String toXml()
      Description copied from class: ValueNode
      Serializes this value node to an XML string.
      Specified by:
      toXml in class ValueNode<Object>
      Returns:
      the XML representation
    • toString

      public String toString()
      Returns a string representation of this value, truncated to Field.MAX_DISPLAY_STRING characters if necessary.
      Specified by:
      toString in class Node
      Returns:
      the string representation