Record Class LightTreeNode<ID>

java.lang.Object
java.lang.Record
cloud.opencode.base.tree.LightTreeNode<ID>
Type Parameters:
ID - the ID type | ID类型
Record Components:
id - the node ID | 节点ID
parentId - the parent ID | 父节点ID
name - the node name | 节点名称
children - the children list | 子节点列表

public record LightTreeNode<ID>(ID id, ID parentId, String name, List<LightTreeNode<ID>> children) extends Record
Light Tree Node 轻量级树节点

A lightweight tree node implementation using record.

使用record实现的轻量级树节点。

Features | 主要功能:

  • Immutable record-based tree node - 基于record的不可变树节点
  • Minimal memory footprint - 最小内存占用
  • Factory methods for root and child nodes - 根节点和子节点的工厂方法
  • Functional child addition via withChild - 通过withChild函数式添加子节点

Usage Examples | 使用示例:

// Create root - 创建根节点
LightTreeNode<Long> root = LightTreeNode.root(1L, "Root");

// Create child - 创建子节点
LightTreeNode<Long> child = LightTreeNode.of(2L, 1L, "Child");

// Add child (returns new node) - 添加子节点(返回新节点)
LightTreeNode<Long> updated = root.withChild(child);

Security | 安全性:

  • Thread-safe: Yes (immutable record, withChild returns new instance) - 线程安全: 是(不可变record,withChild返回新实例)
  • Null-safe: Partial (children list defaults to empty if null) - 空值安全: 部分(子节点列表null时默认为空)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • LightTreeNode

      public LightTreeNode(ID id, ID parentId, String name, List<LightTreeNode<ID>> children)
      Compact constructor 紧凑构造函数
  • Method Details

    • of

      public static <ID> LightTreeNode<ID> of(ID id, ID parentId, String name)
      Create node 创建节点
      Type Parameters:
      ID - the ID type | ID类型
      Parameters:
      id - the ID | ID
      parentId - the parent ID | 父节点ID
      name - the name | 名称
      Returns:
      the node | 节点
    • root

      public static <ID> LightTreeNode<ID> root(ID id, String name)
      Create root node 创建根节点
      Type Parameters:
      ID - the ID type | ID类型
      Parameters:
      id - the ID | ID
      name - the name | 名称
      Returns:
      the node | 节点
    • isRoot

      public boolean isRoot()
      Check if node is root 检查是否为根节点
      Returns:
      true if root | 如果是根节点返回true
    • isLeaf

      public boolean isLeaf()
      Check if node is leaf 检查是否为叶子节点
      Returns:
      true if leaf | 如果是叶子节点返回true
    • withChild

      public LightTreeNode<ID> withChild(LightTreeNode<ID> child)
      Add child and return new node 添加子节点并返回新节点
      Parameters:
      child - the child to add | 要添加的子节点
      Returns:
      new node with child | 带子节点的新节点
    • 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.
    • id

      public ID id()
      Returns the value of the id record component.
      Returns:
      the value of the id record component
    • parentId

      public ID parentId()
      Returns the value of the parentId record component.
      Returns:
      the value of the parentId record component
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • children

      public List<LightTreeNode<ID>> children()
      Returns the value of the children record component.
      Returns:
      the value of the children record component