How to get the difference between two simple objects?
franzisk opened this issue · 4 comments
Sorry but I didn't get the point here.
Let's say I have two simple objects and I need to know if there is a difference between them and if there is I need to know where and I also need to know the previous value of that field and the new value.
Let's get this as an example:
Person person1 = new Person();
person1.setName("Freddy");
person1.setAge(22);
Person person2 = new Person();
person2.setName("Christoph");
person2.setAge(22);
What I want to achieve here is:
- it was changed: yes
- the attribute changed was "name"
- the previous value was "Freddy"
- the current value is "Christoph"
Any help on that?
You can do this
Person person1 = new Person();
person1.setName("Freddy");
person1.setAge(22);
Person person2 = new Person();
person2.setName("Christoph");
person2.setAge(22);
DiffNode diff = ObjectDifferBuilder.startBuilding()
.inclusion().exclude()
// .propertyName("name")
.and().build().compare(person1, person2);
diff.visit((node, visit) -> {
final Object baseValue = node.canonicalGet(person1);
final Object workingValue = node.canonicalGet(person2);
final String message = node.getPath() + " changed from " + baseValue + " to " + workingValue + " change state " + node.getState();
System.out.println(message);
});
System.out.println(diff.hasChanges());
you should read https://java-object-diff.readthedocs.io/en/latest/user-guide/
You can do this
Person person1 = new Person(); person1.setName("Freddy"); person1.setAge(22); Person person2 = new Person(); person2.setName("Christoph"); person2.setAge(22); DiffNode diff = ObjectDifferBuilder.startBuilding() .inclusion().exclude() // .propertyName("name") .and().build().compare(person1, person2); diff.visit((node, visit) -> { final Object baseValue = node.canonicalGet(person1); final Object workingValue = node.canonicalGet(person2); final String message = node.getPath() + " changed from " + baseValue + " to " + workingValue + " change state " + node.getState(); System.out.println(message); }); System.out.println(diff.hasChanges());you should read https://java-object-diff.readthedocs.io/en/latest/user-guide/
hi Bro, how about for child Object into parent object changes? please help me, thank bro very much.!
You can do this
Person person1 = new Person(); person1.setName("Freddy"); person1.setAge(22); Person person2 = new Person(); person2.setName("Christoph"); person2.setAge(22); DiffNode diff = ObjectDifferBuilder.startBuilding() .inclusion().exclude() // .propertyName("name") .and().build().compare(person1, person2); diff.visit((node, visit) -> { final Object baseValue = node.canonicalGet(person1); final Object workingValue = node.canonicalGet(person2); final String message = node.getPath() + " changed from " + baseValue + " to " + workingValue + " change state " + node.getState(); System.out.println(message); }); System.out.println(diff.hasChanges());you should read https://java-object-diff.readthedocs.io/en/latest/user-guide/
not work for me, i copy/paste your code.
Could be because i use lombok
@Data
@Accessors(fluent = true)
@Builder