Introduce RequiredElementCountException
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AbstractReadyIterator.java
index e7e80097dc3e2683a6c6d66dcdbf444e7fe1ad0b..160ce6609dc3855f682f50f6afa4225d83cab2a0 100644 (file)
@@ -7,10 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.Optional;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.spi.Version;
 
 abstract class AbstractReadyIterator {
     final Iterator<ModifiedNode> children;
@@ -19,45 +22,51 @@ abstract class AbstractReadyIterator {
 
     private AbstractReadyIterator(final ModifiedNode node, final Iterator<ModifiedNode> children,
             final ModificationApplyOperation operation) {
-        this.children = Preconditions.checkNotNull(children);
-        this.node = Preconditions.checkNotNull(node);
-        this.op = Preconditions.checkNotNull(operation);
+        this.children = requireNonNull(children);
+        this.node = requireNonNull(node);
+        this.op = requireNonNull(operation);
     }
 
     static AbstractReadyIterator create(final ModifiedNode root, final ModificationApplyOperation operation) {
         return new RootReadyIterator(root, root.getChildren().iterator(), operation);
     }
 
-    final AbstractReadyIterator process() {
+    final AbstractReadyIterator process(final Version version) {
         // Walk all child nodes and remove any children which have not
-        // been modified. If a child
+        // been modified. If a child has children, we need to iterate
+        // through it via re-entering this method on the child iterator.
         while (children.hasNext()) {
             final ModifiedNode child = children.next();
             final Optional<ModificationApplyOperation> childOperation = op.getChild(child.getIdentifier());
-            Preconditions.checkState(childOperation.isPresent(), "Schema for child %s is not present.",
-                    child.getIdentifier());
+            checkState(childOperation.isPresent(), "Schema for child %s is not present.", child.getIdentifier());
             final Collection<ModifiedNode> grandChildren = child.getChildren();
-            if (grandChildren.isEmpty()) {
+            final ModificationApplyOperation childOp = childOperation.get();
 
-                child.seal(childOperation.get());
+            if (grandChildren.isEmpty()) {
+                // The child is empty, seal it
+                child.seal(childOp, version);
                 if (child.getOperation() == LogicalOperation.NONE) {
                     children.remove();
                 }
             } else {
-                return new NestedReadyIterator(this, child, grandChildren.iterator(), childOperation.get());
+                return new NestedReadyIterator(this, child, grandChildren.iterator(), childOp);
             }
         }
 
-        node.seal(op);
+        // We are done with this node, seal it.
+        node.seal(op, version);
 
         // Remove from parent if we have one and this is a no-op
         if (node.getOperation() == LogicalOperation.NONE) {
             removeFromParent();
         }
+
+        // Sub-iteration complete, return back to parent
         return getParent();
     }
 
     abstract AbstractReadyIterator getParent();
+
     abstract void removeFromParent();
 
     private static final class NestedReadyIterator extends AbstractReadyIterator {
@@ -66,7 +75,7 @@ abstract class AbstractReadyIterator {
         private NestedReadyIterator(final AbstractReadyIterator parent, final ModifiedNode node,
                 final Iterator<ModifiedNode> children, final ModificationApplyOperation operation) {
             super(node, children, operation);
-            this.parent = Preconditions.checkNotNull(parent);
+            this.parent = requireNonNull(parent);
         }
 
         @Override
@@ -96,5 +105,4 @@ abstract class AbstractReadyIterator {
             // No-op, since root node cannot be removed
         }
     }
-
 }
\ No newline at end of file