MutableDocument mutable = asMutableDocument();
for (Operation operation : (DocumentChanges)changes) {
operation.replay(mutable);
if (observer != null) {
if (operation instanceof SetValueOperation) {
SetValueOperation op = (SetValueOperation)operation;
observer.setArrayValue(op.getParentPath(), newEntry(op.getIndex(), op.getValue()));
} else if (operation instanceof AddValueOperation) {
AddValueOperation op = (AddValueOperation)operation;
if (op.getActualIndex() != -1) {
observer.addArrayValue(op.getParentPath(), newEntry(op.getActualIndex(), op.getValue()));
}
} else if (operation instanceof AddValueIfAbsentOperation) {
AddValueIfAbsentOperation op = (AddValueIfAbsentOperation)operation;
if (op.isAdded()) {
observer.addArrayValue(op.getParentPath(), newEntry(op.getIndex(), op.getValue()));
}
} else if (operation instanceof RemoveValueOperation) {
RemoveValueOperation op = (RemoveValueOperation)operation;
if (op.getActualIndex() != -1) {
observer.removeArrayValue(op.getParentPath(), newEntry(op.getActualIndex(), op.getRemovedValue()));
}
} else if (operation instanceof RemoveAtIndexOperation) {
RemoveAtIndexOperation op = (RemoveAtIndexOperation)operation;
observer.removeArrayValue(op.getParentPath(), newEntry(op.getIndex(), op.getRemovedValue()));
} else if (operation instanceof RetainAllValuesOperation) {
RetainAllValuesOperation op = (RetainAllValuesOperation)operation;
for (Array.Entry entry : op.getRemovedEntries()) {
observer.removeArrayValue(op.getParentPath(), entry);
}
} else if (operation instanceof RemoveAllValuesOperation) {
RemoveAllValuesOperation op = (RemoveAllValuesOperation)operation;
for (Array.Entry entry : op.getRemovedEntries()) {
observer.removeArrayValue(op.getParentPath(), entry);
}
} else if (operation instanceof ClearOperation) {
ClearOperation op = (ClearOperation)operation;
observer.clear(op.getParentPath());
} else if (operation instanceof PutOperation) {
PutOperation op = (PutOperation)operation;
observer.put(op.getParentPath(), op.getFieldName(), op.getNewValue());
} else if (operation instanceof PutIfAbsentOperation) {
PutIfAbsentOperation op = (PutIfAbsentOperation)operation;
if (op.isApplied()) {
observer.put(op.getParentPath(), op.getFieldName(), op.getNewValue());
}
} else if (operation instanceof RemoveOperation) {
RemoveOperation op = (RemoveOperation)operation;
if (op.isRemoved()) {
observer.remove(op.getParentPath(), op.getFieldName());
}
}
}
}
}