Merge "BUG-582: do not use linear search of child nodes"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / AugmentationSchemaBuilderImpl.java
index 224c45fcda060f1ba1904b66f2914599d2af2b79..add657ea568cc9380141ea2d40d52dd173079002 100644 (file)
@@ -11,6 +11,7 @@ import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -43,6 +44,7 @@ import com.google.common.collect.ImmutableSet;
 
 public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContainerBuilder implements
         AugmentationSchemaBuilder {
+    private final int order;
     private AugmentationSchemaImpl instance;
     private String whenCondition;
 
@@ -57,8 +59,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
     private boolean resolved;
     private AugmentationSchemaBuilder copyOf;
 
-    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr) {
+    public AugmentationSchemaBuilderImpl(final String moduleName, final int line, final String augmentTargetStr, final int order) {
         super(moduleName, line, null);
+        this.order = order;
         this.augmentTargetStr = augmentTargetStr;
         targetPath = ParserUtils.parseXPathString(augmentTargetStr);
     }
@@ -75,7 +78,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
 
     @Override
     public void addGrouping(final GroupingBuilder grouping) {
-        throw new YangParseException(moduleName, line, "augment can not contains grouping statement");
+        throw new YangParseException(getModuleName(), grouping.getLine(), "augment can not contains grouping statement");
     }
 
     @Override
@@ -89,7 +92,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             return instance;
         }
 
-        instance = new AugmentationSchemaImpl(targetPath);
+        instance = new AugmentationSchemaImpl(targetPath, order);
 
         instance.description = description;
         instance.reference = reference;
@@ -109,7 +112,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             for (QName name : parsedPath) {
                 newPath.add(new QName(mb.getNamespace(), mb.getRevision(), name.getPrefix(), name.getLocalName()));
             }
-            instance.targetPath = new SchemaPath(newPath, false);
+            instance.targetPath = SchemaPath.create(newPath, false);
         } else {
             instance.targetPath = targetNodeSchemaPath;
         }
@@ -128,9 +131,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
 
         // CHILD NODES
         for (DataSchemaNodeBuilder node : addedChildNodes) {
-            childNodes.add(node.build());
+            childNodes.put(node.getQName(), node.build());
         }
-        instance.childNodes = ImmutableSet.copyOf(childNodes);
+        instance.childNodes = ImmutableSet.copyOf(childNodes.values());
 
         // USES
         for (UsesNodeBuilder builder : addedUsesNodes) {
@@ -174,7 +177,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
 
     @Override
     public void addTypedef(final TypeDefinitionBuilder type) {
-        throw new YangParseException(moduleName, line, "Augmentation can not contains typedef statement.");
+        throw new YangParseException(getModuleName(), type.getLine(), "Augmentation can not contains typedef statement.");
     }
 
     @Override
@@ -203,7 +206,7 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
     }
 
     @Override
-    public void setStatus(Status status) {
+    public void setStatus(final Status status) {
         this.status = Preconditions.checkNotNull(status, "status cannot be null");
     }
 
@@ -227,6 +230,11 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         this.targetNodeSchemaPath = path;
     }
 
+    @Override
+    public int getOrder() {
+        return order;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 17;
@@ -282,7 +290,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         copyOf = old;
     }
 
-    private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware {
+    private static final class AugmentationSchemaImpl implements AugmentationSchema, NamespaceRevisionAware,
+            Comparable<AugmentationSchemaImpl> {
+        private final int order;
         private SchemaPath targetPath;
         private RevisionAwareXPath whenCondition;
         private ImmutableSet<DataSchemaNode> childNodes;
@@ -296,8 +306,9 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
         private ImmutableList<UnknownSchemaNode> unknownNodes;
         private AugmentationSchema copyOf;
 
-        private AugmentationSchemaImpl(final SchemaPath targetPath) {
+        public AugmentationSchemaImpl(final SchemaPath targetPath, final int order) {
             this.targetPath = targetPath;
+            this.order = order;
         }
 
         public void setCopyOf(final AugmentationSchema build) {
@@ -442,6 +453,26 @@ public final class AugmentationSchemaBuilderImpl extends AbstractDataNodeContain
             sb.append("]");
             return sb.toString();
         }
+
+        @Override
+        public int compareTo(final AugmentationSchemaImpl o) {
+            Iterator<QName> thisIt = this.targetPath.getPath().iterator();
+            Iterator<QName> otherIt = o.getTargetPath().getPath().iterator();
+            while (thisIt.hasNext()) {
+                if (otherIt.hasNext()) {
+                    int comp = thisIt.next().compareTo(otherIt.next());
+                    if (comp != 0) {
+                        return comp;
+                    }
+                } else {
+                    return 1;
+                }
+            }
+            if (otherIt.hasNext()) {
+                return -1;
+            }
+            return this.order - o.order;
+        }
     }
 
 }