Merge "Removed unused dependency."
[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 com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.Iterator;
13
14 abstract class AbstractReadyIterator {
15     final Iterator<ModifiedNode> children;
16     final ModifiedNode node;
17
18     private AbstractReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children) {
19         this.children = Preconditions.checkNotNull(children);
20         this.node = Preconditions.checkNotNull(node);
21     }
22
23     static AbstractReadyIterator create(final ModifiedNode root) {
24         return new RootReadyIterator(root, root.getChildren().iterator());
25     }
26
27     final AbstractReadyIterator process() {
28         // Walk all child nodes and remove any children which have not
29         // been modified. If a child
30         while (children.hasNext()) {
31             final ModifiedNode child = children.next();
32             final Collection<ModifiedNode> grandChildren = child.getChildren();
33             if (grandChildren.isEmpty()) {
34                 child.seal();
35                 if (child.getOperation() == LogicalOperation.NONE) {
36                     children.remove();
37                 }
38             } else {
39                 return new NestedReadyIterator(this, child, grandChildren.iterator());
40             }
41         }
42
43         node.seal();
44
45         // Remove from parent if we have one and this is a no-op
46         if (node.getOperation() == LogicalOperation.NONE) {
47             removeFromParent();
48         }
49         return getParent();
50     }
51
52     abstract AbstractReadyIterator getParent();
53     abstract void removeFromParent();
54
55     private static final class NestedReadyIterator extends AbstractReadyIterator {
56         private final AbstractReadyIterator parent;
57
58         private NestedReadyIterator(final AbstractReadyIterator parent, final ModifiedNode node, final Iterator<ModifiedNode> children) {
59             super(node, children);
60             this.parent = Preconditions.checkNotNull(parent);
61         }
62
63         @Override
64         AbstractReadyIterator getParent() {
65             return parent;
66         }
67
68         @Override
69         void removeFromParent() {
70             parent.children.remove();
71         }
72     }
73
74     private static final class RootReadyIterator extends AbstractReadyIterator {
75         private RootReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children) {
76             super(node, children);
77         }
78
79         @Override
80         AbstractReadyIterator getParent() {
81             return null;
82         }
83
84         @Override
85         void removeFromParent() {
86             // No-op, since root node cannot be removed
87         }
88     }
89
90 }