An attempt to compare two objects and extract a third object containing their differences

I have two domain objects of the same type. They contain enumerations, primitive arrays and other objects, and there is a list in Heyrarchia.

I need something to extract a third object of the same type that contains only their differences, almost like a mask containing only their changes. And everything that has not changed should be null.

Everything points to Apache BeanUtils, but I can't find what I'm looking for, any suggestions?

Edit # 1 Example for clarification: If obj1 is the original and obj2 is the updated version. Then, if obj1.value is equal to obj2.value, then obj3.value will be null. If obj1.value is not equal to obj2.value, then obj3.value will be set to obj2.value

Edit # 2 Ideally, this should be abstract and in no way need to know what type of object the comparison is on. Since it can be used for different objects in the future. If one of the update values ​​is set to null, then it can be ignored, as if it were not a change.

+4
source share
2 answers

This can be done without any external library;)

Take a trivial bean

public class Bean { public String value; public List<String> list; public String[] array; public EnumType enum; } 

and add the static (factory) method:

 public static Bean createDelta(Bean master, Bean variant) { Bean delta = new Bean(); // fields are simple if (!master.value.equals(variant.value)) delta.value = variant.value; // enums are simple too if (master.enumValue != variant.enumValue) delta.value = variant.value; // for arrays .. it get slightly difficult, because arrays may vary in size int size = master.array.length > variant.array.length ? master.array.length : variant.array.length; delta.array = new String[size]; for (int i = 0; i < size; i++) { if ((i >= master.array.length) || (!master.array[i].equals(variant.array[i]))) { delta.array[i] = variant.array[i]; // same pattern for lists - except we have to add null int size = master.array.length > variant.array.length ? master.array.length : variant.array.length; delta.list = new ArrayList<String>(); for (int i = 0; i < size; i++) { if ((i >= master.array.length) || (!master.array[i].equals(variant.array[i]))) { delta.list.add(variant.get(i)); } else { delta.list.add(null); } } } 

(Note - not verified, no IDE / compiler at hand, but it shows a general approach)

+1
source

Your question is interesting to me. I am looking a lot for your goals and find a small library. This library is located in google code and its name is jettison . This utility has a main class called Diff4J , which has a method with the diffs method, and it compares two objects and finds different ones.

Then I write codes for your purposes as follows:

fisrt define a Model Object named Bean :

 public class Bean { private String name; private String family; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFamily() { return family; } public void setFamily(String family) { this.family = family; } public Bean() { } public Bean(String name, String family ) { this.name = name; this.family = family; } } 

Then coding the test class as follows:

 public static void main(String[] args) throws IllegalAccessException, InvocationTargetException { Bean bean_1 = new Bean("Sara", "clooney"); Bean bean_2 = new Bean("Sally", "clooney"); Diff4J comparator = new Diff4J(); Collection<ChangeInfo> diffs = comparator.diff(bean_1, bean_2); Bean final_result = new Bean(); for(ChangeInfo c : diffs) { String filedName = c.getFieldName(); Object to_value = c.getTo(); Object from_value = c.getFrom(); BeanUtilsBean.getInstance().setProperty(final_result, filedName, to_value); } System.out.println(final_result); } 

In this solution, if you run this code, see the following result:

 Bean [family=null, name=Sally] 

this result is your goal.

Note: In the last line of the loop statement, I used the BeanUtilBean from the Apache Commons Util for the Reflection fill object.

This utility has a problem, it does not support Deep Comparator (maybe I could not find it), and you should imitate this task.

To view this library, go to http://code.google.com/p/jettison/ .

I hope this answer helps you.

+1
source

Source: https://habr.com/ru/post/1401121/