Merge "Improved sorting of augmentations before code generation."
authorTony Tkacik <ttkacik@cisco.com>
Fri, 6 Jun 2014 08:24:42 +0000 (08:24 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 6 Jun 2014 08:24:42 +0000 (08:24 +0000)
1  2 
code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.xtend

index e462fbaf23b156a6907972aa62dbb1d348d62028,82a191eb44288371129f55d67c3ae9b4be6d3320..d2341b73309964759b57476237ea1e1a0ab159d9
@@@ -79,6 -79,7 +79,7 @@@ import org.opendaylight.yangtools.yang.
  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 {
  
          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
      private def GeneratedTypeBuilder addDefaultInterfaceDefinition(String packageName, SchemaNode schemaNode,
          Type parent) {
          val it = addRawInterfaceDefinition(packageName, schemaNode, "");
 -        qnameConstant(BindingMapping.QNAME_STATIC_FIELD_NAME,schemaNode.QName);
          if (parent === null) {
              addImplementsType(DATA_OBJECT);
          } else {
  
          //FIXME: Validation of name conflict
          val newType = new GeneratedTypeBuilderImpl(packageName, genTypeName);
 +        qnameConstant(newType,BindingMapping.QNAME_STATIC_FIELD_NAME,schemaNode.QName);
          newType.addComment(schemaNode.getDescription());
          if (!genTypeBuilders.containsKey(packageName)) {
              val Map<String, GeneratedTypeBuilder> builders = new HashMap();