BUG-868: migrate to SchemaPath.create()
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / impl / BindingGeneratorImpl.xtend
index e462fbaf23b156a6907972aa62dbb1d348d62028..620efbcee3da17b7fb6146f5bf4f5dedc763f388 100644 (file)
@@ -79,6 +79,7 @@ import org.opendaylight.yangtools.yang.binding.BindingMapping
 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase
 
 import com.google.common.collect.Sets
+import java.util.TreeSet
 
 public class BindingGeneratorImpl implements BindingGenerator {
 
@@ -364,19 +365,45 @@ public class BindingGeneratorImpl implements BindingGenerator {
         checkState(module.augmentations !== null, "Augmentations Set cannot be NULL.");
 
         val Set<AugmentationSchema> augmentations = module.augmentations;
-        val List<AugmentationSchema> sortedAugmentations = new ArrayList(augmentations);
+        var List<AugmentationSchema> sortedAugmentations = getSortedOrNull(augmentations)
+        if (sortedAugmentations != null) {
+            return sortedAugmentations
+        }
+        sortedAugmentations = new ArrayList(augmentations);
         Collections.sort(sortedAugmentations,
             [ augSchema1, augSchema2 |
-                if (augSchema1.targetPath.path.size() > augSchema2.targetPath.path.size()) {
-                    return 1;
-                } else if (augSchema1.targetPath.path.size() < augSchema2.targetPath.path.size()) {
-                    return -1;
+                val Iterator<QName> thisIt = augSchema1.targetPath.getPath().iterator();
+                val Iterator<QName> otherIt = augSchema2.getTargetPath().getPath().iterator();
+                while (thisIt.hasNext()) {
+                    if (otherIt.hasNext()) {
+                        val int comp = thisIt.next().compareTo(otherIt.next());
+                        if (comp != 0) {
+                            return comp
+                        }
+                    } else {
+                        return 1
+                    }
+                }
+                if (otherIt.hasNext()) {
+                    return -1
                 }
-                return 0;
+                return 0
             ]);
         return sortedAugmentations;
     }
 
+    private def List<AugmentationSchema> getSortedOrNull(Collection<AugmentationSchema> collection) {
+        val TreeSet<AugmentationSchema> set = new TreeSet()
+        for (e : collection) {
+            if (e instanceof Comparable<?>) {
+                set.add(e)
+            } else {
+                return null
+            }
+        }
+        return new ArrayList(set.toArray)
+    }
+
     /**
      * Converts whole <b>module</b> to <code>GeneratedType</code> object.
      * Firstly is created the module builder object from which is vally
@@ -1129,7 +1156,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     val List<QName> nodeNames = nodeSp.path
                     val List<QName> nodeNewNames = new ArrayList(nodeNames)
                     nodeNewNames.remove(nodeNewNames.size - 1)
-                    val SchemaPath nodeNewSp = new SchemaPath(nodeNewNames, nodeSp.absolute)
+                    val SchemaPath nodeNewSp = SchemaPath.create(nodeNewNames, nodeSp.absolute)
                     parentNode = findDataSchemaNode(schemaContext, nodeNewSp)
 
                     var SchemaNode parent
@@ -1152,7 +1179,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                         val List<QName> names = sp.path
                         val List<QName> newNames = new ArrayList(names)
                         newNames.remove(newNames.size - 1)
-                        val SchemaPath newSp = new SchemaPath(newNames, sp.absolute)
+                        val SchemaPath newSp = SchemaPath.create(newNames, sp.absolute)
                         parent = findDataSchemaNode(schemaContext, newSp)
                     }
                     var GeneratedTypeBuilder childOfType = findChildNodeByPath(parent.path)
@@ -1206,7 +1233,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 val List<QName> nodeNames = nodeSp.path
                 val List<QName> nodeNewNames = new ArrayList(nodeNames)
                 nodeNewNames.remove(nodeNewNames.size - 1)
-                val SchemaPath nodeNewSp = new SchemaPath(nodeNewNames, nodeSp.absolute)
+                val SchemaPath nodeNewSp = SchemaPath.create(nodeNewNames, nodeSp.absolute)
                 parent = findDataSchemaNode(schemaContext, nodeNewSp)
 
                 var GeneratedTypeBuilder childOfType = null;