if (changeType == ListEvent.INSERT) {
final E inserted = get(changeIndex);
this.observedElements.add(changeIndex, inserted);
// connect a listener to the freshly inserted element
final EventListener listener = this.connectElement(inserted);
// record the listener in the registry
this.registerListener(changeIndex, listener, false);
// unregister a listener on the deleted object
} else if (changeType == ListEvent.DELETE) {
// try to get the previous value through the ListEvent
E deleted = listChanges.getOldValue();
E deletedElementFromPrivateCopy = this.observedElements.remove(changeIndex);
// if the ListEvent could give us the previous value, use the value from our private copy of the source
if (deleted == ListEvent.UNKNOWN_VALUE)
deleted = deletedElementFromPrivateCopy;
// remove the listener from the registry
final EventListener listener = this.unregisterListener(changeIndex);
// disconnect the listener from the freshly deleted element
this.disconnectElement(deleted, listener);
// register/unregister listeners if the value at the changeIndex is now a different object
} else if (changeType == ListEvent.UPDATE) {
E previousValue = listChanges.getOldValue();
// if the ListEvent could give us the previous value, use the value from our private copy of the source
if (previousValue == ListEvent.UNKNOWN_VALUE)
previousValue = this.observedElements.get(changeIndex);
final E newValue = get(changeIndex);
// if a different object is present at the index
if (newValue != previousValue) {
this.observedElements.set(changeIndex, newValue);
// disconnect the listener from the previous element at the index
this.disconnectElement(previousValue, this.getListener(changeIndex));
// connect the listener to the new element at the index
final EventListener listener = this.connectElement(newValue);
// replace the old listener in the registry with the new listener for the new element
this.registerListener(changeIndex, listener, true);
}
}
}