Class VirtualTree<T,ID>

java.lang.Object
cloud.opencode.base.tree.virtual.VirtualTree<T,ID>
Type Parameters:
T - the data type | 数据类型
ID - the ID type | ID类型
All Implemented Interfaces:
Treeable<VirtualTree<T,ID>, ID>

public class VirtualTree<T,ID> extends Object implements Treeable<VirtualTree<T,ID>, ID>
Virtual Tree 虚拟化树

A tree implementation with lazy loading support for large datasets. Children are loaded on-demand when accessed, reducing initial memory footprint.

支持懒加载的树实现,适用于大数据量场景。 子节点在访问时按需加载,减少初始内存占用。

Key Features | 主要功能:

  • Lazy loading - Children loaded on first access - 懒加载
  • Caching - Loaded nodes are cached - 缓存已加载节点
  • Thread-safe - Concurrent access support - 线程安全
  • Memory efficient - LRU eviction for large trees - 内存高效
  • Pagination - Support for paginated child loading - 分页加载

Usage Examples | 使用示例:

VirtualTree<String, Long> tree = VirtualTree.root(1L, "Root",
    parentId -> loadChildrenFromDB(parentId));

// Children loaded lazily on access
List<VirtualTree<String, Long>> children = tree.getChildren();

// Preload to depth
tree.preload(3);
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • VirtualTree

      public VirtualTree(ID id, ID parentId, T data, LazyChildLoader<VirtualTree<T,ID>, ID> childLoader)
      Create virtual tree node with lazy loading 创建支持懒加载的虚拟树节点
      Parameters:
      id - the node ID | 节点ID
      parentId - the parent ID | 父节点ID
      data - the node data | 节点数据
      childLoader - the lazy child loader | 懒加载器
    • VirtualTree

      public VirtualTree(ID id, ID parentId, T data, LazyChildLoader<VirtualTree<T,ID>, ID> childLoader, Map<ID, VirtualTree<T,ID>> nodeCache, int maxCacheSize, boolean cacheEnabled)
      Create virtual tree node with configuration 创建可配置的虚拟树节点
      Parameters:
      id - the node ID | 节点ID
      parentId - the parent ID | 父节点ID
      data - the node data | 节点数据
      childLoader - the lazy child loader | 懒加载器
      nodeCache - shared node cache | 共享节点缓存
      maxCacheSize - maximum cache size | 最大缓存大小
      cacheEnabled - whether caching is enabled | 是否启用缓存
  • Method Details

    • root

      public static <T,ID> VirtualTree<T,ID> root(ID id, T data, LazyChildLoader<VirtualTree<T,ID>, ID> childLoader)
      Create a root virtual tree node 创建根虚拟树节点
      Type Parameters:
      T - the data type | 数据类型
      ID - the ID type | ID类型
      Parameters:
      id - the node ID | 节点ID
      data - the node data | 节点数据
      childLoader - the lazy child loader | 懒加载器
      Returns:
      the virtual tree node | 虚拟树节点
    • builder

      public static <T,ID> VirtualTree.Builder<T,ID> builder()
      Create a virtual tree node with builder 使用构建器创建虚拟树节点
      Type Parameters:
      T - the data type | 数据类型
      ID - the ID type | ID类型
      Returns:
      the builder | 构建器
    • getId

      public ID getId()
      Description copied from interface: Treeable
      Get the node ID 获取节点ID
      Specified by:
      getId in interface Treeable<T,ID>
      Returns:
      the ID | ID
    • getParentId

      public ID getParentId()
      Description copied from interface: Treeable
      Get the parent node ID 获取父节点ID
      Specified by:
      getParentId in interface Treeable<T,ID>
      Returns:
      the parent ID | 父节点ID
    • getChildren

      public List<VirtualTree<T,ID>> getChildren()
      Description copied from interface: Treeable
      Get the children list 获取子节点列表
      Specified by:
      getChildren in interface Treeable<T,ID>
      Returns:
      the children | 子节点列表
    • setChildren

      public void setChildren(List<VirtualTree<T,ID>> children)
      Description copied from interface: Treeable
      Set the children list 设置子节点列表
      Specified by:
      setChildren in interface Treeable<T,ID>
      Parameters:
      children - the children | 子节点列表
    • isChildrenLoaded

      public boolean isChildrenLoaded()
      Check if children are loaded 检查子节点是否已加载
      Returns:
      true if loaded | 如果已加载返回true
    • reloadChildren

      public void reloadChildren()
      Reload children (force refresh) 重新加载子节点(强制刷新)
    • preload

      public void preload(int depth)
      Preload children to specified depth 预加载到指定深度的子节点
      Parameters:
      depth - the depth to preload | 预加载深度
    • unloadChildren

      public void unloadChildren()
      Unload children to free memory 卸载子节点以释放内存
    • getData

      public T getData()
      Get the node data 获取节点数据
      Returns:
      the data | 数据
    • isRoot

      public boolean isRoot()
      Check if this is a root node 检查是否为根节点
      Returns:
      true if root | 如果是根节点返回true
    • isLeaf

      public boolean isLeaf()
      Check if this is a leaf node 检查是否为叶子节点
      Returns:
      true if leaf | 如果是叶子节点返回true
    • getChildCount

      public int getChildCount()
      Get child count without loading all children 获取子节点数量(不加载所有子节点)
      Returns:
      the child count | 子节点数量
    • findInCache

      public Optional<VirtualTree<T,ID>> findInCache(ID nodeId)
      Find node by ID in cache 从缓存中查找节点
      Parameters:
      nodeId - the node ID | 节点ID
      Returns:
      the node if found | 找到的节点
    • find

      public Optional<VirtualTree<T,ID>> find(ID nodeId)
      Find node by ID (searches tree recursively) 通过ID查找节点(递归搜索树)
      Parameters:
      nodeId - the node ID | 节点ID
      Returns:
      the node if found | 找到的节点
    • findAll

      public List<VirtualTree<T,ID>> findAll(Predicate<T> predicate)
      Find all nodes matching predicate 查找所有匹配谓词的节点
      Parameters:
      predicate - the predicate | 谓词
      Returns:
      the matching nodes | 匹配的节点
    • traversePreOrder

      public void traversePreOrder(Consumer<VirtualTree<T,ID>> visitor)
      Traverse in pre-order 前序遍历
      Parameters:
      visitor - the visitor | 访问者
    • traversePostOrder

      public void traversePostOrder(Consumer<VirtualTree<T,ID>> visitor)
      Traverse in post-order 后序遍历
      Parameters:
      visitor - the visitor | 访问者
    • traverseBreadthFirst

      public void traverseBreadthFirst(Consumer<VirtualTree<T,ID>> visitor)
      Traverse level by level (breadth-first) 层级遍历(广度优先)
      Parameters:
      visitor - the visitor | 访问者
    • traverseWithDepthLimit

      public void traverseWithDepthLimit(Consumer<VirtualTree<T,ID>> visitor, int maxDepth)
      Traverse with depth limit (for controlled lazy loading) 带深度限制的遍历(用于控制懒加载)
      Parameters:
      visitor - the visitor | 访问者
      maxDepth - the maximum depth | 最大深度
    • getCacheStats

      public VirtualTree.CacheStats getCacheStats()
      Get cache statistics 获取缓存统计
      Returns:
      the statistics | 统计信息
    • clearCache

      public void clearCache()
      Clear cache 清除缓存
    • getLoadedNodeCount

      public int getLoadedNodeCount()
      Get total node count (loaded nodes only) 获取总节点数(仅已加载的节点)
      Returns:
      the count | 数量
    • toString

      public String toString()
      Overrides:
      toString in class Object