Package org.assertj.core.internal
Class DeepDifference
- java.lang.Object
-
- org.assertj.core.internal.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
DeepDifference.Difference
private static class
DeepDifference.DualKey
-
Field Summary
Fields Modifier and Type Field Description private static Map<Class<?>,Boolean>
customEquals
private static Map<Class<?>,Boolean>
customHash
private static 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(Object array1, Object array2, List<String> path, Deque<DeepDifference.DualKey> toCompare, Set<DeepDifference.DualKey> visited)
Deeply compare to Arrays [].private static <K,V>
booleancompareOrderedCollection(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.private static <K1,V1,K2,V2>
booleancompareSortedMap(SortedMap<K1,V1> map1, SortedMap<K2,V2> map2, List<String> path, Deque<DeepDifference.DualKey> toCompare, Set<DeepDifference.DualKey> visited)
Deeply compare two SortedMap instances.private static <K,V>
booleancompareUnorderedCollection(Collection<K> col1, Collection<V> col2, List<String> path, Deque<DeepDifference.DualKey> toCompare, Set<DeepDifference.DualKey> visited, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
private static <K,V>
booleancompareUnorderedCollectionByHashCodes(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.private static <K1,V1,K2,V2>
booleancompareUnorderedMap(Map<K1,V1> map1, Map<K2,V2> map2, List<String> path, Deque<DeepDifference.DualKey> toCompare, Set<DeepDifference.DualKey> visited)
Deeply compare two Map instances.(package private) 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.private static List<DeepDifference.Difference>
determineDifferences(Object a, Object b, List<String> parentPath, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
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.private static Set<String>
getFieldsNames(Collection<Field> fields)
private static boolean
hasCustomComparator(DeepDifference.DualKey dualKey, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
(package private) static boolean
hasCustomEquals(Class<?> c)
Determine if the passed in class has a non-Object.equals() method.(package private) static boolean
hasCustomHashCode(Class<?> c)
Determine if the passed in class has a non-Object.hashCode() method.private static Deque<DeepDifference.DualKey>
initStack(Object a, Object b, List<String> parentPath, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
private static boolean
isContainerType(Object o)
-
-
-
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 compareb
- Object two to comparecomparatorByPropertyOrField
- comparators to compare properties or fields with the given namescomparatorByType
- 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 List<DeepDifference.Difference> determineDifferences(Object a, Object b, List<String> parentPath, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
-
hasCustomComparator
private static boolean hasCustomComparator(DeepDifference.DualKey dualKey, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
-
initStack
private static Deque<DeepDifference.DualKey> initStack(Object a, Object b, List<String> parentPath, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
-
getFieldsNames
private static Set<String> getFieldsNames(Collection<Field> fields)
-
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 comparetoCompare
- 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 typeV
- the value type- Parameters:
col1
- First collection of items to comparecol2
- Second collection of items to comparepath
- The path to the collectionstoCompare
- 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 typeV
- the value type- Parameters:
col1
- First collection of items to comparecol2
- Second collection of items to comparepath
- the path to the collections to comparetoCompare
- 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(Collection<K> col1, Collection<V> col2, List<String> path, Deque<DeepDifference.DualKey> toCompare, Set<DeepDifference.DualKey> visited, Map<String,Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
-
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 typeV1
- the first value typeK2
- the second key typeV2
- the second value type- Parameters:
map1
- SortedMap onemap2
- SortedMap twopath
- the path to the maps to comparetoCompare
- 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 typeV1
- the first value typeK2
- the second key typeV2
- the second value type- Parameters:
map1
- Map onemap2
- Map twopath
- the path to the maps to comparetoCompare
- 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.
-
-