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>
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classVirtual Tree Builder 虚拟树构建器static final recordCache Statistics 缓存统计 -
Constructor Summary
ConstructorsConstructorDescriptionVirtualTree(ID id, ID parentId, T data, LazyChildLoader<VirtualTree<T, ID>, ID> childLoader) Create virtual tree node with lazy loading 创建支持懒加载的虚拟树节点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 创建可配置的虚拟树节点 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T,ID> VirtualTree.Builder <T, ID> builder()Create a virtual tree node with builder 使用构建器创建虚拟树节点voidClear cache 清除缓存Find node by ID (searches tree recursively) 通过ID查找节点(递归搜索树)List<VirtualTree<T, ID>> Find all nodes matching predicate 查找所有匹配谓词的节点findInCache(ID nodeId) Find node by ID in cache 从缓存中查找节点Get cache statistics 获取缓存统计intGet child count without loading all children 获取子节点数量(不加载所有子节点)List<VirtualTree<T, ID>> Get the children list 获取子节点列表getData()Get the node data 获取节点数据getId()Get the node ID 获取节点IDintGet total node count (loaded nodes only) 获取总节点数(仅已加载的节点)Get the parent node ID 获取父节点IDbooleanCheck if children are loaded 检查子节点是否已加载booleanisLeaf()Check if this is a leaf node 检查是否为叶子节点booleanisRoot()Check if this is a root node 检查是否为根节点voidpreload(int depth) Preload children to specified depth 预加载到指定深度的子节点voidReload children (force refresh) 重新加载子节点(强制刷新)static <T,ID> VirtualTree <T, ID> root(ID id, T data, LazyChildLoader<VirtualTree<T, ID>, ID> childLoader) Create a root virtual tree node 创建根虚拟树节点voidsetChildren(List<VirtualTree<T, ID>> children) Set the children list 设置子节点列表toString()voidtraverseBreadthFirst(Consumer<VirtualTree<T, ID>> visitor) Traverse level by level (breadth-first) 层级遍历(广度优先)voidtraversePostOrder(Consumer<VirtualTree<T, ID>> visitor) Traverse in post-order 后序遍历voidtraversePreOrder(Consumer<VirtualTree<T, ID>> visitor) Traverse in pre-order 前序遍历voidtraverseWithDepthLimit(Consumer<VirtualTree<T, ID>> visitor, int maxDepth) Traverse with depth limit (for controlled lazy loading) 带深度限制的遍历(用于控制懒加载)voidUnload children to free memory 卸载子节点以释放内存
-
Constructor Details
-
VirtualTree
Create virtual tree node with lazy loading 创建支持懒加载的虚拟树节点- Parameters:
id- the node ID | 节点IDparentId- the parent ID | 父节点IDdata- 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 | 节点IDparentId- the parent ID | 父节点IDdata- 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 | 节点IDdata- the node data | 节点数据childLoader- the lazy child loader | 懒加载器- Returns:
- the virtual tree node | 虚拟树节点
-
builder
Create a virtual tree node with builder 使用构建器创建虚拟树节点- Type Parameters:
T- the data type | 数据类型ID- the ID type | ID类型- Returns:
- the builder | 构建器
-
getId
-
getParentId
-
getChildren
Description copied from interface:TreeableGet the children list 获取子节点列表- Specified by:
getChildrenin interfaceTreeable<T,ID> - Returns:
- the children | 子节点列表
-
setChildren
Description copied from interface:TreeableSet the children list 设置子节点列表- Specified by:
setChildrenin interfaceTreeable<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
-
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
Find node by ID in cache 从缓存中查找节点- Parameters:
nodeId- the node ID | 节点ID- Returns:
- the node if found | 找到的节点
-
find
Find node by ID (searches tree recursively) 通过ID查找节点(递归搜索树)- Parameters:
nodeId- the node ID | 节点ID- Returns:
- the node if found | 找到的节点
-
findAll
-
traversePreOrder
Traverse in pre-order 前序遍历- Parameters:
visitor- the visitor | 访问者
-
traversePostOrder
Traverse in post-order 后序遍历- Parameters:
visitor- the visitor | 访问者
-
traverseBreadthFirst
Traverse level by level (breadth-first) 层级遍历(广度优先)- Parameters:
visitor- the visitor | 访问者
-
traverseWithDepthLimit
Traverse with depth limit (for controlled lazy loading) 带深度限制的遍历(用于控制懒加载)- Parameters:
visitor- the visitor | 访问者maxDepth- the maximum depth | 最大深度
-
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
-