Class DeepDifference


  • public class DeepDifference
    extends java.lang.Object
    Tests two objects for differences by doing a 'deep' comparison. Based on the deep equals implementation of https://github.com/jdereg/java-util
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Map<java.lang.Class<?>,​java.lang.Boolean> customEquals  
      private static java.util.Map<java.lang.Class<?>,​java.lang.Boolean> customHash  
      private static java.lang.String MISSING_FIELDS  
    • Constructor Summary

      Constructors 
      Constructor Description
      DeepDifference()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static boolean compareArrays​(java.lang.Object array1, java.lang.Object array2, java.util.List<java.lang.String> path, java.util.Deque<DeepDifference.DualKey> toCompare, java.util.Set<DeepDifference.DualKey> visited)
      Deeply compare to Arrays [].
      private static <K,​V>
      boolean
      compareOrderedCollection​(java.util.Collection<K> col1, java.util.Collection<V> col2, java.util.List<java.lang.String> path, java.util.Deque<DeepDifference.DualKey> toCompare, java.util.Set<DeepDifference.DualKey> visited)
      Deeply compare two Collections that must be same length and in same order.
      private static <K1,​V1,​K2,​V2>
      boolean
      compareSortedMap​(java.util.SortedMap<K1,​V1> map1, java.util.SortedMap<K2,​V2> map2, java.util.List<java.lang.String> path, java.util.Deque<DeepDifference.DualKey> toCompare, java.util.Set<DeepDifference.DualKey> visited)
      Deeply compare two SortedMap instances.
      private static <K,​V>
      boolean
      compareUnorderedCollection​(java.util.Collection<K> col1, java.util.Collection<V> col2, java.util.List<java.lang.String> path, java.util.Deque<DeepDifference.DualKey> toCompare, java.util.Set<DeepDifference.DualKey> visited, java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)  
      private static <K,​V>
      boolean
      compareUnorderedCollectionByHashCodes​(java.util.Collection<K> col1, java.util.Collection<V> col2, java.util.List<java.lang.String> path, java.util.Deque<DeepDifference.DualKey> toCompare, java.util.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.
      private static <K1,​V1,​K2,​V2>
      boolean
      compareUnorderedMap​(java.util.Map<K1,​V1> map1, java.util.Map<K2,​V2> map2, java.util.List<java.lang.String> path, java.util.Deque<DeepDifference.DualKey> toCompare, java.util.Set<DeepDifference.DualKey> visited)
      Deeply compare two Map instances.
      (package private) static int deepHashCode​(java.lang.Object obj)
      Get a deterministic hashCode (int) value for an Object, regardless of when it was created or where it was loaded into memory.
      private static java.util.List<DeepDifference.Difference> determineDifferences​(java.lang.Object a, java.lang.Object b, java.util.List<java.lang.String> parentPath, java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)  
      static java.util.List<DeepDifference.Difference> determineDifferences​(java.lang.Object a, java.lang.Object b, java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
      Compare two objects for differences by doing a 'deep' comparison.
      private static java.util.Set<java.lang.String> getFieldsNames​(java.util.Collection<java.lang.reflect.Field> fields)  
      private static boolean hasCustomComparator​(DeepDifference.DualKey dualKey, java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)  
      (package private) static boolean hasCustomEquals​(java.lang.Class<?> c)
      Determine if the passed in class has a non-Object.equals() method.
      (package private) static boolean hasCustomHashCode​(java.lang.Class<?> c)
      Determine if the passed in class has a non-Object.hashCode() method.
      private static java.util.Deque<DeepDifference.DualKey> initStack​(java.lang.Object a, java.lang.Object b, java.util.List<java.lang.String> parentPath, java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)  
      private static boolean isContainerType​(java.lang.Object o)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • customEquals

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Boolean> customEquals
      • customHash

        private static final java.util.Map<java.lang.Class<?>,​java.lang.Boolean> customHash
    • Constructor Detail

      • DeepDifference

        public DeepDifference()
    • Method Detail

      • determineDifferences

        public static java.util.List<DeepDifference.Difference> determineDifferences​(java.lang.Object a,
                                                                                     java.lang.Object b,
                                                                                     java.util.Map<java.lang.String,​java.util.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.
      • determineDifferences

        private static java.util.List<DeepDifference.Difference> determineDifferences​(java.lang.Object a,
                                                                                      java.lang.Object b,
                                                                                      java.util.List<java.lang.String> parentPath,
                                                                                      java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                                                      TypeComparators comparatorByType)
      • hasCustomComparator

        private static boolean hasCustomComparator​(DeepDifference.DualKey dualKey,
                                                   java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                   TypeComparators comparatorByType)
      • initStack

        private static java.util.Deque<DeepDifference.DualKey> initStack​(java.lang.Object a,
                                                                         java.lang.Object b,
                                                                         java.util.List<java.lang.String> parentPath,
                                                                         java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                                         TypeComparators comparatorByType)
      • getFieldsNames

        private static java.util.Set<java.lang.String> getFieldsNames​(java.util.Collection<java.lang.reflect.Field> fields)
      • isContainerType

        private static boolean isContainerType​(java.lang.Object o)
      • compareArrays

        private static boolean compareArrays​(java.lang.Object array1,
                                             java.lang.Object array2,
                                             java.util.List<java.lang.String> path,
                                             java.util.Deque<DeepDifference.DualKey> toCompare,
                                             java.util.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​(java.util.Collection<K> col1,
                                                                    java.util.Collection<V> col2,
                                                                    java.util.List<java.lang.String> path,
                                                                    java.util.Deque<DeepDifference.DualKey> toCompare,
                                                                    java.util.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​(java.util.Collection<K> col1,
                                                                                 java.util.Collection<V> col2,
                                                                                 java.util.List<java.lang.String> path,
                                                                                 java.util.Deque<DeepDifference.DualKey> toCompare,
                                                                                 java.util.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.
      • compareUnorderedCollection

        private static <K,​V> boolean compareUnorderedCollection​(java.util.Collection<K> col1,
                                                                      java.util.Collection<V> col2,
                                                                      java.util.List<java.lang.String> path,
                                                                      java.util.Deque<DeepDifference.DualKey> toCompare,
                                                                      java.util.Set<DeepDifference.DualKey> visited,
                                                                      java.util.Map<java.lang.String,​java.util.Comparator<?>> comparatorByPropertyOrField,
                                                                      TypeComparators comparatorByType)
      • compareSortedMap

        private static <K1,​V1,​K2,​V2> boolean compareSortedMap​(java.util.SortedMap<K1,​V1> map1,
                                                                                java.util.SortedMap<K2,​V2> map2,
                                                                                java.util.List<java.lang.String> path,
                                                                                java.util.Deque<DeepDifference.DualKey> toCompare,
                                                                                java.util.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​(java.util.Map<K1,​V1> map1,
                                                                                   java.util.Map<K2,​V2> map2,
                                                                                   java.util.List<java.lang.String> path,
                                                                                   java.util.Deque<DeepDifference.DualKey> toCompare,
                                                                                   java.util.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​(java.lang.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​(java.lang.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​(java.lang.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.