Class DeepDifference


  • public class DeepDifference
    extends Object
    Tests two objects for differences by doing a 'deep' comparison. Based on the deep equals implementation of https://github.com/jdereg/java-util
    Author:
    John DeRegnaucourt (john@cedarsoftware.com), Pascal Schumacher
    • Constructor Detail

      • DeepDifference

        public DeepDifference()
    • Method Detail

      • determineDifferences

        public static List<DeepDifference.Difference> determineDifferences​(Object a,
                                                                           Object b,
                                                                           Map<String,​Comparator<?>> comparatorByPropertyOrField,
                                                                           TypeComparators comparatorByType)
        Compare two objects for differences by doing a 'deep' comparison. This will traverse the Object graph and perform either a field-by-field comparison on each object (if not .equals() method has been overridden from Object), or it will call the customized .equals() method if it exists.

        This method handles cycles correctly, for example A->B->C->A. Suppose a and a' are two separate instances of the A with the same values for all fields on A, B, and C. Then a.deepEquals(a') will return an empty list. It uses cycle detection storing visited objects in a Set to prevent endless loops.
        Parameters:
        a - Object one to compare
        b - Object two to compare
        comparatorByPropertyOrField - comparators to compare properties or fields with the given names
        comparatorByType - comparators to compare properties or fields with the given types
        Returns:
        the list of differences found or an empty list if objects are equivalent. Equivalent means that all field values of both subgraphs are the same, either at the field level or via the respectively encountered overridden .equals() methods during traversal.