Class DefaultTreeNode<ID>

java.lang.Object
cloud.opencode.base.tree.DefaultTreeNode<ID>
Type Parameters:
ID - the ID type | ID类型
All Implemented Interfaces:
Treeable<DefaultTreeNode<ID>, ID>

public class DefaultTreeNode<ID> extends Object implements Treeable<DefaultTreeNode<ID>, ID>
Default Tree Node 默认树节点

Default implementation of Treeable interface.

Treeable接口的默认实现。

Features | 主要功能:

  • ID-based parent-child relationships - 基于ID的父子关系
  • Extra attributes via Map - 通过Map存储额外属性
  • Sortable with sort field - 可通过sort字段排序
  • Root/leaf detection - 根节点/叶子节点检测

Usage Examples | 使用示例:

// Create node - 创建节点
DefaultTreeNode<Long> node = new DefaultTreeNode<>(1L, 0L, "Root");
node.put("key", "value");

// Add child - 添加子节点
node.addChild(new DefaultTreeNode<>(2L, 1L, "Child"));

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Partial (setExtra and setChildren handle null) - 空值安全: 部分(setExtra和setChildren处理null)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • DefaultTreeNode

      public DefaultTreeNode()
      Create empty node 创建空节点
    • DefaultTreeNode

      public DefaultTreeNode(ID id)
      Create node with ID 使用ID创建节点
      Parameters:
      id - the ID | ID
    • DefaultTreeNode

      public DefaultTreeNode(ID id, ID parentId, String name)
      Create node with ID, parentId and name 使用ID、父ID和名称创建节点
      Parameters:
      id - the ID | ID
      parentId - the parent ID | 父ID
      name - the name | 名称
  • Method Details

    • getId

      public ID getId()
      Description copied from interface: Treeable
      Get the node ID 获取节点ID
      Specified by:
      getId in interface Treeable<DefaultTreeNode<ID>, ID>
      Returns:
      the ID | ID
    • setId

      public void setId(ID id)
    • getParentId

      public ID getParentId()
      Description copied from interface: Treeable
      Get the parent node ID 获取父节点ID
      Specified by:
      getParentId in interface Treeable<DefaultTreeNode<ID>, ID>
      Returns:
      the parent ID | 父节点ID
    • setParentId

      public void setParentId(ID parentId)
    • getName

      public String getName()
    • setName

      public void setName(String name)
    • getSort

      public int getSort()
    • setSort

      public void setSort(int sort)
    • getExtra

      public Map<String,Object> getExtra()
    • setExtra

      public void setExtra(Map<String,Object> extra)
    • getChildren

      public List<DefaultTreeNode<ID>> getChildren()
      Description copied from interface: Treeable
      Get the children list 获取子节点列表
      Specified by:
      getChildren in interface Treeable<DefaultTreeNode<ID>, ID>
      Returns:
      the children | 子节点列表
    • setChildren

      public void setChildren(List<DefaultTreeNode<ID>> children)
      Description copied from interface: Treeable
      Set the children list 设置子节点列表
      Specified by:
      setChildren in interface Treeable<DefaultTreeNode<ID>, ID>
      Parameters:
      children - the children | 子节点列表
    • put

      public DefaultTreeNode<ID> put(String key, Object value)
      Put extra attribute 设置额外属性
      Parameters:
      key - the key | 键
      value - the value | 值
      Returns:
      this node | 此节点
    • get

      public <V> V get(String key)
      Get extra attribute 获取额外属性
      Type Parameters:
      V - the value type | 值类型
      Parameters:
      key - the key | 键
      Returns:
      the value | 值
    • get

      public <V> V get(String key, V defaultValue)
      Get extra attribute with default 获取额外属性(带默认值)
      Type Parameters:
      V - the value type | 值类型
      Parameters:
      key - the key | 键
      defaultValue - the default value | 默认值
      Returns:
      the value or default | 值或默认值
    • 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
    • addChild

      public DefaultTreeNode<ID> addChild(DefaultTreeNode<ID> child)
      Add child node 添加子节点
      Parameters:
      child - the child node | 子节点
      Returns:
      this node | 此节点
    • toString

      public String toString()
      Overrides:
      toString in class Object