When I really wanted to compare two objects easily, I found a library called Javers. This does pretty good job for the most cases, such as comparing value changes in the objects and also works well for lists. But if a have two objects which contain a map then how will capture the changes? For example I have a following class with a map,
public class Test {
private Map<String,Object> map = new HashMap();
//some others fields
//getters and setters
}
Now, if I have objects of this with the below values:
Test test1 = new Test();
Map<String,Object> map1 = new HashMap();
map1.put("one",1);
map1.put("two",2);
test1.setMap(map1);
Test test2 = new Test();
Map<String,Object> map2 = new HashMap();
map2.put("one",2);
map2.put("two",1);
test2.setMap(map2);
Javers javers = JaversBuilder.javers().build();
javers.compare(test1,test2);//?
This doesn't give me the changes done in map. How will I capture it?
To capture the changes you need to process the diff object returned by the compare method. For example to just print the changes:
Diff diff = javers.compare(test1,test2);
diff.getChanges().forEach(change -> System.out.println(change.toString()));
More info can be found in the documentation of javers: http://javers.org/documentation/diff-examples/
And in the javadoc: http://javers.org/javadoc_3.x/org/javers/core/diff/Diff.html