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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collection;
14 import java.util.Iterator;
15 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
17 abstract class AbstractReadyIterator {
18 final Iterator<ModifiedNode> children;
19 final ModifiedNode node;
20 final ModificationApplyOperation op;
22 private AbstractReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children,
23 final ModificationApplyOperation operation) {
24 this.children = requireNonNull(children);
25 this.node = requireNonNull(node);
26 this.op = requireNonNull(operation);
29 static AbstractReadyIterator create(final ModifiedNode root, final ModificationApplyOperation operation) {
30 return new RootReadyIterator(root, root.getChildren().iterator(), operation);
33 final AbstractReadyIterator process(final Version version) {
34 // Walk all child nodes and remove any children which have not
35 // been modified. If a child has children, we need to iterate
36 // through it via re-entering this method on the child iterator.
37 while (children.hasNext()) {
38 final ModifiedNode child = children.next();
39 final ModificationApplyOperation childOp = op.childByArg(child.getIdentifier());
40 checkState(childOp != null, "Schema for child %s is not present.", child.getIdentifier());
41 final Collection<ModifiedNode> grandChildren = child.getChildren();
43 if (grandChildren.isEmpty()) {
44 // The child is empty, seal it
45 child.seal(childOp, version);
46 if (child.getOperation() == LogicalOperation.NONE) {
50 return new NestedReadyIterator(this, child, grandChildren.iterator(), childOp);
54 // We are done with this node, seal it.
55 node.seal(op, version);
57 // Remove from parent if we have one and this is a no-op
58 if (node.getOperation() == LogicalOperation.NONE) {
62 // Sub-iteration complete, return back to parent
66 abstract AbstractReadyIterator getParent();
68 abstract void removeFromParent();
70 private static final class NestedReadyIterator extends AbstractReadyIterator {
71 private final AbstractReadyIterator parent;
73 private NestedReadyIterator(final AbstractReadyIterator parent, final ModifiedNode node,
74 final Iterator<ModifiedNode> children, final ModificationApplyOperation operation) {
75 super(node, children, operation);
76 this.parent = requireNonNull(parent);
80 AbstractReadyIterator getParent() {
85 void removeFromParent() {
86 parent.children.remove();
90 private static final class RootReadyIterator extends AbstractReadyIterator {
91 private RootReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children,
92 final ModificationApplyOperation operation) {
93 super(node, children, operation);
97 AbstractReadyIterator getParent() {
102 void removeFromParent() {
103 // No-op, since root node cannot be removed