Class CycleDetector

java.lang.Object
cloud.opencode.base.tree.validation.CycleDetector

public final class CycleDetector extends Object
Cycle Detector 循环检测器

Detects cycles in tree or graph structures.

检测树或图结构中的循环。

Features | 主要功能:

  • Cycle detection in tree structures - 树结构中的循环检测
  • Cycle path extraction - 循环路径提取
  • Flat list cycle detection - 扁平列表循环检测

Usage Examples | 使用示例:

boolean hasCycle = CycleDetector.hasCycle(roots);
Optional<List<ID>> path = CycleDetector.findCyclePath(roots);
CycleDetector.checkNoCycle(roots); // throws CycleDetectedException

Security | 安全性:

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

    • hasCycle

      public static <T extends Treeable<T,ID>, ID> boolean hasCycle(List<T> roots)
      Check if tree has cycles 检查树是否有循环
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点
      Returns:
      true if has cycle | 如果有循环返回true
    • findCyclePath

      public static <T extends Treeable<T,ID>, ID> Optional<List<ID>> findCyclePath(List<T> roots)
      Detect cycle and return path 检测循环并返回路径
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点
      Returns:
      the cycle path if found | 如果找到返回循环路径
    • checkNoCycle

      public static <T extends Treeable<T,ID>, ID> void checkNoCycle(List<T> roots)
      Check for cycle and throw if found 检查循环,如果发现则抛出异常
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点
      Throws:
      CycleDetectedException - if cycle is found | 如果发现循环抛出异常
    • hasPotentialCycle

      public static <T extends Treeable<T,ID>, ID> boolean hasPotentialCycle(List<T> nodes)
      Check flat list for potential cycles 检查扁平列表中的潜在循环
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      nodes - the nodes to check | 要检查的节点
      Returns:
      true if potential cycle exists | 如果存在潜在循环返回true