Class ObjectDiff

java.lang.Object
cloud.opencode.base.core.bean.ObjectDiff

public final class ObjectDiff extends Object
Object Difference Comparison Engine - Compares two objects and reports property-level diffs 对象差异比较引擎 - 比较两个对象并报告属性级别的差异

Provides both simple shallow comparison and advanced deep comparison with cycle detection, depth limits, include/exclude field filtering, and collection diff support.

提供简单的浅比较和高级深度比较功能,支持循环引用检测、深度限制、 字段包含/排除过滤以及集合差异比较。

Features | 主要功能:

  • Shallow property comparison using Objects.equals - 使用 Objects.equals 的浅属性比较
  • Deep recursive comparison with cycle detection - 带循环引用检测的深度递归比较
  • Configurable max depth with overflow protection - 可配置的最大深度及溢出保护
  • Include/exclude field filtering - 字段包含/排除过滤
  • Collection element-level diff - 集合元素级别差异比较
  • Record type support - Record 类型支持

Usage Examples | 使用示例:

// Simple compare - 简单比较
DiffResult<User> result = ObjectDiff.compare(oldUser, newUser);
if (result.hasDiffs()) {
    result.getModified().forEach(d ->
        System.out.println(d.fieldName() + ": " + d.oldValue() + " -> " + d.newValue()));
}

// Advanced compare with builder - 使用构建器进行高级比较
DiffResult<User> result = ObjectDiff.builder(oldUser, newUser)
    .deep(true)
    .maxDepth(5)
    .exclude("password", "internalId")
    .collectionDiff(true)
    .compare();

Security | 安全性:

  • Thread-safe: Yes (stateless utility, Builder is single-thread use then compare) - 线程安全: 是 (无状态工具,Builder 为单线程使用后比较)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • compare

      public static <T> DiffResult<T> compare(T oldObj, T newObj)
      Compares two objects shallowly, reporting property-level diffs 浅比较两个对象,报告属性级别的差异

      Uses Objects.equals(Object, Object) for each property value.

      对每个属性值使用 Objects.equals(Object, Object) 进行比较。

      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      oldObj - the old object (nullable) | 旧对象(可为 null)
      newObj - the new object (nullable) | 新对象(可为 null)
      Returns:
      the diff result | 差异结果
    • builder

      public static <T> ObjectDiff.ObjectDiffBuilder<T> builder(T oldObj, T newObj)
      Creates an advanced diff builder for the two objects 为两个对象创建高级差异比较构建器
      Type Parameters:
      T - the object type | 对象类型
      Parameters:
      oldObj - the old object (nullable) | 旧对象(可为 null)
      newObj - the new object (nullable) | 新对象(可为 null)
      Returns:
      a new builder instance | 新的构建器实例