Record Class LiteralNode

java.lang.Object
java.lang.Record
cloud.opencode.base.expression.ast.LiteralNode
Record Components:
value - the literal value | 字面量值
All Implemented Interfaces:
Node

public record LiteralNode(Object value) extends Record implements Node
Literal Node 字面量节点

Represents literal values: numbers, strings, booleans, null.

表示字面量值:数字、字符串、布尔值、null。

Features | 主要功能:

  • Typed factory methods for int, long, double, string, boolean, null - 为int、long、double、string、boolean、null提供类型化工厂方法
  • Constant folding optimization support - 支持常量折叠优化

Usage Examples | 使用示例:

Node num = LiteralNode.ofInt(42);
Node str = LiteralNode.ofString("hello");
Node nil = LiteralNode.ofNull();
Object result = num.evaluate(ctx);  // 42

Security | 安全性:

  • Thread-safe: Yes, immutable record - 线程安全: 是,不可变记录
  • Null-safe: Yes, null value is a valid literal - 空值安全: 是,null值是合法字面量
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • LiteralNode

      public LiteralNode(Object value)
      Creates an instance of a LiteralNode record class.
      Parameters:
      value - the value for the value record component
  • Method Details

    • of

      public static LiteralNode of(Object value)
      Create literal node with any value 创建任意值的字面量节点
      Parameters:
      value - the literal value | 字面量值
      Returns:
      the literal node | 字面量节点
    • ofNull

      public static LiteralNode ofNull()
      Create null literal 创建 null 字面量
      Returns:
      the null literal node | null 字面量节点
    • ofBoolean

      public static LiteralNode ofBoolean(boolean value)
      Create boolean literal 创建布尔字面量
      Parameters:
      value - the boolean value | 布尔值
      Returns:
      the boolean literal node | 布尔字面量节点
    • ofInt

      public static LiteralNode ofInt(int value)
      Create integer literal 创建整数字面量
      Parameters:
      value - the integer value | 整数值
      Returns:
      the integer literal node | 整数字面量节点
    • ofLong

      public static LiteralNode ofLong(long value)
      Create long literal 创建长整数字面量
      Parameters:
      value - the long value | 长整数值
      Returns:
      the long literal node | 长整数字面量节点
    • ofDouble

      public static LiteralNode ofDouble(double value)
      Create double literal 创建双精度字面量
      Parameters:
      value - the double value | 双精度值
      Returns:
      the double literal node | 双精度字面量节点
    • ofString

      public static LiteralNode ofString(String value)
      Create string literal 创建字符串字面量
      Parameters:
      value - the string value | 字符串值
      Returns:
      the string literal node | 字符串字面量节点
    • evaluate

      public Object evaluate(EvaluationContext context)
      Description copied from interface: Node
      Evaluate this node 求值此节点
      Specified by:
      evaluate in interface Node
      Parameters:
      context - the evaluation context | 求值上下文
      Returns:
      the evaluation result | 求值结果
    • toExpressionString

      public String toExpressionString()
      Description copied from interface: Node
      Get string representation for debugging 获取用于调试的字符串表示
      Specified by:
      toExpressionString in interface Node
      Returns:
      the string representation | 字符串表示
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • value

      public Object value()
      Returns the value of the value record component.
      Returns:
      the value of the value record component