Do not use Optional in NormalizedNodeWrapper
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / transformer / NormalizedNodePruner.java
index 1577f0914706c75a874f8a68c94c6630a0a7fec9..cacef95d03506243e322bcb7486ad9617fdbf90e 100644 (file)
@@ -9,11 +9,10 @@ package org.opendaylight.controller.cluster.datastore.node.utils.transformer;
 
 import static com.google.common.base.Preconditions.checkState;
 
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Optional;
 import java.net.URI;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import java.util.NoSuchElementException;
 import javax.xml.transform.dom.DOMSource;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -43,7 +42,7 @@ public class NormalizedNodePruner implements NormalizedNodeStreamWriter {
 
     public static final URI BASE_NAMESPACE = URI.create("urn:ietf:params:xml:ns:netconf:base:1.0");
 
-    private final SimpleStack<NormalizedNodeBuilderWrapper> stack = new SimpleStack<>();
+    private final Deque<NormalizedNodeBuilderWrapper> stack = new ArrayDeque<>();
     private final DataSchemaContextNode<?> nodePathSchemaNode;
 
     private NormalizedNode<?, ?> normalizedNode;
@@ -191,11 +190,14 @@ public class NormalizedNodePruner implements NormalizedNodeStreamWriter {
     public void endNode() {
         checkNotSealed();
 
-        NormalizedNodeBuilderWrapper child = stack.pop();
-
-        checkState(child != null, "endNode called on an empty stack");
+        final NormalizedNodeBuilderWrapper child;
+        try {
+            child = stack.pop();
+        } catch (NoSuchElementException e) {
+            throw new IllegalStateException("endNode called on an empty stack", e);
+        }
 
-        if (!child.getSchema().isPresent()) {
+        if (child.getSchema() == null) {
             LOG.debug("Schema not found for {}", child.identifier());
             return;
         }
@@ -229,7 +231,8 @@ public class NormalizedNodePruner implements NormalizedNodeStreamWriter {
     }
 
     private static boolean hasValidSchema(final QName name, final NormalizedNodeBuilderWrapper parent) {
-        boolean valid = parent.getSchema().isPresent() && parent.getSchema().get().getChild(name) != null;
+        final DataSchemaContextNode<?> parentSchema = parent.getSchema();
+        final boolean valid = parentSchema != null && parentSchema.getChild(name) != null;
         if (!valid) {
             LOG.debug("Schema not found for {}", name);
         }
@@ -239,14 +242,13 @@ public class NormalizedNodePruner implements NormalizedNodeStreamWriter {
 
     private NormalizedNodeBuilderWrapper addBuilder(final NormalizedNodeContainerBuilder<?, ?, ?, ?> builder,
             final PathArgument identifier) {
-        final Optional<DataSchemaContextNode<?>> schemaNode;
-        NormalizedNodeBuilderWrapper parent = stack.peek();
-        if (parent == null) {
-            schemaNode = Optional.fromNullable(nodePathSchemaNode);
-        } else if (parent.getSchema().isPresent()) {
-            schemaNode = Optional.fromNullable(parent.getSchema().get().getChild(identifier));
+        final DataSchemaContextNode<?> schemaNode;
+        final NormalizedNodeBuilderWrapper parent = stack.peek();
+        if (parent != null) {
+            final DataSchemaContextNode<?> parentSchema = parent.getSchema();
+            schemaNode = parentSchema == null ? null : parentSchema.getChild(identifier);
         } else {
-            schemaNode = Optional.absent();
+            schemaNode = nodePathSchemaNode;
         }
 
         NormalizedNodeBuilderWrapper wrapper = new NormalizedNodeBuilderWrapper(builder, identifier, schemaNode);
@@ -266,32 +268,4 @@ public class NormalizedNodePruner implements NormalizedNodeStreamWriter {
 
         return schemaNode;
     }
-
-    @VisibleForTesting
-    static class SimpleStack<E> {
-        List<E> stack = new LinkedList<>();
-
-        void push(final E element) {
-            stack.add(element);
-        }
-
-        E pop() {
-            if (size() == 0) {
-                return null;
-            }
-            return stack.remove(stack.size() - 1);
-        }
-
-        E peek() {
-            if (size() == 0) {
-                return null;
-            }
-
-            return stack.get(stack.size() - 1);
-        }
-
-        int size() {
-            return stack.size();
-        }
-    }
 }