Class PathFinder

java.lang.Object
cloud.opencode.base.tree.path.PathFinder

public final class PathFinder extends Object
Path Finder 路径查找器

Finds paths in tree structures.

在树结构中查找路径。

Features | 主要功能:

  • Find path to node by ID or predicate - 通过ID或谓词查找节点路径
  • Find all paths to matching nodes - 查找到所有匹配节点的路径
  • Find paths to leaf nodes - 查找到叶子节点的路径
  • Get ancestor IDs and node depth - 获取祖先ID和节点深度

Usage Examples | 使用示例:

Optional<TreePath<MyNode>> path = PathFinder.findPathById(roots, targetId);
List<ID> ancestors = PathFinder.getAncestorIds(roots, targetId);
int depth = PathFinder.getDepth(roots, targetId);

Security | 安全性:

  • Thread-safe: No - 否
  • Null-safe: No (roots and targetId must not be null) - 否(根节点和目标ID不能为null)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • findPathById

      public static <T extends Treeable<T,ID>, ID> Optional<TreePath<T>> findPathById(List<T> roots, ID targetId)
      Find path to node by ID 通过ID查找到节点的路径
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      targetId - the target ID | 目标ID
      Returns:
      the path if found | 如果找到返回路径
    • findPath

      public static <T extends Treeable<T,ID>, ID> Optional<TreePath<T>> findPath(List<T> roots, Predicate<T> predicate)
      Find path to node matching predicate 查找到匹配谓词的节点的路径
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the target predicate | 目标谓词
      Returns:
      the path if found | 如果找到返回路径
    • findAllPaths

      public static <T extends Treeable<T,ID>, ID> List<TreePath<T>> findAllPaths(List<T> roots, Predicate<T> predicate)
      Find all paths to matching nodes 查找到所有匹配节点的路径
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the target predicate | 目标谓词
      Returns:
      the paths | 路径列表
    • findAllLeafPaths

      public static <T extends Treeable<T,ID>, ID> List<TreePath<T>> findAllLeafPaths(List<T> roots)
      Find paths to all leaf nodes 查找到所有叶子节点的路径
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the paths to leaves | 到叶子节点的路径
    • getAncestorIds

      public static <T extends Treeable<T,ID>, ID> List<ID> getAncestorIds(List<T> roots, ID targetId)
      Get ancestor IDs for a node 获取节点的祖先ID列表
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      targetId - the target ID | 目标ID
      Returns:
      the ancestor IDs | 祖先ID列表
    • getDepth

      public static <T extends Treeable<T,ID>, ID> int getDepth(List<T> roots, ID targetId)
      Get depth of node 获取节点深度
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      targetId - the target ID | 目标ID
      Returns:
      the depth (-1 if not found) | 深度(未找到返回-1)