Record Class TreePath<T>

java.lang.Object
java.lang.Record
cloud.opencode.base.tree.path.TreePath<T>
Type Parameters:
T - the node type | 节点类型
Record Components:
nodes - the path nodes from root to target | 从根到目标的路径节点

public record TreePath<T>(List<T> nodes) extends Record
Tree Path 树路径

Represents a path from root to a node.

表示从根到节点的路径。

Features | 主要功能:

  • Immutable path representation - 不可变路径表示
  • Root, target, and parent access - 根、目标和父节点访问
  • Sub-path and append operations - 子路径和追加操作
  • String representation with separator - 带分隔符的字符串表示

Usage Examples | 使用示例:

TreePath<MyNode> path = PathFinder.findPathById(roots, id).get();
MyNode root = path.getRoot();
MyNode target = path.getTarget();
int depth = path.length();
String display = path.toString(" / ");

Security | 安全性:

  • Thread-safe: Yes (immutable record) - 是(不可变记录)
  • Null-safe: Yes (null nodes default to empty) - 是(null节点默认为空)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    TreePath(List<T> nodes)
    Compact constructor 紧凑构造函数
  • Method Summary

    Modifier and Type
    Method
    Description
    append(T node)
    Append node to path 向路径追加节点
    boolean
    contains(T node)
    Check if path contains node 检查路径是否包含节点
    static <T> TreePath<T>
    Create empty path 创建空路径
    final boolean
    Indicates whether some other object is "equal to" this one.
    get(int index)
    Get node at index 获取指定索引的节点
    Get parent of target 获取目标的父节点
    Get root node 获取根节点
    Get target node (last node) 获取目标节点(最后一个节点)
    final int
    Returns a hash code value for this object.
    boolean
    Check if path is empty 检查路径是否为空
    int
    Get path length 获取路径长度
    Returns the value of the nodes record component.
    static <T> TreePath<T>
    of(List<T> nodes)
    Create path from list 从列表创建路径
    static <T> TreePath<T>
    of(T... nodes)
    Create path from nodes 从节点创建路径
    Get reversed path 获取反转的路径
    subPath(int start, int end)
    Get sub-path from start to end index 获取从开始到结束索引的子路径
    Returns a string representation of this record class.
    toString(String separator)
    Convert to string representation 转换为字符串表示

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TreePath

      public TreePath(List<T> nodes)
      Compact constructor 紧凑构造函数
  • Method Details

    • empty

      public static <T> TreePath<T> empty()
      Create empty path 创建空路径
      Type Parameters:
      T - the node type | 节点类型
      Returns:
      the empty path | 空路径
    • of

      @SafeVarargs public static <T> TreePath<T> of(T... nodes)
      Create path from nodes 从节点创建路径
      Type Parameters:
      T - the node type | 节点类型
      Parameters:
      nodes - the nodes | 节点
      Returns:
      the path | 路径
    • of

      public static <T> TreePath<T> of(List<T> nodes)
      Create path from list 从列表创建路径
      Type Parameters:
      T - the node type | 节点类型
      Parameters:
      nodes - the nodes | 节点
      Returns:
      the path | 路径
    • isEmpty

      public boolean isEmpty()
      Check if path is empty 检查路径是否为空
      Returns:
      true if empty | 如果为空返回true
    • length

      public int length()
      Get path length 获取路径长度
      Returns:
      the length | 长度
    • getRoot

      public T getRoot()
      Get root node 获取根节点
      Returns:
      the root or null | 根节点或null
    • getTarget

      public T getTarget()
      Get target node (last node) 获取目标节点(最后一个节点)
      Returns:
      the target or null | 目标节点或null
    • get

      public T get(int index)
      Get node at index 获取指定索引的节点
      Parameters:
      index - the index | 索引
      Returns:
      the node | 节点
    • getParent

      public T getParent()
      Get parent of target 获取目标的父节点
      Returns:
      the parent or null | 父节点或null
    • subPath

      public TreePath<T> subPath(int start, int end)
      Get sub-path from start to end index 获取从开始到结束索引的子路径
      Parameters:
      start - the start index | 开始索引
      end - the end index (exclusive) | 结束索引(不包含)
      Returns:
      the sub-path | 子路径
    • append

      public TreePath<T> append(T node)
      Append node to path 向路径追加节点
      Parameters:
      node - the node to append | 要追加的节点
      Returns:
      the new path | 新路径
    • reverse

      public TreePath<T> reverse()
      Get reversed path 获取反转的路径
      Returns:
      the reversed path | 反转的路径
    • contains

      public boolean contains(T node)
      Check if path contains node 检查路径是否包含节点
      Parameters:
      node - the node to check | 要检查的节点
      Returns:
      true if contains | 如果包含返回true
    • toString

      public String toString(String separator)
      Convert to string representation 转换为字符串表示
      Parameters:
      separator - the separator | 分隔符
      Returns:
      the string | 字符串
    • toString

      public 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.
    • nodes

      public List<T> nodes()
      Returns the value of the nodes record component.
      Returns:
      the value of the nodes record component