API Clarity: Documented o.o.y.yang.parser.builder.api
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / api / AbstractDataNodeContainerBuilder.java
index 55610d33b664ccaa1c3a3c55139809a3ca9e7f01..9a86e5ca2f03ff78c9310201cb286d0b7a9dadf3 100644 (file)
@@ -1,18 +1,14 @@
 /*
- * 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.api;
 
-import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
-import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.opendaylight.yangtools.yang.common.QName;
@@ -27,18 +23,18 @@ import org.opendaylight.yangtools.yang.parser.util.YangParseException;
  * Basic implementation of DataNodeContainerBuilder.
  */
 public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder implements DataNodeContainerBuilder {
-    protected QName qname;
+    protected final QName qname;
 
-    protected Map<QName, DataSchemaNode> childNodes = new TreeMap<QName, DataSchemaNode>(Comparators.QNAME_COMP);
-    protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<DataSchemaNodeBuilder>();
+    protected final Set<DataSchemaNode> childNodes = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    protected final Set<DataSchemaNodeBuilder> addedChildNodes = new HashSet<>();
 
-    protected Set<GroupingDefinition> groupings = new TreeSet<GroupingDefinition>(Comparators.SCHEMA_NODE_COMP);
-    protected final Set<GroupingBuilder> addedGroupings = new HashSet<GroupingBuilder>();
+    protected final Set<GroupingDefinition> groupings = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    protected final Set<GroupingBuilder> addedGroupings = new HashSet<>();
 
-    protected Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
+    protected final Set<TypeDefinition<?>> typedefs = new TreeSet<>(Comparators.SCHEMA_NODE_COMP);
     protected final Set<TypeDefinitionBuilder> addedTypedefs = new HashSet<>();
 
-    protected Set<UsesNode> usesNodes = new HashSet<>();
+    protected final Set<UsesNode> usesNodes = new HashSet<>();
     protected final Set<UsesNodeBuilder> addedUsesNodes = new HashSet<>();
 
     protected AbstractDataNodeContainerBuilder(final String moduleName, final int line, final QName qname) {
@@ -51,12 +47,8 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         return qname;
     }
 
-    @Override
-    public Collection<DataSchemaNode> getChildNodes() {
-        if (childNodes == null) {
-            return Collections.emptySet();
-        }
-        return childNodes.values();
+    public Set<DataSchemaNode> getChildNodes() {
+        return childNodes;
     }
 
     @Override
@@ -76,27 +68,33 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
 
     @Override
     public void addChildNode(DataSchemaNodeBuilder child) {
-        String childName = child.getQName().getLocalName();
+        QName childName = child.getQName();
         for (DataSchemaNodeBuilder addedChildNode : addedChildNodes) {
-            if (addedChildNode.getQName().getLocalName().equals(childName)) {
-                throw new YangParseException(child.getModuleName(), child.getLine(), "Can not add '" + child + "' to '"
-                        + this + "' in module '" + moduleName + "': node with same name already declared at line "
-                        + addedChildNode.getLine());
+            if (addedChildNode.getQName().equals(childName)) {
+                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
+    public void addChildNodeToContext(DataSchemaNodeBuilder child) {
+        addedChildNodes.add(child);
+    }
+
     @Override
     public void addChildNode(DataSchemaNode child) {
         QName childName = child.getQName();
-        for (QName qname : childNodes.keySet()) {
-            if (qname.equals(childName)) {
-                throw new YangParseException(moduleName, line, "Can not add '" + child + "' to '" + this
-                        + "' in module '" + moduleName + "': node with same name already declared");
+        for (DataSchemaNode childNode : childNodes) {
+            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()));
             }
         }
-        childNodes.put(child.getQName(), child);
+        childNodes.add(child);
     }
 
     @Override
@@ -107,34 +105,36 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         return groupings;
     }
 
-    public void setGroupings(final Set<GroupingDefinition> groupings) {
-        this.groupings = groupings;
-    }
-
+    @Override
     public Set<GroupingBuilder> getGroupingBuilders() {
         return addedGroupings;
     }
 
     @Override
     public void addGrouping(GroupingBuilder grouping) {
-        String groupingName = grouping.getQName().getLocalName();
+        QName groupingName = grouping.getQName();
         for (GroupingBuilder addedGrouping : addedGroupings) {
-            if (addedGrouping.getQName().getLocalName().equals(groupingName)) {
-                throw new YangParseException(grouping.getModuleName(), grouping.getLine(), "Can not add '" + grouping
-                        + "': grouping with same name already declared in module '" + moduleName + "' at line "
-                        + addedGrouping.getLine());
+            if (addedGrouping.getQName().equals(groupingName)) {
+                throw new YangParseException(grouping.getModuleName(), grouping.getLine(), String.format(
+                        "Can not add '%s': grouping with same name already declared in module '%s' at line %d",
+                        grouping, getModuleName(), addedGrouping.getLine()));
             }
         }
         addedGroupings.add(grouping);
     }
 
     @Override
-    public Set<UsesNodeBuilder> getUsesNodes() {
-        return addedUsesNodes;
+    public Set<TypeDefinition<?>> getTypeDefinitions() {
+        return typedefs;
+    }
+
+    public Set<UsesNode> getUsesNodes() {
+        return usesNodes;
     }
 
-    public void setUsesnodes(final Set<UsesNode> usesNodes) {
-        this.usesNodes = usesNodes;
+    @Override
+    public Set<UsesNodeBuilder> getUsesNodeBuilders() {
+        return addedUsesNodes;
     }
 
     @Override
@@ -142,4 +142,22 @@ public abstract class AbstractDataNodeContainerBuilder extends AbstractBuilder i
         addedUsesNodes.add(usesNode);
     }
 
+    protected static DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, QName name) {
+        for (DataSchemaNode node : childNodes) {
+            if (node.getQName().equals(name)) {
+                return node;
+            }
+        }
+        return null;
+    }
+
+    protected static DataSchemaNode getChildNode(Set<DataSchemaNode> childNodes, String name) {
+        for (DataSchemaNode node : childNodes) {
+            if (node.getQName().getLocalName().equals(name)) {
+                return node;
+            }
+        }
+        return null;
+    }
+
 }