+ private Optional<TreeNode> metadataFromSnapshot(@Nonnull final PathArgument child) {
+ return original.isPresent() ? original.get().getChild(child) : Optional.<TreeNode>absent();
+ }
+
+ private Optional<TreeNode> metadataFromData(@Nonnull final PathArgument child, final Version modVersion) {
+ if (writtenOriginal == null) {
+ // Lazy instantiation, as we do not want do this for all writes. We are using the modification's version
+ // here, as that version is what the SchemaAwareApplyOperation will see when dealing with the resulting
+ // modifications.
+ writtenOriginal = TreeNodeFactory.createTreeNode(value, modVersion);
+ }
+
+ return writtenOriginal.getChild(child);
+ }
+
+ /**
+ * Determine the base tree node we are going to apply the operation to. This is not entirely trivial because
+ * both DELETE and WRITE operations unconditionally detach their descendants from the original snapshot, so we need
+ * to take the current node's operation into account.
+ *
+ * @param child Child we are looking to modify
+ * @param modVersion Version allocated by the calling {@link InMemoryDataTreeModification}
+ * @return Before-image tree node as observed by that child.
+ */
+ private Optional<TreeNode> findOriginalMetadata(@Nonnull final PathArgument child, final Version modVersion) {
+ switch (operation) {
+ case DELETE:
+ // DELETE implies non-presence
+ return Optional.absent();
+ case NONE:
+ case TOUCH:
+ case MERGE:
+ return metadataFromSnapshot(child);
+ case WRITE:
+ // WRITE implies presence based on written data
+ return metadataFromData(child, modVersion);
+ }
+
+ throw new IllegalStateException("Unhandled node operation " + operation);
+ }
+