Class TreeDiff

java.lang.Object
cloud.opencode.base.tree.diff.TreeDiff

public final class TreeDiff extends Object
Tree Diff 树差异计算

Compares two trees and finds differences.

比较两棵树并找出差异。

Features | 主要功能:

  • Compare two trees by node ID - 通过节点ID比较两棵树
  • Custom equality checker support - 自定义相等性检查器支持
  • Custom key extractor support - 自定义键提取器支持
  • Categorized diff (added, removed, modified, unchanged) - 分类差异(新增、删除、修改、未变化)

Usage Examples | 使用示例:

TreeDiffResult<MyNode> diff = TreeDiff.diff(oldRoots, newRoots);
if (diff.hasChanges()) {
    System.out.println("Added: " + diff.getAddedCount());
    System.out.println("Removed: " + diff.getRemovedCount());
}

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

    • diff

      public static <T extends Treeable<T,ID>, ID> TreeDiffResult<T> diff(List<T> oldRoots, List<T> newRoots)
      Compare two trees by ID 通过ID比较两棵树
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      oldRoots - the old tree roots | 旧树根节点
      newRoots - the new tree roots | 新树根节点
      Returns:
      the diff result | 差异结果
    • diff

      public static <T extends Treeable<T,ID>, ID> TreeDiffResult<T> diff(List<T> oldRoots, List<T> newRoots, BiPredicate<T,T> equalityChecker)
      Compare two trees with custom equality 使用自定义相等性比较两棵树
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      oldRoots - the old tree roots | 旧树根节点
      newRoots - the new tree roots | 新树根节点
      equalityChecker - the equality checker | 相等性检查器
      Returns:
      the diff result | 差异结果
    • diffByKey

      public static <T extends Treeable<T,ID>, ID, K> TreeDiffResult<T> diffByKey(List<T> oldRoots, List<T> newRoots, Function<T,K> keyExtractor, BiPredicate<T,T> equalityChecker)
      Compare using custom key extractor 使用自定义键提取器比较
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      K - the key type | 键类型
      Parameters:
      oldRoots - the old tree roots | 旧树根节点
      newRoots - the new tree roots | 新树根节点
      keyExtractor - the key extractor | 键提取器
      equalityChecker - the equality checker | 相等性检查器
      Returns:
      the diff result | 差异结果