How to create a delta object that only has fields that changed?
سؤال
I have to write a common utility class that does following:
Input: 2 objects of the same class - old and new
Output: 1 object of the same class as Inputs, with only the fields that change - delta
Not sure if there is any existing framework out there. I would prefer to use Scala, but even Java is ok.
المحلول
If the objects are beans, then you can use the java.beans API. It lets you query what the fields of the bean are, via Introspector. It would be up to you to iterate over all the fields of objA and objB, comparing values, and setting values on objRes. Also, the class type would have to have a default constructor.
لا تنتمي إلى StackOverflow