Speed up AbstractBuilderTemplate.removeProperty()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / AbstractBuilderTemplate.xtend
index 6622ee2d4bed465a525eb52af6deee413469c4c8..da98b8c7bf829ab7aaec277b7249545e34912044 100644 (file)
@@ -7,28 +7,31 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
 
 import com.google.common.base.MoreObjects
+import java.util.ArrayList
 import java.util.Collection
 import java.util.Collections
+import java.util.Comparator
+import java.util.List
 import java.util.Map
 import java.util.Set
+import org.opendaylight.mdsal.binding.model.api.AnnotationType
 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
+import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
 import org.opendaylight.mdsal.binding.model.api.GeneratedType
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.yangtools.yang.binding.CodeHelpers
-import java.util.ArrayList
-import org.opendaylight.mdsal.binding.model.util.Types
-import org.opendaylight.yangtools.yang.binding.Identifiable
-import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
+import org.opendaylight.mdsal.binding.model.util.BindingTypes
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
-import com.google.common.collect.ImmutableMap
-import org.opendaylight.yangtools.yang.binding.AugmentationHolder
-import java.util.HashMap
+import org.opendaylight.yangtools.yang.binding.CodeHelpers
 
 abstract class AbstractBuilderTemplate extends BaseTemplate {
+    static val Comparator<GeneratedProperty> KEY_PROPS_COMPARATOR = [ p1, p2 | return p1.name.compareTo(p2.name) ]
+
+    protected static val DEPRECATED = JavaTypeName.create(Deprecated);
+
     /**
      * Generated property is set if among methods is found one with the name GET_AUGMENTATION_METHOD_NAME.
      */
@@ -70,7 +73,7 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
      * @param makeFinal value which specify whether field is|isn't final
      * @return string with class attributes and their types
      */
-    def generateFields(boolean makeFinal) '''
+    def protected final generateFields(boolean makeFinal) '''
         «IF properties !== null»
             «FOR f : properties»
                 private«IF makeFinal» final«ENDIF» «f.returnType.importedName» «f.fieldName»;
@@ -81,11 +84,12 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
         «ENDIF»
     '''
 
-    def generateAugmentField(boolean isPrivate) '''
-        «IF augmentType !== null»
-            «IF isPrivate»private «ENDIF»«Map.importedName»<«Class.importedName»<? extends «augmentType.importedName»>, «augmentType.importedName»> «AUGMENTATION_FIELD» = «Collections.importedName».emptyMap();
-        «ENDIF»
-    '''
+    def protected final generateAugmentField() {
+        val augmentTypeRef = augmentType.importedName
+        return '''
+           «Map.importedName»<«Class.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> «AUGMENTATION_FIELD» = «Collections.importedName».emptyMap();
+        '''
+    }
 
     override generateToString(Collection<GeneratedProperty> properties) '''
         «IF properties !== null»
@@ -96,7 +100,7 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
                     «CodeHelpers.importedName».appendValue(helper, "«property.fieldName»", «property.fieldName»);
                 «ENDFOR»
                 «IF augmentType !== null»
-                    «CodeHelpers.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", «AUGMENTATION_FIELD».values());
+                    «CodeHelpers.importedName».appendValue(helper, "«AUGMENTATION_FIELD»", augmentations().values());
                 «ENDIF»
                 return helper.toString();
             }
@@ -108,7 +112,7 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
      *
      * @return string with getter methods
      */
-    def generateGetters(boolean addOverride) '''
+    def final generateGetters(boolean addOverride) '''
         «IF keyType !== null»
             «IF addOverride»@«Override.importedName»«ENDIF»
             public «keyType.importedName» «BindingMapping.IDENTIFIABLE_KEY_NAME»() {
@@ -122,66 +126,43 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
                 «field.getterMethod»
             «ENDFOR»
         «ENDIF»
-        «IF augmentType !== null»
-
-            @SuppressWarnings("unchecked")
-            «IF addOverride»@«Override.importedName»«ENDIF»
-            public <E extends «augmentType.importedName»> E «AUGMENTABLE_AUGMENTATION_NAME»(«Class.importedName»<E> augmentationType) {
-                return (E) «AUGMENTATION_FIELD».get(«CodeHelpers.importedName».nonNullValue(augmentationType, "augmentationType"));
-            }
-        «ENDIF»
     '''
 
-    def CharSequence generateCopyConstructor(boolean impl, Type fromType, Type implType) '''
-        «IF impl»private«ELSE»public«ENDIF» «type.name»(«fromType.importedName» base) {
+    def protected final CharSequence generateCopyConstructor(Type fromType, Type implType) '''
+        «type.name»(«fromType.importedName» base) {
+            «IF augmentType !== null»
+                «generateCopyAugmentation(implType)»
+            «ENDIF»
             «val allProps = new ArrayList(properties)»
-            «val isList = implementsIfc(targetType, Types.parameterizedTypeFor(Types.typeForClass(Identifiable), targetType))»
+            «val isList = implementsIfc(targetType, BindingTypes.identifiable(targetType))»
             «IF isList && keyType !== null»
                 «val keyProps = new ArrayList((keyType as GeneratedTransferObject).properties)»
-                «Collections.sort(keyProps, [ p1, p2 | return p1.name.compareTo(p2.name) ]
+                «keyProps.sort(KEY_PROPS_COMPARATOR
                 «FOR field : keyProps»
                     «removeProperty(allProps, field.name)»
                 «ENDFOR»
-                if (base.«BindingMapping.IDENTIFIABLE_KEY_NAME»() == null) {
-                    this.key = new «keyType.importedName»(
-                        «FOR keyProp : keyProps SEPARATOR ", "»
-                            base.«keyProp.getterMethodName»()
-                        «ENDFOR»
-                    );
-                    «FOR field : keyProps»
-                        this.«field.fieldName» = base.«field.getterMethodName»();
-                    «ENDFOR»
-                } else {
-                    this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
-                    «FOR field : keyProps»
-                           this.«field.fieldName» = key.«field.getterMethodName»();
-                    «ENDFOR»
-                }
+                «generateCopyKeys(keyProps)»
             «ENDIF»
             «FOR field : allProps»
                 this.«field.fieldName» = base.«field.getterMethodName»();
             «ENDFOR»
-            «IF augmentType !== null»
-                «IF impl»
-                    this.«AUGMENTATION_FIELD» = «ImmutableMap.importedName».copyOf(base.«AUGMENTATION_FIELD»);
-                «ELSE»
-                    if (base instanceof «implType.importedName») {
-                        «implType.importedName» impl = («implType.importedName») base;
-                        if (!impl.«AUGMENTATION_FIELD».isEmpty()) {
-                            this.«AUGMENTATION_FIELD» = new «HashMap.importedName»<>(impl.«AUGMENTATION_FIELD»);
-                        }
-                    } else if (base instanceof «AugmentationHolder.importedName») {
-                        @SuppressWarnings("unchecked")
-                        «AugmentationHolder.importedName»<«fromType.importedName»> casted =(«AugmentationHolder.importedName»<«fromType.importedName»>) base;
-                        if (!casted.augmentations().isEmpty()) {
-                            this.«AUGMENTATION_FIELD» = new «HashMap.importedName»<>(casted.augmentations());
-                        }
-                    }
-                «ENDIF»
-            «ENDIF»
         }
     '''
 
+    def protected final CharSequence generateDeprecatedAnnotation(List<AnnotationType> annotations) {
+        var AnnotationType found = annotations.findDeprecatedAnnotation
+        if (found === null) {
+            return ""
+        }
+        return generateDeprecatedAnnotation(found)
+    }
+
+    def protected abstract CharSequence generateCopyKeys(List<GeneratedProperty> keyProps)
+
+    def protected abstract CharSequence generateCopyAugmentation(Type implType)
+
+    def protected abstract CharSequence generateDeprecatedAnnotation(AnnotationType ann)
+
     private def boolean implementsIfc(GeneratedType type, Type impl) {
         for (Type ifc : type.implements) {
             if (ifc.equals(impl)) {
@@ -192,14 +173,23 @@ abstract class AbstractBuilderTemplate extends BaseTemplate {
     }
 
     private def void removeProperty(Collection<GeneratedProperty> props, String name) {
-        var GeneratedProperty toRemove = null
-        for (p : props) {
-            if (p.name.equals(name)) {
-                toRemove = p;
+        val iter = props.iterator
+        while (iter.hasNext) {
+            if (name.equals(iter.next.name)) {
+                iter.remove
+                return
             }
         }
-        if (toRemove !== null) {
-            props.remove(toRemove);
+    }
+
+    private static def findDeprecatedAnnotation(List<AnnotationType> annotations) {
+        if (annotations !== null) {
+            for (annotation : annotations) {
+                if (DEPRECATED.equals(annotation.identifier)) {
+                    return annotation
+                }
+            }
         }
+        return null
     }
-}
\ No newline at end of file
+}