-
Notifications
You must be signed in to change notification settings - Fork 0
Comparison
A comparison operation compares two instances of the same type. As in all utilities the comparison can be customized by a builder.
The comparison of the attriutes is based on Type.isEqual() instead of Object.equals(). The comparison of associated EntityTypes is based on EntityType.isEqual(), whose implementation is type specific and which may make a deep comparison. Therefore comparison of associated entities should be customized by either omitting them, restricting them (e.g.: a technical id or a business key).
A difference includes a type, the two values and a path to the attribute. The type can be addition, removal, reordering or attribute change. The path can include keys and indexes for maps and lists respectively. A path cannot describe an element in an unsorted or unordered collection though. Differences in these collections will only include additions and deletions of elements. Changes of elements will therefore not appear. The algorithm for finding reodering depends on identifying entities. If the attributes included in the comparison are equal then the entities are considered identical.
ComparisonBuilder comparisonBuilder = comparisonBuilderFactory.create(myEntityType);
comparisonBuilder.include("addresses");
....
Comparison comparison = comparisonBuilder.create();
Set<Difference> differences = comparison.compare(person1, person2);
###Definition of the changes.
- list changes:
first the elements are compared. If the identical elements are found then there position is checked. If it changed then a reorder change is created. If the entity exists more than once then the minimal modifications necessary should be calculated. path includes index. reorder all other elements are additions and removals - map changes. additions and removals.
- set changes: removals and additions. no attribute changes for elements. path do not include index.
- sorted set: path includes no index. additions and removals. no attriue changes.
###Configuration
- classpath:/meta/utility/compare.xml : classpath scanning
- classpath:/meta/utility/compare-example.xml : defines component "meta-utility-comparisonBuilderFactory"
taken from /meta/utility/compare-example.xml:
<import resource="compare.xml"/>
<import resource="path-example.xml"/>
<bean id="meta-utility-comparisonBuilderFactory" class="org.atemsource.atem.utility.compare.ComparisonBuilderFactory">
<property name="beanName"><idref local="meta-utility-comparisonBuilder"></idref></property>
</bean>
<bean id="meta-utility-comparisonBuilder" scope="prototype" class="org.atemsource.atem.utility.compare.ComparisonBuilder">
<property name="attributeFactories">
<list>
<bean class="org.atemsource.atem.utility.compare.builder.SingleAttributeBuilderFactory"/>
<bean class="org.atemsource.atem.utility.compare.builder.MapAttributeBuilderFactory"/>
<bean class="org.atemsource.atem.utility.compare.builder.OrderableCollectionAttributeBuilderFactory"/>
<bean class="org.atemsource.atem.utility.compare.builder.CollectionAttributeBuilderFactory"/>
</list>
</property>
</bean>
###Extending Comparison
By implementing and registering an instance of ComparisonAttributeBuilderFactory the comparison can be customized even further.