* {@inheritDoc}
*/
public void run() {
try {
EntityContainerComparator comparator;
ChangeContainer changeContainer0 = null;
ChangeContainer changeContainer1 = null;
// Create a comparator for comparing two entities by type and identifier.
comparator = new EntityContainerComparator(new EntityByTypeThenIdThenVersionComparator());
// We can't get meaningful data from the initialize data on the
// input streams, so pass empty meta data to the sink and discard
// the input meta data.
postbox0.outputInitialize();
postbox1.outputInitialize();
changeSink.initialize(Collections.<String, Object>emptyMap());
// We continue in the comparison loop while both sources still have data.
while (
(changeContainer0 != null || postbox0.hasNext())
&& (changeContainer1 != null || postbox1.hasNext())) {
long comparisonResult;
// Get the next input data where required.
if (changeContainer0 == null) {
changeContainer0 = postbox0.getNext();
}
if (changeContainer1 == null) {
changeContainer1 = postbox1.getNext();
}
// Compare the two entities.
comparisonResult =
comparator.compare(changeContainer0.getEntityContainer(), changeContainer1.getEntityContainer());
if (comparisonResult < 0) {
// Entity 0 doesn't exist on the other source and can be
// sent straight through.
changeSink.process(changeContainer0);
changeContainer0 = null;
} else if (comparisonResult > 0) {
// Entity 1 doesn't exist on the other source and can be
// sent straight through.
changeSink.process(changeContainer1);
changeContainer1 = null;
} else {
// The entity exists on both sources so we must resolve the conflict.
if (conflictResolutionMethod.equals(ConflictResolutionMethod.Timestamp)) {
int timestampComparisonResult;
timestampComparisonResult =
changeContainer0.getEntityContainer().getEntity().getTimestamp()
.compareTo(changeContainer1.getEntityContainer().getEntity().getTimestamp());
if (timestampComparisonResult < 0) {
changeSink.process(changeContainer1);
} else if (timestampComparisonResult > 0) {
changeSink.process(changeContainer0);
} else {
// If both have identical timestamps, use the second source.
changeSink.process(changeContainer1);
}
} else if (conflictResolutionMethod.equals(ConflictResolutionMethod.LatestSource)) {
changeSink.process(changeContainer1);
} else if (conflictResolutionMethod.equals(ConflictResolutionMethod.Version)) {
int version0 = changeContainer0.getEntityContainer().getEntity().getVersion();
int version1 = changeContainer1.getEntityContainer().getEntity().getVersion();
if (version0 < version1) {
changeSink.process(changeContainer1);
} else if (version0 > version1) {
changeSink.process(changeContainer0);
} else {