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 ([email protected]), 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.
      • isContainerType

        private static boolean isContainerType​(Object o)
      • compareArrays

        private static boolean compareArrays​(Object array1,
                                             Object array2,
                                             List<String> path,
                                             Deque<DeepDifference.DualKey> toCompare,
                                             Set<DeepDifference.DualKey> visited)
        Deeply compare to Arrays []. Both arrays must be of the same type, same length, and all elements within the arrays must be deeply equal in order to return true.
        Parameters:
        array1 - [] type (Object[], String[], etc.)
        array2 - [] type (Object[], String[], etc.)
        path - the path to the arrays to compare
        toCompare - add items to compare to the Stack (Stack versus recursion)
        visited - Set of objects already compared (prevents cycles)
        Returns:
        true if the two arrays are the same length and contain deeply equivalent items.
      • compareOrderedCollection

        private static <K,​V> boolean compareOrderedCollection​(Collection<K> col1,
                                                                    Collection<V> col2,
                                                                    List<String> path,
                                                                    Deque<DeepDifference.DualKey> toCompare,
                                                                    Set<DeepDifference.DualKey> visited)
        Deeply compare two Collections that must be same length and in same order.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        col1 - First collection of items to compare
        col2 - Second collection of items to compare
        path - The path to the collections
        toCompare - add items to compare to the Stack (Stack versus recursion)
        visited - Set of objects already compared (prevents cycles) value of 'true' indicates that the Collections may be equal, and the sets items will be added to the Stack for further comparison.
        Returns:
        boolean false if the Collections are for certain not equals
      • compareUnorderedCollectionByHashCodes

        private static <K,​V> boolean compareUnorderedCollectionByHashCodes​(Collection<K> col1,
                                                                                 Collection<V> col2,
                                                                                 List<String> path,
                                                                                 Deque<DeepDifference.DualKey> toCompare,
                                                                                 Set<DeepDifference.DualKey> visited)
        It places one collection into a temporary Map by deepHashCode(), so that it can walk the other collection and look for each item in the map, which runs in O(N) time, rather than an O(N^2) lookup that would occur if each item from collection one was scanned for in collection two.
        Type Parameters:
        K - the key type
        V - the value type
        Parameters:
        col1 - First collection of items to compare
        col2 - Second collection of items to compare
        path - the path to the collections to compare
        toCompare - add items to compare to the Stack (Stack versus recursion)
        visited - Set containing items that have already been compared, so as to prevent cycles.
        Returns:
        boolean false if the Collections are for certain not equals. A value of 'true' indicates that the Collections may be equal, and the sets items will be added to the Stack for further comparison.
      • compareSortedMap

        private static <K1,​V1,​K2,​V2> boolean compareSortedMap​(SortedMap<K1,​V1> map1,
                                                                                SortedMap<K2,​V2> map2,
                                                                                List<String> path,
                                                                                Deque<DeepDifference.DualKey> toCompare,
                                                                                Set<DeepDifference.DualKey> visited)
        Deeply compare two SortedMap instances. This method walks the Maps in order, taking advantage of the fact that the Maps are SortedMaps.
        Type Parameters:
        K1 - the first key type
        V1 - the first value type
        K2 - the second key type
        V2 - the second value type
        Parameters:
        map1 - SortedMap one
        map2 - SortedMap two
        path - the path to the maps to compare
        toCompare - add items to compare to the Stack (Stack versus recursion)
        visited - Set containing items that have already been compared, to prevent cycles.
        Returns:
        false if the Maps are for certain not equals. 'true' indicates that 'on the surface' the maps are equal, however, it will place the contents of the Maps on the stack for further comparisons.
      • compareUnorderedMap

        private static <K1,​V1,​K2,​V2> boolean compareUnorderedMap​(Map<K1,​V1> map1,
                                                                                   Map<K2,​V2> map2,
                                                                                   List<String> path,
                                                                                   Deque<DeepDifference.DualKey> toCompare,
                                                                                   Set<DeepDifference.DualKey> visited)
        Deeply compare two Map instances. After quick short-circuit tests, this method uses a temporary Map so that this method can run in O(N) time.
        Type Parameters:
        K1 - the first key type
        V1 - the first value type
        K2 - the second key type
        V2 - the second value type
        Parameters:
        map1 - Map one
        map2 - Map two
        path - the path to the maps to compare
        toCompare - add items to compare to the Stack (Stack versus recursion)
        visited - Set containing items that have already been compared, to prevent cycles.
        Returns:
        false if the Maps are for certain not equals. 'true' indicates that 'on the surface' the maps are equal, however, it will place the contents of the Maps on the stack for further comparisons.
      • hasCustomEquals

        static boolean hasCustomEquals​(Class<?> c)
        Determine if the passed in class has a non-Object.equals() method. This method caches its results in static ConcurrentHashMap to benefit execution performance.
        Parameters:
        c - Class to check.
        Returns:
        true, if the passed in Class has a .equals() method somewhere between itself and just below Object in it's inheritance.
      • deepHashCode

        static int deepHashCode​(Object obj)
        Get a deterministic hashCode (int) value for an Object, regardless of when it was created or where it was loaded into memory. The problem with java.lang.Object.hashCode() is that it essentially relies on memory location of an object (what identity it was assigned), whereas this method will produce the same hashCode for any object graph, regardless of how many times it is created.

        This method will handle cycles correctly (A->B->C->A). In this case, Starting with object A, B, or C would yield the same hashCode. If an object encountered (root, subobject, etc.) has a hashCode() method on it (that is not Object.hashCode()), that hashCode() method will be called and it will stop traversal on that branch.
        Parameters:
        obj - Object who hashCode is desired.
        Returns:
        the 'deep' hashCode value for the passed in object.
      • hasCustomHashCode

        static boolean hasCustomHashCode​(Class<?> c)
        Determine if the passed in class has a non-Object.hashCode() method. This method caches its results in static ConcurrentHashMap to benefit execution performance.
        Parameters:
        c - Class to check.
        Returns:
        true, if the passed in Class has a .hashCode() method somewhere between itself and just below Object in it's inheritance.