- protected AbstractNode(final ModifiedNode mod,
- final TreeNode oldMeta, final TreeNode newMeta) {
- this.newMeta = newMeta;
- this.oldMeta = oldMeta;
- this.mod = Preconditions.checkNotNull(mod);
- }
-
- protected final ModifiedNode getMod() {
- return mod;
- }
-
- protected final TreeNode getNewMeta() {
- return newMeta;
- }
-
- protected final TreeNode getOldMeta() {
- return oldMeta;
- }
-
- private static final TreeNode childMeta(final TreeNode parent, final PathArgument id) {
- if (parent != null) {
- return parent.getChild(id).orNull();
- } else {
- return null;
- }
- }
-
- private DataTreeCandidateNode childNode(final ModifiedNode input) {
- final PathArgument id = input.getIdentifier();
- return new ChildNode(input, childMeta(oldMeta, id), childMeta(newMeta, id));
- }
-
- @Override
- public Collection<DataTreeCandidateNode> getChildNodes() {
- return Collections2.transform(mod.getChildren(), new Function<ModifiedNode, DataTreeCandidateNode>() {
- @Override
- public DataTreeCandidateNode apply(final ModifiedNode input) {
- return childNode(input);
- }
- });
- }
-
- @Override
- public ModificationType getModificationType() {
- switch (mod.getOperation()) {
- case DELETE:
- return ModificationType.DELETE;
- case MERGE:
- // Merge into non-existing data is a write
- if (oldMeta == null) {
- return ModificationType.WRITE;
- }
-
- // Data-based checks to narrow down types
- final NormalizedNode<?, ?> data = newMeta.getData();
-
- // leaf or anyxml are always written
- if (!(data instanceof NormalizedNodeContainer)) {
- return ModificationType.WRITE;
- }
-
- // Unkeyed collections are always written
- if (data instanceof UnkeyedListNode || data instanceof OrderedMapNode || data instanceof OrderedLeafSetNode) {
- return ModificationType.WRITE;
- }
-
- // Everything else is subtree modified
- return ModificationType.SUBTREE_MODIFIED;
- case TOUCH:
- return ModificationType.SUBTREE_MODIFIED;
- case NONE:
- return ModificationType.UNMODIFIED;
- case WRITE:
- return ModificationType.WRITE;
- }
-
- throw new IllegalStateException("Unhandled internal operation " + mod.getOperation());
- }
-
- private Optional<NormalizedNode<?, ?>> optionalData(final TreeNode meta) {
- if (meta != null) {
- return Optional.<NormalizedNode<?,?>>of(meta.getData());
- } else {
- return Optional.absent();
- }
- }
-
- @Override
- public Optional<NormalizedNode<?, ?>> getDataAfter() {
- return optionalData(newMeta);
- }
-
- @Override
- public Optional<NormalizedNode<?, ?>> getDataBefore() {
- return optionalData(oldMeta);
- }
-
- @Override
- public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
- final Optional<ModifiedNode> childMod = mod.getChild(identifier);
- if(childMod.isPresent()) {
- return childNode(mod);
- }
- return null;
- }
- }
-
- private static final class ChildNode extends AbstractNode {
- public ChildNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {
- super(mod, oldMeta, newMeta);
- }
-
- @Override
- public PathArgument getIdentifier() {
- return getMod().getIdentifier();
- }
- }
-
- private static final class RootNode extends AbstractNode {
- public RootNode(final ModifiedNode mod, final TreeNode oldMeta, final TreeNode newMeta) {