2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
16 abstract class AbstractReadyIterator {
17 final Iterator<ModifiedNode> children;
18 final ModifiedNode node;
19 final ModificationApplyOperation op;
21 private AbstractReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children,
22 final ModificationApplyOperation operation) {
23 this.children = Preconditions.checkNotNull(children);
24 this.node = Preconditions.checkNotNull(node);
25 this.op = Preconditions.checkNotNull(operation);
28 static AbstractReadyIterator create(final ModifiedNode root, final ModificationApplyOperation operation) {
29 return new RootReadyIterator(root, root.getChildren().iterator(), operation);
32 final AbstractReadyIterator process(final Version version) {
33 // Walk all child nodes and remove any children which have not
34 // been modified. If a child has children, we need to iterate
35 // through it via re-entering this method on the child iterator.
36 while (children.hasNext()) {
37 final ModifiedNode child = children.next();
38 final Optional<ModificationApplyOperation> childOperation = op.getChild(child.getIdentifier());
39 Preconditions.checkState(childOperation.isPresent(), "Schema for child %s is not present.",
40 child.getIdentifier());
41 final Collection<ModifiedNode> grandChildren = child.getChildren();
42 final ModificationApplyOperation childOp = childOperation.get();
44 if (grandChildren.isEmpty()) {
45 // The child is empty, seal it
46 child.seal(childOp, version);
47 if (child.getOperation() == LogicalOperation.NONE) {
51 return new NestedReadyIterator(this, child, grandChildren.iterator(), childOp);
55 // We are done with this node, seal it.
56 node.seal(op, version);
58 // Remove from parent if we have one and this is a no-op
59 if (node.getOperation() == LogicalOperation.NONE) {
63 // Sub-iteration complete, return back to parent
67 abstract AbstractReadyIterator getParent();
69 abstract void removeFromParent();
71 private static final class NestedReadyIterator extends AbstractReadyIterator {
72 private final AbstractReadyIterator parent;
74 private NestedReadyIterator(final AbstractReadyIterator parent, final ModifiedNode node,
75 final Iterator<ModifiedNode> children, final ModificationApplyOperation operation) {
76 super(node, children, operation);
77 this.parent = Preconditions.checkNotNull(parent);
81 AbstractReadyIterator getParent() {
86 void removeFromParent() {
87 parent.children.remove();
91 private static final class RootReadyIterator extends AbstractReadyIterator {
92 private RootReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children,
93 final ModificationApplyOperation operation) {
94 super(node, children, operation);
98 AbstractReadyIterator getParent() {
103 void removeFromParent() {
104 // No-op, since root node cannot be removed