Commons BeanUtils
  1. Commons BeanUtils
  2. BEANUTILS-304

BeanDiff - encapsulate, apply, and merge bean differences

    Details

      Description

      It would be great to have a way to compare two beans and capture the diff somehow. This is just my initial brainstorm on the idea:

      • The BeanDiff class encapsulates differences between two beans of the same type, the source bean and the target bean.
      • A BeanDiff instance represents what property assignments would be necessary to turn the target bean into the source bean.
      • A BeanDiff can be applied to a target bean, performing the necessary property assignments to make its property values exactly match the source bean.
      • Two BeanDiff instances can be merged into a single BeanDiff instance, allowing multiple source beans to be merged into one target bean.
      • If a conflict would occur during a merge (from a property being assigned two different values), a BeanDiffConflictException is thrown.
      • The BeanDiffConflictException contains an array of BeanDiffConflict objects.
      • Each BeanDiffConflict instance represents a single property and the two conflicting values that were to be assigned.
      • All these actions would work on DynaBeans as well.
      • Converters would be able to account for type differences in source and target bean properties.

      An example of how this could be used is when dealing with ORM and optimistic locking. Knowing exactly which properties have been modified would allow concurrent modification of a bean (record) without fear of reasserting the original values and destroying someone else's changes.

        Activity

        Hide
        Mark Lewis added a comment -

        Didn't realize that a "week" was a 168-hour "actual" week and not a 40-hour "work" week.

        Show
        Mark Lewis added a comment - Didn't realize that a "week" was a 168-hour "actual" week and not a 40-hour "work" week.
        Hide
        Java Developer added a comment - - edited

        Sorry for English!:

        In Weblogic application server's classes and jar files you can find the "com.bea.core.descriptor_1.4.0.0.jar" in "BEA_HOME\modules" folder.

        You can find some interesting classes inside "weblogic.descriptor" package (e.g. BeanUpdateListener, BeanUpdateEvent, DescriptorBean, Descriptor) . And also another interesting class "weblogic.descriptor.internal.BeanDiff" class. Looking at them is useful!! and may brings some ideas.

        Implementing this feature would also eases the process of writing ConfigurationMBeans for applications(http://issues.apache.org/jira/browse/CONFIGURATION-394). ConfigurationMBeans and RuntimeMBeans are two mbean hierarchy in weblogic application server.

        Show
        Java Developer added a comment - - edited Sorry for English!: In Weblogic application server's classes and jar files you can find the "com.bea.core.descriptor_1.4.0.0.jar" in "BEA_HOME\modules" folder. You can find some interesting classes inside "weblogic.descriptor" package (e.g. BeanUpdateListener, BeanUpdateEvent, DescriptorBean, Descriptor) . And also another interesting class "weblogic.descriptor.internal.BeanDiff" class. Looking at them is useful!! and may brings some ideas. Implementing this feature would also eases the process of writing ConfigurationMBeans for applications( http://issues.apache.org/jira/browse/CONFIGURATION-394 ). ConfigurationMBeans and RuntimeMBeans are two mbean hierarchy in weblogic application server.
        Hide
        Henri Yandell added a comment -

        Looking at them using a decompiler would seem a bad idea legally. Please do not do this and then contribute code to Apache.

        Show
        Henri Yandell added a comment - Looking at them using a decompiler would seem a bad idea legally. Please do not do this and then contribute code to Apache.
        Hide
        Java Developer added a comment -

        You are right, I've corrected it. Another suggestion: Xml diff algorithms(e.g. http://tdm.berlios.de/3dm/doc/index.html) might help to implement BeanDiff.

        Show
        Java Developer added a comment - You are right, I've corrected it. Another suggestion: Xml diff algorithms(e.g. http://tdm.berlios.de/3dm/doc/index.html ) might help to implement BeanDiff.
        Hide
        Java Developer added a comment -

        I've attached a simple implementation of BeanDiff here:

        http://issues.apache.org/jira/browse/CONFIGURATION-394

        Hope it helps.

        Show
        Java Developer added a comment - I've attached a simple implementation of BeanDiff here: http://issues.apache.org/jira/browse/CONFIGURATION-394 Hope it helps.
        Hide
        Java Developer added a comment -

        I've attached better and more complete implementation of BeanDiff here:

        http://issues.apache.org/jira/browse/CONFIGURATION-394

        'FinalConfigClasses.zip' contains implementation.

        Show
        Java Developer added a comment - I've attached better and more complete implementation of BeanDiff here: http://issues.apache.org/jira/browse/CONFIGURATION-394 'FinalConfigClasses.zip' contains implementation.
        Hide
        Mark Lewis added a comment -

        I've also found an implementation of BeanDiff here (Google Code).

        The problem is that one requires the beans to implement an interface, and the other requires them to be annotated. This introduces unnecessary compile-time dependencies, and it means you can't use existing beans which have already been compiled. I would've expected it to work more like BeanUtils.copyProperties(dest, src) where it can work on any bean.

        I did some experimental work on this a while, but I don't remember where I stashed it.

        Show
        Mark Lewis added a comment - I've also found an implementation of BeanDiff here (Google Code). The problem is that one requires the beans to implement an interface, and the other requires them to be annotated. This introduces unnecessary compile-time dependencies, and it means you can't use existing beans which have already been compiled. I would've expected it to work more like BeanUtils.copyProperties(dest, src) where it can work on any bean. I did some experimental work on this a while, but I don't remember where I stashed it.
        Hide
        Java Developer added a comment -

        Sorry for English:

        Take advantage of both worlds, I mean we can have a default behavior for beans which doesn't implement the interface nor annotated( like BeanUtils.copyProperties(dest, src) ), and, customized differentiating behavior for beans which implement the interface or are annotated.

        This way the programmer can implement his/her customized differentiating behavior as well.

        Choice between interface and annotation is a bit challenging because some decisions of differentiating algorithm could be made at run-time. Annotations can not help in run-time case. For an example: the decision about Sub-diff for current property may be annotated with @Subdiff or be implemented with the assist of an interface which is more flexible and dynamic.

        Think about both annotation and interface

        Show
        Java Developer added a comment - Sorry for English: Take advantage of both worlds, I mean we can have a default behavior for beans which doesn't implement the interface nor annotated( like BeanUtils.copyProperties(dest, src) ), and, customized differentiating behavior for beans which implement the interface or are annotated. This way the programmer can implement his/her customized differentiating behavior as well. Choice between interface and annotation is a bit challenging because some decisions of differentiating algorithm could be made at run-time. Annotations can not help in run-time case. For an example: the decision about Sub-diff for current property may be annotated with @Subdiff or be implemented with the assist of an interface which is more flexible and dynamic. Think about both annotation and interface
        Hide
        Mark Lewis added a comment - - edited

        If it did use annotations/interfaces, it would be an anomaly within Commons BeanUtils. None of the current APIs require changes to your beans, and I'm sure that's by design. (Also, I think they're still trying to make the library compatible with Java 1.4, so annotations may not be allowed.)

        Some level of customization could be handled already with org.apache.commons.beanutils.Converter and java.util.Comparator. As for sub-diff, I think that would best be handled programmatically rather than declaratively. You'd run a diff on the two parent beans. Then if the child beans are different, you could run a diff on them too. This would simplify the API quite a bit, as we wouldn't have to worry about representing nested differences.

        These are great use-cases, though, and we should consider all possibilities.

        Show
        Mark Lewis added a comment - - edited If it did use annotations/interfaces, it would be an anomaly within Commons BeanUtils. None of the current APIs require changes to your beans, and I'm sure that's by design. (Also, I think they're still trying to make the library compatible with Java 1.4, so annotations may not be allowed.) Some level of customization could be handled already with org.apache.commons.beanutils.Converter and java.util.Comparator . As for sub-diff, I think that would best be handled programmatically rather than declaratively. You'd run a diff on the two parent beans. Then if the child beans are different, you could run a diff on them too. This would simplify the API quite a bit, as we wouldn't have to worry about representing nested differences. These are great use-cases, though, and we should consider all possibilities.
        Hide
        Scott Langley added a comment -

        There is also this implementation:

        http://sqisher.github.io/java-object-diff/

        which uses the Apache 2.0 license and works with nearly all object types - not just beans.

        Show
        Scott Langley added a comment - There is also this implementation: http://sqisher.github.io/java-object-diff/ which uses the Apache 2.0 license and works with nearly all object types - not just beans.

          People

          • Assignee:
            Unassigned
            Reporter:
            Mark Lewis
          • Votes:
            4 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:

              Time Tracking

              Estimated:
              Original Estimate - 240h
              240h
              Remaining:
              Remaining Estimate - 240h
              240h
              Logged:
              Time Spent - Not Specified
              Not Specified

                Development