Top Description Inners Fields Constructors Methods
org.assertj.core.internal

public Class DeepDifference

extends Object
Class Inheritance
Static Imports
java.lang.String.format, org.assertj.core.internal.Objects.getDeclaredFieldsIncludingInherited, .Objects.propertyOrFieldValuesAreEqual, .TypeComparators.defaultTypeComparators, org.assertj.core.util.Sets.newHashSet, .Strings.join, org.assertj.core.util.introspection.PropertyOrFieldSupport.COMPARISON

Tests two objects for differences by doing a 'deep' comparison. Based on the deep equals implementation of https://github.com/jdereg/java-util
Authors
John DeRegnaucourt ([email protected]), Pascal Schumacher

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static class

Field Summary

Modifier and TypeField and Description
private static final Map<Class<?>, Boolean>
private static final Map<Class<?>, Boolean>
private static final String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static boolean

Returns:

true if the two arrays are the same length and contain deeply equivalent items.
compareArrays
(Object
[] type (Object[], String[], etc.)
array1
,
Object
[] type (Object[], String[], etc.)
array2
,
List<String>
the path to the arrays to compare
path
,
Deque<DeepDifference.DualKey>
add items to compare to the Stack (Stack versus recursion)
toCompare
,
Set<DeepDifference.DualKey>
Set of objects already compared (prevents cycles)
visited
)

Deeply compare to Arrays [].

private static <
the key type
K
,
the value type
V
>
boolean

Returns:

boolean false if the Collections are for certain not equals
compareOrderedCollection
(Collection<K>
First collection of items to compare
col1
,
Collection<V>
Second collection of items to compare
col2
,
List<String>
The path to the collections
path
,
Deque<DeepDifference.DualKey>
add items to compare to the Stack (Stack versus recursion)
toCompare
,
Set<DeepDifference.DualKey>
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.
visited
)

Deeply compare two Collections that must be same length and in same order.

private static <
the first key type
K1
,
the first value type
V1
,
the second key type
K2
,
the second value type
V2
>
boolean

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.
compareSortedMap
(SortedMap<K1, V1>
SortedMap one
map1
,
SortedMap<K2, V2>
SortedMap two
map2
,
List<String>
the path to the maps to compare
path
,
Deque<DeepDifference.DualKey>
add items to compare to the Stack (Stack versus recursion)
toCompare
,
Set<DeepDifference.DualKey>
Set containing items that have already been compared, to prevent cycles.
visited
)

Deeply compare two SortedMap instances.

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)

private static <
the key type
K
,
the value type
V
>
boolean

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.
compareUnorderedCollectionByHashCodes
(Collection<K>
First collection of items to compare
col1
,
Collection<V>
Second collection of items to compare
col2
,
List<String>
the path to the collections to compare
path
,
Deque<DeepDifference.DualKey>
add items to compare to the Stack (Stack versus recursion)
toCompare
,
Set<DeepDifference.DualKey>
Set containing items that have already been compared, so as to prevent cycles.
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 <
the first key type
K1
,
the first value type
V1
,
the second key type
K2
,
the second value type
V2
>
boolean

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
(Map<K1, V1>
Map one
map1
,
Map<K2, V2>
Map two
map2
,
List<String>
the path to the maps to compare
path
,
Deque<DeepDifference.DualKey>
add items to compare to the Stack (Stack versus recursion)
toCompare
,
Set<DeepDifference.DualKey>
Set containing items that have already been compared, to prevent cycles.
visited
)

Deeply compare two Map instances.

pack-priv static int

Returns:

the 'deep' hashCode value for the passed in object.
deepHashCode
(Object
Object who hashCode is desired.
obj
)

Get a deterministic hashCode (int) value for an Object, regardless of when it was created or where it was loaded into memory.

public static List<DeepDifference.Difference>

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
(Object
Object one to compare
a
,
Object
Object two to compare
b
,
Map<String, Comparator<?>>
comparators to compare properties or fields with the given names
comparatorByPropertyOrField
,
TypeComparators
comparators to compare properties or fields with the given types
comparatorByType
)

Compare two objects for differences by doing a 'deep' comparison.

private static List<DeepDifference.Difference>
determineDifferences(Object a, Object b, List<String> parentPath, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)

private static Set<String>
private static boolean
hasCustomComparator(DeepDifference.DualKey dualKey, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)

pack-priv static boolean

Returns:

true, if the passed in Class has a .equals() method somewhere between itself and just below Object in it's inheritance.
hasCustomEquals
(Class<?>
Class to check.
c
)

Determine if the passed in class has a non-Object.equals() method.

pack-priv static boolean

Returns:

true, if the passed in Class has a .hashCode() method somewhere between itself and just below Object in it's inheritance.
hasCustomHashCode
(Class<?>
Class to check.
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
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

customEqualsback to summary
private static final Map<Class<?>, Boolean> customEquals
customHashback to summary
private static final Map<Class<?>, Boolean> customHash
MISSING_FIELDSback to summary
private static final String MISSING_FIELDS

Constructor Detail

DeepDifferenceback to summary
public DeepDifference()

Method Detail

compareArraysback to summary
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:Object

[] type (Object[], String[], etc.)

array2:Object

[] type (Object[], String[], etc.)

path:List<String>

the path to the arrays to compare

toCompare:Deque<DeepDifference.DualKey>

add items to compare to the Stack (Stack versus recursion)

visited:Set<DeepDifference.DualKey>

Set of objects already compared (prevents cycles)

Returns:boolean

true if the two arrays are the same length and contain deeply equivalent items.

compareOrderedCollectionback to summary
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.

Parameters
<K>
the key type
<V>
the value type
col1:Collection<K>

First collection of items to compare

col2:Collection<V>

Second collection of items to compare

path:List<String>

The path to the collections

toCompare:Deque<DeepDifference.DualKey>

add items to compare to the Stack (Stack versus recursion)

visited:Set<DeepDifference.DualKey>

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

boolean false if the Collections are for certain not equals

compareSortedMapback to summary
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.

Parameters
<K1>
the first key type
<V1>
the first value type
<K2>
the second key type
<V2>
the second value type
map1:SortedMap<K1, V1>

SortedMap one

map2:SortedMap<K2, V2>

SortedMap two

path:List<String>

the path to the maps to compare

toCompare:Deque<DeepDifference.DualKey>

add items to compare to the Stack (Stack versus recursion)

visited:Set<DeepDifference.DualKey>

Set containing items that have already been compared, to prevent cycles.

Returns:boolean

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.

compareUnorderedCollectionback to summary
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)
compareUnorderedCollectionByHashCodesback to summary
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.

Parameters
<K>
the key type
<V>
the value type
col1:Collection<K>

First collection of items to compare

col2:Collection<V>

Second collection of items to compare

path:List<String>

the path to the collections to compare

toCompare:Deque<DeepDifference.DualKey>

add items to compare to the Stack (Stack versus recursion)

visited:Set<DeepDifference.DualKey>

Set containing items that have already been compared, so as to prevent cycles.

Returns:boolean

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.

compareUnorderedMapback to summary
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.

Parameters
<K1>
the first key type
<V1>
the first value type
<K2>
the second key type
<V2>
the second value type
map1:Map<K1, V1>

Map one

map2:Map<K2, V2>

Map two

path:List<String>

the path to the maps to compare

toCompare:Deque<DeepDifference.DualKey>

add items to compare to the Stack (Stack versus recursion)

visited:Set<DeepDifference.DualKey>

Set containing items that have already been compared, to prevent cycles.

Returns:boolean

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.

deepHashCodeback to summary
pack-priv 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

Object who hashCode is desired.

Returns:int

the 'deep' hashCode value for the passed in object.

determineDifferencesback to summary
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

Object one to compare

b:Object

Object two to compare

comparatorByPropertyOrField:Map<String, Comparator<?>>

comparators to compare properties or fields with the given names

comparatorByType:TypeComparators

comparators to compare properties or fields with the given types

Returns:List<DeepDifference.Difference>

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.

determineDifferencesback to summary
private static List<DeepDifference.Difference> determineDifferences(Object a, Object b, List<String> parentPath, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
getFieldsNamesback to summary
private static Set<String> getFieldsNames(Collection<Field> fields)
hasCustomComparatorback to summary
private static boolean hasCustomComparator(DeepDifference.DualKey dualKey, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
hasCustomEqualsback to summary
pack-priv 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<?>

Class to check.

Returns:boolean

true, if the passed in Class has a .equals() method somewhere between itself and just below Object in it's inheritance.

hasCustomHashCodeback to summary
pack-priv 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<?>

Class to check.

Returns:boolean

true, if the passed in Class has a .hashCode() method somewhere between itself and just below Object in it's inheritance.

initStackback to summary
private static Deque<DeepDifference.DualKey> initStack(Object a, Object b, List<String> parentPath, Map<String, Comparator<?>> comparatorByPropertyOrField, TypeComparators comparatorByType)
isContainerTypeback to summary
private static boolean isContainerType(Object o)
org.assertj.core.internal back to summary

public Class DeepDifference.Difference

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv Object
pack-priv Optional<String>
pack-priv Object
pack-priv List<String>

Constructor Summary

AccessConstructor and Description
public
Difference(List<String> path, Object actual, Object other)

public
Difference(List<String> path, Object actual, Object other, String description)

Method Summary

Modifier and TypeMethod and Description
public Object
public Optional<String>
public Object
public List<String>
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

actualback to summary
pack-priv Object actual
descriptionback to summary
pack-priv Optional<String> description
otherback to summary
pack-priv Object other
pathback to summary
pack-priv List<String> path

Constructor Detail

Differenceback to summary
public Difference(List<String> path, Object actual, Object other)
Differenceback to summary
public Difference(List<String> path, Object actual, Object other, String description)

Method Detail

getActualback to summary
public Object getActual()
getDescriptionback to summary
public Optional<String> getDescription()
getOtherback to summary
public Object getOther()
getPathback to summary
public List<String> getPath()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
org.assertj.core.internal back to summary

private final Class DeepDifference.DualKey

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Object
private final Object
private final List<String>

Constructor Summary

AccessConstructor and Description
private
DualKey(List<String> path, Object key1, Object key2)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public String
public List<String>
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

key1back to summary
private final Object key1
key2back to summary
private final Object key2
pathback to summary
private final List<String> path

Constructor Detail

DualKeyback to summary
private DualKey(List<String> path, Object key1, Object key2)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getConcatenatedPathback to summary
public String getConcatenatedPath()
getPathback to summary
public List<String> getPath()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override