Class TreePrinter

java.lang.Object
cloud.opencode.base.tree.TreePrinter

public final class TreePrinter extends Object
Tree Printer 树打印器

Prints tree structure in ASCII format.

以ASCII格式打印树结构。

Features | 主要功能:

  • ASCII tree visualization with box-drawing characters - 使用制表符的ASCII树可视化
  • Custom node formatter support - 自定义节点格式化器支持
  • Simple indented format option - 简单缩进格式选项
  • Tree statistics output - 树统计输出
  • Console and stream output - 控制台和流输出

Usage Examples | 使用示例:

// Print tree to string - 打印树为字符串
String output = TreePrinter.print(roots);

// Print with custom formatter - 使用自定义格式化器打印
String output = TreePrinter.print(roots, node -> node.getName());

// Print to console - 打印到控制台
TreePrinter.printToConsole(roots);

// Get tree statistics - 获取树统计
String stats = TreePrinter.getStats(roots);

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
  • Null-safe: No (null nodes or children may cause NullPointerException) - 空值安全: 否(null节点或子节点可能导致空指针异常)
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • print

      public static <T extends Treeable<T,ID>, ID> String print(List<T> roots)
      Print tree to string 打印树为字符串
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the string representation | 字符串表示
    • print

      public static <T extends Treeable<T,ID>, ID> String print(List<T> roots, Function<T,String> formatter)
      Print tree to string with custom formatter 使用自定义格式化器打印树为字符串
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      formatter - the node formatter | 节点格式化器
      Returns:
      the string representation | 字符串表示
    • printToConsole

      public static <T extends Treeable<T,ID>, ID> void printToConsole(List<T> roots)
      Print tree to console 打印树到控制台
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
    • printToConsole

      public static <T extends Treeable<T,ID>, ID> void printToConsole(List<T> roots, Function<T,String> formatter)
      Print tree to console with formatter 使用格式化器打印树到控制台
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      formatter - the node formatter | 节点格式化器
    • printToStream

      public static <T extends Treeable<T,ID>, ID> void printToStream(List<T> roots, PrintStream out, Function<T,String> formatter)
      Print tree to stream 打印树到流
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      out - the output stream | 输出流
      formatter - the node formatter | 节点格式化器
    • printSingle

      public static <T extends Treeable<T,ID>, ID> String printSingle(T root)
      Print single node tree 打印单节点树
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      root - the root node | 根节点
      Returns:
      the string representation | 字符串表示
    • printSimple

      public static <T extends Treeable<T,ID>, ID> String printSimple(List<T> roots, String indent, Function<T,String> formatter)
      Print simple tree (indented format) 打印简单树(缩进格式)
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      indent - the indent string | 缩进字符串
      formatter - the node formatter | 节点格式化器
      Returns:
      the string representation | 字符串表示
    • getStats

      public static <T extends Treeable<T,ID>, ID> String getStats(List<T> roots)
      Get tree statistics string 获取树统计字符串
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the statistics string | 统计字符串