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 1c5022368a9e065a1cbea68c0aec2c4e1bd4f320..22b2a98a4821cd379ec50dcf084ea77cbc3f2b86 100644 (file)
@@ -1,21 +1,23 @@
 /*
- * 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.net.URI;
-import java.util.Date;
+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;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DocumentedNode;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
@@ -30,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;
 
-    protected 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<>();
@@ -44,31 +49,34 @@ 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);
         this.qname = qname;
     }
 
-    public AbstractDocumentedDataNodeContainerBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final DataNodeContainer base) {
+    protected AbstractDocumentedDataNodeContainerBuilder(final String moduleName, final int line, final QName qname, final SchemaPath path, final DataNodeContainer base) {
         super(moduleName, line);
         this.qname = qname;
 
-        URI ns = qname.getNamespace();
-        Date rev = qname.getRevision();
-        String pref = qname.getPrefix();
-
         // We do copy of child nodes with namespace change
         // FIXME: Copy should be part of builder API so impl we prevent
         // cyclic dependencies and each builder carries its own semantic for copy.
-        addedChildNodes.addAll(BuilderUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, ns, rev, pref));
-        addedGroupings.addAll(BuilderUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, ns, rev, pref));
-        addedTypedefs.addAll(BuilderUtils.wrapTypedefs(moduleName, line, base, path, ns, rev, pref));
+        addedChildNodes.addAll(BuilderUtils.wrapChildNodes(moduleName, line, base.getChildNodes(), path, qname));
+        addedGroupings.addAll(BuilderUtils.wrapGroupings(moduleName, line, base.getGroupings(), path, qname));
+        addedTypedefs.addAll(BuilderUtils.wrapTypedefs(moduleName, line, base, path, qname));
         // FIXME: unkownSchemaNodes should be available in DataNodeContainer
-//        addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName, line, base.getUnknownSchemaNodes(), path, ns,
-//                rev, pref));
+        // addedUnknownNodes.addAll(BuilderUtils.wrapUnknownNodes(moduleName,
+        // line, base.getUnknownSchemaNodes(), path, qname));
         usesNodes.addAll(base.getUses());
+
+        if (base instanceof DocumentedNode) {
+            DocumentedNode node = (DocumentedNode) base;
+            setDescription(node.getDescription());
+            setReference(node.getReference());
+            setStatus(node.getStatus());
+        }
     }
 
     @Override
@@ -82,7 +90,7 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     }
 
     @Override
-    public final Set<DataSchemaNodeBuilder> getChildNodeBuilders() {
+    public final List<DataSchemaNodeBuilder> getChildNodeBuilders() {
         return addedChildNodes;
     }
 
@@ -98,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
@@ -118,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
@@ -139,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) {
@@ -162,7 +181,7 @@ public abstract class AbstractDocumentedDataNodeContainerBuilder extends Abstrac
     }
 
     @Override
-    public final Set<UsesNodeBuilder> getUsesNodeBuilders() {
+    public final List<UsesNodeBuilder> getUsesNodeBuilders() {
         return addedUsesNodes;
     }
 
@@ -214,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;
-    }
-
-
 }