Class TreeMapper

java.lang.Object
cloud.opencode.base.tree.operation.TreeMapper

public final class TreeMapper extends Object
Tree Mapper - Maps tree nodes to different types 树映射器 - 将树节点映射为不同类型

Maps tree nodes to different types while preserving tree structure.

将树节点映射为不同类型同时保持树结构。

Features | 主要功能:

  • Map tree nodes to different types - 将树节点映射为不同类型
  • Custom children setter support - 自定义子节点设置器支持
  • Value extraction from tree - 从树中提取值
  • Max depth protection (1000) - 最大深度保护(1000)

Usage Examples | 使用示例:

// Map nodes to DTOs
List<NodeDTO> dtos = TreeMapper.map(roots, node -> new NodeDTO(node));

// Extract all names
List<String> names = TreeMapper.extractAll(roots, MyNode::getName);

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

    • map

      public static <S extends Treeable<S,ID>, T extends Treeable<T,ID>, ID> List<T> map(List<S> roots, Function<S,T> mapper)
      Map tree nodes to different type 将树节点映射为不同类型
      Type Parameters:
      S - the source type | 源类型
      T - the target type | 目标类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      mapper - the mapper function | 映射函数
      Returns:
      the mapped roots | 映射后的根节点
      Throws:
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000
    • mapToAny

      public static <S extends Treeable<S,ID>, T, ID> List<T> mapToAny(List<S> roots, Function<S,T> nodeMapper, BiConsumer<T,List<T>> childrenSetter)
      Map tree to different structure with custom children mapper 使用自定义子节点映射器将树映射为不同结构
      Type Parameters:
      S - the source type | 源类型
      T - the target type | 目标类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      nodeMapper - the node mapper | 节点映射器
      childrenSetter - the children setter | 子节点设置器
      Returns:
      the mapped list | 映射后的列表
      Throws:
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000
    • extractAll

      public static <T extends Treeable<T,ID>, ID, R> List<R> extractAll(List<T> roots, Function<T,R> extractor)
      Extract values from tree 从树中提取值
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      R - the result type | 结果类型
      Parameters:
      roots - the root nodes | 根节点列表
      extractor - the value extractor | 值提取器
      Returns:
      the extracted values | 提取的值
      Throws:
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000