BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / util / AbstractDocumentedDataNodeContainerBuilder.java
index 6877538607c62f64a4c7db3005b3729aaab4a96a..22b2a98a4821cd379ec50dcf084ea77cbc3f2b86 100644 (file)
@@ -1,15 +1,18 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 package org.opendaylight.yangtools.yang.parser.builder.util;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.TreeMap;
 import java.util.TreeSet;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
@@ -29,12 +32,15 @@ import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
 /**
  * Basic implementation of DataNodeContainerBuilder.
+ *
+ * @deprecated Pre-Beryllium implementation, scheduled for removal.
  */
+@Deprecated
 public abstract class AbstractDocumentedDataNodeContainerBuilder extends AbstractDocumentedNodeBuilder implements DataNodeContainerBuilder {
     protected final QName qname;
 
-    private final Map<QName, DataSchemaNode> childNodes = new TreeMap<>();
-    private final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
+    private final Map<QName, DataSchemaNode> childNodes = new LinkedHashMap<>();
+    private final List<DataSchemaNodeBuilder> addedChildNodes = new ArrayList<>();
 
     private final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
     private final Set<GroupingBuilder> addedGroupings = new HashSet<>();
@@ -43,7 +49,7 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     private final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
 
     private final Set<UsesNode> usesNodes = new HashSet<>();
-    private final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
+    private final List<UsesNodeBuilder> addedUsesNodes = new ArrayList<>();
 
     protected AbstractDocumentedDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
         super(moduleName, line);
@@ -84,7 +90,7 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     }
 
     @Override
-    public final Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
+    public final List<DataSchemaNodeBuilder> getChildNodeBuilders() {
         return addedChildNodes;
     }
 
@@ -100,15 +106,28 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
 
     @Override
     public final void addChildNode(final DataSchemaNodeBuilder child) {
-        QName childName = child.getQName();
+        checkIsPresent(child);
+        addedChildNodes.add(child);
+    }
+
+    @Override
+    public final void addChildNode(final int index, final DataSchemaNodeBuilder child) {
+        checkIsPresent(child);
+        if (index > addedChildNodes.size()) {
+            addedChildNodes.add(child);
+        } else {
+            addedChildNodes.add(index, child);
+        }
+    }
+
+    private void checkIsPresent(final DataSchemaNodeBuilder child) {
         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
-            if (addedChildNode.getQName().equals(childName)) {
+            if (addedChildNode.getQName().equals(child.getQName())) {
                 throw new YangParseException(child.getModuleName(), child.getLine(), String.format(
                         "Can not add '%s' to '%s' in module '%s': node with same name already declared at line %d",
                         child, this, getModuleName(), addedChildNode.getLine()));
             }
         }
-        addedChildNodes.add(child);
     }
 
     @Override
@@ -120,14 +139,12 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     public final void addChildNode(final DataSchemaNode child) {
         checkNotSealed();
         QName childName = child.getQName();
-        for (DataSchemaNode childNode : childNodes.values()) {
-            if (childNode.getQName().equals(childName)) {
-                throw new YangParseException(getModuleName(), getLine(), String.format(
-                        "Can not add '%s' to '%s' in module '%s': node with same name already declared", child, this,
-                        getModuleName()));
-            }
+        if (childNodes.containsKey(childName)) {
+            throw new YangParseException(getModuleName(), getLine(), String.format(
+                    "Can not add '%s' to '%s' in module '%s': node with same name already declared", child, this,
+                    getModuleName()));
         }
-        childNodes.put(child.getQName(), child);
+        childNodes.put(childName, child);
     }
 
     @Override
@@ -141,7 +158,7 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     }
 
     @Override
-    public final void addGrouping(final GroupingBuilder grouping) {
+    public void addGrouping(final GroupingBuilder grouping) {
         checkNotSealed();
         QName groupingName = grouping.getQName();
         for (GroupingBuilder addedGrouping : addedGroupings) {
@@ -164,7 +181,7 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     }
 
     @Override
-    public final Set<UsesNodeBuilder> getUsesNodeBuilders() {
+    public final List<UsesNodeBuilder> getUsesNodeBuilders() {
         return addedUsesNodes;
     }
 
@@ -216,25 +233,4 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
         }
     }
 
-    @Deprecated
-    protected static DataSchemaNode getChildNode(final Set<DataSchemaNode> childNodes, final QName name) {
-        for (DataSchemaNode node : childNodes) {
-            if (node.getQName().equals(name)) {
-                return node;
-            }
-        }
-        return null;
-    }
-
-    @Deprecated
-    protected static DataSchemaNode getChildNode(final Set<DataSchemaNode> childNodes, final String name) {
-        for (DataSchemaNode node : childNodes) {
-            if (node.getQName().getLocalName().equals(name)) {
-                return node;
-            }
-        }
-        return null;
-    }
-
-
 }