Class RecursiveComparisonDifferenceCalculator

    • Field Detail

      • DIFFERENT_ACTUAL_AND_EXPECTED_FIELD_TYPES

        private static final java.lang.String DIFFERENT_ACTUAL_AND_EXPECTED_FIELD_TYPES
        See Also:
        Constant Field Values
      • ACTUAL_NOT_ORDERED_COLLECTION

        private static final java.lang.String ACTUAL_NOT_ORDERED_COLLECTION
      • VALUE_FIELD_NAME

        private static final java.lang.String VALUE_FIELD_NAME
        See Also:
        Constant Field Values
      • STRICT_TYPE_ERROR

        private static final java.lang.String STRICT_TYPE_ERROR
        See Also:
        Constant Field Values
      • DIFFERENT_SIZE_ERROR

        private static final java.lang.String DIFFERENT_SIZE_ERROR
        See Also:
        Constant Field Values
      • 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

      • RecursiveComparisonDifferenceCalculator

        public RecursiveComparisonDifferenceCalculator()
    • Method Detail

      • determineDifferences

        public java.util.List<ComparisonDifference> determineDifferences​(java.lang.Object actual,
                                                                         java.lang.Object expected,
                                                                         RecursiveComparisonConfiguration recursiveComparisonConfiguration)
        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:
        actual - Object one to compare
        expected - Object two to compare
        recursiveComparisonConfiguration - the recursive comparison configuration
        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<ComparisonDifference> determineDifferences​(java.lang.Object actual,
                                                                                 java.lang.Object expected,
                                                                                 java.util.List<java.lang.String> parentPath,
                                                                                 boolean isRootObject,
                                                                                 java.util.List<DualValue> visited,
                                                                                 RecursiveComparisonConfiguration recursiveComparisonConfiguration)
      • differentTypeErrorMessage

        private static java.lang.String differentTypeErrorMessage​(DualValue dualValue,
                                                                  java.lang.String actualTypeDescription)
      • hasOverriddenEquals

        static boolean hasOverriddenEquals​(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.
      • expectedAndActualTypeDifference

        private static ComparisonDifference expectedAndActualTypeDifference​(java.lang.Object actual,
                                                                            java.lang.Object expected)
      • expectedTypeIsNotSubtypeOfActualType

        private static boolean expectedTypeIsNotSubtypeOfActualType​(DualValue dualField)
      • expectedTypeIsNotSubtypeOfActualType

        private static boolean expectedTypeIsNotSubtypeOfActualType​(java.lang.Object actual,
                                                                    java.lang.Object expected)
      • describeOrderedCollectionTypes

        private static java.lang.String describeOrderedCollectionTypes()