Map identities to proper objects
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderTemplate.xtend
index 09ac0700b0fe59983e13937b8cbb52e8385b6094..dbc7846d57b4a36d7bcfbfa8bc098e5a58be80ac 100644 (file)
@@ -8,12 +8,14 @@
 package org.opendaylight.mdsal.binding.java.api.generator
 
 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
-import static org.opendaylight.mdsal.binding.model.util.BindingTypes.DATA_OBJECT
+import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.DATA_OBJECT
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTABLE_AUGMENTATION_NAME
 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.AUGMENTATION_FIELD
-import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
+import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
 
 import com.google.common.collect.ImmutableList
+import com.google.common.collect.ImmutableSet
+import com.google.common.collect.Sets
 import java.util.ArrayList
 import java.util.Collection
 import java.util.HashSet
@@ -25,32 +27,26 @@ 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.MethodSignature;
 import org.opendaylight.mdsal.binding.model.api.ParameterizedType
 import org.opendaylight.mdsal.binding.model.api.Type
-import org.opendaylight.mdsal.binding.model.util.TypeConstants
-import org.opendaylight.mdsal.binding.model.util.Types
+import org.opendaylight.mdsal.binding.model.ri.TypeConstants
+import org.opendaylight.mdsal.binding.model.ri.Types
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
-import org.opendaylight.yangtools.concepts.Builder
-import org.opendaylight.yangtools.yang.binding.AugmentationHolder
 
 /**
  * Template for generating JAVA builder classes.
  */
 class BuilderTemplate extends AbstractBuilderTemplate {
-    /**
-     * Constant used as suffix for builder name.
-     */
-    package static val BUILDER_STR = "Builder";
-
-    static val BUILDER = JavaTypeName.create(Builder)
+    val BuilderImplTemplate implTemplate
 
     /**
      * Constructs new instance of this class.
      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
      */
-    new(GeneratedType genType, GeneratedType targetType, Set<BuilderGeneratedProperty> properties, Type augmentType,
-            Type keyType) {
-        super(genType, targetType, properties, augmentType, keyType)
+    new(GeneratedType genType, GeneratedType targetType, Type keyType) {
+        super(genType, targetType, keyType)
+        implTemplate = new BuilderImplTemplate(this, type.enclosedTypes.get(0))
     }
 
     override isLocalInnerClass(JavaTypeName name) {
@@ -66,7 +62,8 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     override body() '''
         «wrapToDocumentation(formatDataForJavaDoc(targetType))»
         «targetType.annotations.generateDeprecatedAnnotation»
-        public class «type.name» implements «BUILDER.importedName»<«targetType.importedName»> {
+        «generatedAnnotation»
+        public class «type.name» {
 
             «generateFields(false)»
 
@@ -90,12 +87,16 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
             «generateSetters»
 
-            @«OVERRIDE.importedName»
+            /**
+             * A new {@link «targetType.name»} instance.
+             *
+             * @return A new {@link «targetType.name»} instance.
+             */
             public «targetType.name» build() {
                 return new «type.enclosedTypes.get(0).importedName»(this);
             }
 
-            «new BuilderImplTemplate(this, type.enclosedTypes.get(0)).body»
+            «implTemplate.body»
         }
     '''
 
@@ -113,8 +114,9 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private generateConstructorsFromIfcs() '''
         public «type.name»() {
         }
+
         «IF (!(targetType instanceof GeneratedTransferObject))»
-            «FOR impl : targetType.implements»
+            «FOR impl : targetType.implements SEPARATOR "\n"»
                 «generateConstructorFromIfc(impl)»
             «ENDFOR»
         «ENDIF»
@@ -126,7 +128,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private Object generateConstructorFromIfc(Type impl) '''
         «IF (impl instanceof GeneratedType)»
             «IF impl.hasNonDefaultMethods»
-                public «type.name»(«impl.fullyQualifiedName» arg) {
+                public «type.name»(«impl.importedName» arg) {
                     «printConstructorPropertySetter(impl)»
                 }
             «ENDIF»
@@ -141,15 +143,41 @@ class BuilderTemplate extends AbstractBuilderTemplate {
             «val ifc = implementedIfc as GeneratedType»
             «FOR getter : ifc.nonDefaultMethods»
                 «IF BindingMapping.isGetterMethodName(getter.name)»
-                    this._«getter.propertyNameFromGetter» = arg.«getter.name»();
+                    «val propertyName = getter.propertyNameFromGetter»
+                    «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»;
                 «ENDIF»
             «ENDFOR»
             «FOR impl : ifc.implements»
-                «printConstructorPropertySetter(impl)»
+                «printConstructorPropertySetter(impl, getSpecifiedGetters(ifc))»
+            «ENDFOR»
+        «ENDIF»
+    '''
+
+    def private Object printConstructorPropertySetter(Type implementedIfc, Set<MethodSignature> alreadySetProperties) '''
+        «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
+            «val ifc = implementedIfc as GeneratedType»
+            «FOR getter : ifc.nonDefaultMethods»
+                «IF BindingMapping.isGetterMethodName(getter.name) && getterByName(alreadySetProperties, getter.name).isEmpty»
+                    «val propertyName = getter.propertyNameFromGetter»
+                    «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»;
+                «ENDIF»
+            «ENDFOR»
+            «FOR descendant : ifc.implements»
+                «printConstructorPropertySetter(descendant, Sets.union(alreadySetProperties, getSpecifiedGetters(ifc)))»
             «ENDFOR»
         «ENDIF»
     '''
 
+    def static Set<MethodSignature> getSpecifiedGetters(GeneratedType type) {
+        val ImmutableSet.Builder<MethodSignature> setBuilder = new ImmutableSet.Builder
+        for (MethodSignature method : type.getMethodDefinitions()) {
+            if (method.hasOverrideAnnotation) {
+                setBuilder.add(method)
+            }
+        }
+        return setBuilder.build()
+    }
+
     /**
      * Generate 'fieldsFrom' method to set builder properties based on type of given argument.
      */
@@ -174,12 +202,12 @@ class BuilderTemplate extends AbstractBuilderTemplate {
          * Set fields from given grouping argument. Valid argument is instance of one of following types:
          * <ul>
          «FOR impl : type.getAllIfcs»
-         * <li>«impl.fullyQualifiedName»</li>
+         * <li>«impl.importedName»</li>
          «ENDFOR»
          * </ul>
          *
          * @param arg grouping object
-         * @throws IllegalArgumentException if given argument is none of valid types
+         * @throws IllegalArgumentException if given argument is none of valid types or has property with incompatible value
         */
     '''
 
@@ -199,7 +227,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private generateIfCheck(Type impl, List<Type> done) '''
         «IF (impl instanceof GeneratedType && (impl as GeneratedType).hasNonDefaultMethods)»
             «val implType = impl as GeneratedType»
-            if (arg instanceof «implType.fullyQualifiedName») {
+            if (arg instanceof «implType.importedName») {
                 «printPropertySetter(implType)»
                 isValidArg = true;
             }
@@ -210,13 +238,34 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
         «val ifc = implementedIfc as GeneratedType»
         «FOR getter : ifc.nonDefaultMethods»
-            «IF BindingMapping.isGetterMethodName(getter.name)»
-                this._«getter.propertyNameFromGetter» = ((«implementedIfc.fullyQualifiedName»)arg).«getter.name»();
+            «IF BindingMapping.isGetterMethodName(getter.name) && !hasOverrideAnnotation(getter)»
+                «printPropertySetter(getter, '''((«ifc.importedName»)arg).«getter.name»()''', getter.propertyNameFromGetter)»;
             «ENDIF»
         «ENDFOR»
         «ENDIF»
     '''
 
+    def private printPropertySetter(MethodSignature getter, String retrieveProperty, String propertyName) {
+        val ownGetter = implTemplate.findGetter(getter.name)
+        val ownGetterType = ownGetter.returnType
+        if (Types.strictTypeEquals(getter.returnType, ownGetterType)) {
+            return "this._" + propertyName + " = " + retrieveProperty
+        }
+        if (ownGetterType instanceof ParameterizedType) {
+            val itemType = ownGetterType.actualTypeArguments.get(0)
+            if (Types.isListType(ownGetterType)) {
+                return printPropertySetter(retrieveProperty, propertyName, "checkListFieldCast", itemType.importedName)
+            }
+            if (Types.isSetType(ownGetterType)) {
+                return printPropertySetter(retrieveProperty, propertyName, "checkSetFieldCast", itemType.importedName)
+            }
+        }
+        return printPropertySetter(retrieveProperty, propertyName, "checkFieldCast", ownGetterType.importedName)
+    }
+
+    def private printPropertySetter(String retrieveProperty, String propertyName, String checkerName, String className) '''
+            this._«propertyName» = «CODEHELPERS.importedName».«checkerName»(«className».class, "«propertyName»", «retrieveProperty»)'''
+
     private def List<Type> getBaseIfcs(GeneratedType type) {
         val List<Type> baseIfcs = new ArrayList();
         for (ifc : type.implements) {
@@ -244,7 +293,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     private def List<String> toListOfNames(Collection<Type> types) {
         val List<String> names = new ArrayList
         for (type : types) {
-            names.add(type.fullyQualifiedName)
+            names.add(type.importedName)
         }
         return names
     }
@@ -274,16 +323,20 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private generateSetter(GeneratedProperty field) {
         val returnType = field.returnType
         if (returnType instanceof ParameterizedType) {
-            if (Types.isListType(returnType)) {
-                return generateListSetter(field, returnType.actualTypeArguments.get(0), "")
+            if (Types.isListType(returnType) || Types.isSetType(returnType)) {
+                val arguments = returnType.actualTypeArguments
+                if (arguments.isEmpty) {
+                    return generateListSetter(field, Types.objectType)
+                }
+                return generateListSetter(field, arguments.get(0))
             } else if (Types.isMapType(returnType)) {
-                return generateListSetter(field, returnType.actualTypeArguments.get(1), ".values()")
+                return generateMapSetter(field, returnType.actualTypeArguments.get(1))
             }
         }
         return generateSimpleSetter(field, returnType)
     }
 
-    def private generateListSetter(GeneratedProperty field, Type actualType, String extractor) '''
+    def private generateListSetter(GeneratedProperty field, Type actualType) '''
         «val restrictions = restrictionsForSetter(actualType)»
         «IF restrictions !== null»
             «generateCheckers(field, restrictions, actualType)»
@@ -291,7 +344,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         public «type.getName» set«field.getName.toFirstUpper»(final «field.returnType.importedName» values) {
         «IF restrictions !== null»
             if (values != null) {
-               for («actualType.importedName» value : values«extractor») {
+               for («actualType.importedName» value : values) {
                    «checkArgument(field, restrictions, actualType, "value")»
                }
             }
@@ -302,6 +355,24 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
     '''
 
+    def private generateMapSetter(GeneratedProperty field, Type actualType) '''
+        «val restrictions = restrictionsForSetter(actualType)»
+        «IF restrictions !== null»
+            «generateCheckers(field, restrictions, actualType)»
+        «ENDIF»
+        public «type.getName» set«field.name.toFirstUpper»(final «field.returnType.importedName» values) {
+        «IF restrictions !== null»
+            if (values != null) {
+               for («actualType.importedName» value : values.values()) {
+                   «checkArgument(field, restrictions, actualType, "value")»
+               }
+            }
+        «ENDIF»
+            this.«field.fieldName» = values;
+            return this;
+        }
+    '''
+
     def private generateSimpleSetter(GeneratedProperty field, Type actualType) '''
         «val restrictions = restrictionsForSetter(actualType)»
         «IF restrictions !== null»
@@ -349,20 +420,13 @@ class BuilderTemplate extends AbstractBuilderTemplate {
               * @throws NullPointerException if {@code augmentation} is null
               */
             public «type.name» addAugmentation(«augmentTypeRef» augmentation) {
-                return doAddAugmentation(augmentation.«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»(), augmentation);
-            }
+                «jlClassRef»<? extends «augmentTypeRef»> augmentationType = augmentation.«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»();
+                if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
+                    this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
+                }
 
-            /**
-              * Add or remove an augmentation to this builder's product.
-              *
-              * @param augmentationType augmentation type to be added or removed
-              * @param augmentationValue augmentation value, null if the augmentation type should be removed
-              * @return this builder
-              * @deprecated Use either {@link #addAugmentation(«augmentType.importedJavadocName»)} or {@link #removeAugmentation(«CLASS.importedName»)} instead.
-              */
-            @«DEPRECATED.importedName»
-            public «type.name» addAugmentation(«jlClassRef»<? extends «augmentTypeRef»> augmentationType, «augmentTypeRef» augmentationValue) {
-                return augmentationValue == null ? removeAugmentation(augmentationType) : doAddAugmentation(augmentationType, augmentationValue);
+                this.«AUGMENTATION_FIELD».put(augmentationType, augmentation);
+                return this;
             }
 
             /**
@@ -378,20 +442,11 @@ class BuilderTemplate extends AbstractBuilderTemplate {
                 }
                 return this;
             }
-
-            private «type.name» doAddAugmentation(«jlClassRef»<? extends «augmentTypeRef»> augmentationType, «augmentTypeRef» augmentationValue) {
-                if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
-                    this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
-                }
-
-                this.«AUGMENTATION_FIELD».put(augmentationType, augmentationValue);
-                return this;
-            }
         «ENDIF»
     '''
 
     private def createDescription(GeneratedType targetType) {
-        val target = type.importedName
+        val target = targetType.importedName
         return '''
         Class that builds {@link «target»} instances. Overall design of the class is that of a
         <a href="https://en.wikipedia.org/wiki/Fluent_interface">fluent interface</a>, where method chaining is used.
@@ -400,7 +455,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         In general, this class is supposed to be used like this template:
         <pre>
           <code>
-            «target» createTarget(int fooXyzzy, int barBaz) {
+            «target» create«target»(int fooXyzzy, int barBaz) {
                 return new «target»Builder()
                     .setFoo(new FooBuilder().setXyzzy(fooXyzzy).build())
                     .setBar(new BarBuilder().setBaz(barBaz).build())
@@ -421,14 +476,13 @@ class BuilderTemplate extends AbstractBuilderTemplate {
               invocation, which is terminated by {@link #build()}, which is then returned from the method</li>
           <li>better understanding by humans, as the scope of mutable state (the builder) is kept to a minimum and is
               very localized</li>
-          <li>better optimization oportunities, as the object scope is minimized in terms of invocation (rather than
+          <li>better optimization opportunities, as the object scope is minimized in terms of invocation (rather than
               method) stack, making <a href="https://en.wikipedia.org/wiki/Escape_analysis">escape analysis</a> a lot
               easier. Given enough compiler (JIT/AOT) prowess, the cost of th builder object can be completely
               eliminated</li>
         </ul>
 
         @see «target»
-        @see «BUILDER.importedName»
     '''
     }
 
@@ -458,31 +512,18 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
     override protected CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props) '''
         «FOR field : props»
-            this.«field.fieldName» = base.«field.getterMethodName»();
+            this.«field.fieldName» = base.«field.getterName»();
         «ENDFOR»
     '''
 
     override protected generateCopyAugmentation(Type implType) {
-        val augmentationHolderRef = AugmentationHolder.importedName
-        val typeRef = targetType.importedName
         val hashMapRef = JU_HASHMAP.importedName
         val augmentTypeRef = augmentType.importedName
         return '''
-            if (base instanceof «augmentationHolderRef») {
-                @SuppressWarnings("unchecked")
-                «JU_MAP.importedName»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> aug =((«augmentationHolderRef»<«typeRef»>) base).augmentations();
-                if (!aug.isEmpty()) {
-                    this.«AUGMENTATION_FIELD» = new «hashMapRef»<>(aug);
-                }
+            «JU_MAP.importedName»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> aug = base.augmentations();
+            if (!aug.isEmpty()) {
+                this.«AUGMENTATION_FIELD» = new «hashMapRef»<>(aug);
             }
         '''
     }
-
-    private static def hasNonDefaultMethods(GeneratedType type) {
-        !type.methodDefinitions.isEmpty && type.methodDefinitions.exists([def | !def.isDefault])
-    }
-
-    private static def nonDefaultMethods(GeneratedType type) {
-        type.methodDefinitions.filter([def | !def.isDefault])
-    }
 }