Do not pretty-print body class
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractReadyIterator.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.Iterator;
15 import org.opendaylight.yangtools.yang.data.spi.tree.Version;
16
17 abstract class AbstractReadyIterator {
18     final Iterator<ModifiedNode> children;
19     final ModifiedNode node;
20     final ModificationApplyOperation op;
21
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);
27     }
28
29     static AbstractReadyIterator create(final ModifiedNode root, final ModificationApplyOperation operation) {
30         return new RootReadyIterator(root, root.getChildren().iterator(), operation);
31     }
32
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();
42
43             if (grandChildren.isEmpty()) {
44                 // The child is empty, seal it
45                 child.seal(childOp, version);
46                 if (child.getOperation() == LogicalOperation.NONE) {
47                     children.remove();
48                 }
49             } else {
50                 return new NestedReadyIterator(this, child, grandChildren.iterator(), childOp);
51             }
52         }
53
54         // We are done with this node, seal it.
55         node.seal(op, version);
56
57         // Remove from parent if we have one and this is a no-op
58         if (node.getOperation() == LogicalOperation.NONE) {
59             removeFromParent();
60         }
61
62         // Sub-iteration complete, return back to parent
63         return getParent();
64     }
65
66     abstract AbstractReadyIterator getParent();
67
68     abstract void removeFromParent();
69
70     private static final class NestedReadyIterator extends AbstractReadyIterator {
71         private final AbstractReadyIterator parent;
72
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);
77         }
78
79         @Override
80         AbstractReadyIterator getParent() {
81             return parent;
82         }
83
84         @Override
85         void removeFromParent() {
86             parent.children.remove();
87         }
88     }
89
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);
94         }
95
96         @Override
97         AbstractReadyIterator getParent() {
98             return null;
99         }
100
101         @Override
102         void removeFromParent() {
103             // No-op, since root node cannot be removed
104         }
105     }
106 }