Generate javadoc for builder setters
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderTemplate.xtend
index 5481e01c6e806bc49ae06a7317080000e2bd4994..c29f8aea4b59143f86d0be305e52a3f712545974 100644 (file)
@@ -8,7 +8,7 @@
 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
@@ -30,22 +30,14 @@ 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
 
 /**
  * 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
 
     /**
@@ -71,18 +63,33 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «wrapToDocumentation(formatDataForJavaDoc(targetType))»
         «targetType.annotations.generateDeprecatedAnnotation»
         «generatedAnnotation»
-        public class «type.name» implements «BUILDER.importedName»<«targetType.importedName»> {
+        public class «type.name» {
 
             «generateFields(false)»
 
             «constantsDeclarations()»
 
             «IF augmentType !== null»
-                «generateAugmentField()»
+                «val augmentTypeRef = augmentType.importedName»
+                «val mapTypeRef = JU_MAP.importedName»
+                «mapTypeRef»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> «AUGMENTATION_FIELD» = «mapTypeRef».of();
             «ENDIF»
 
+            /**
+             * Construct an empty builder.
+             */
+            public «type.name»() {
+                // No-op
+            }
+
             «generateConstructorsFromIfcs()»
 
+            «val targetTypeName = targetType.importedName»
+            /**
+             * Construct a builder initialized with state from specified {@link «targetTypeName»}.
+             *
+             * @param base «targetTypeName» from which the builder should be initialized
+             */
             public «generateCopyConstructor(targetType, type.enclosedTypes.get(0))»
 
             «generateMethodFieldsFrom()»
@@ -95,8 +102,12 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
             «generateSetters»
 
-            @«OVERRIDE.importedName»
-            public «targetType.name» build() {
+            /**
+             * A new {@link «targetTypeName»} instance.
+             *
+             * @return A new {@link «targetTypeName»} instance.
+             */
+            public «targetType.importedNonNull» build() {
                 return new «type.enclosedTypes.get(0).importedName»(this);
             }
 
@@ -116,9 +127,6 @@ class BuilderTemplate extends AbstractBuilderTemplate {
      * Generate default constructor and constructor for every implemented interface from uses statements.
      */
     def private generateConstructorsFromIfcs() '''
-        public «type.name»() {
-        }
-
         «IF (!(targetType instanceof GeneratedTransferObject))»
             «FOR impl : targetType.implements SEPARATOR "\n"»
                 «generateConstructorFromIfc(impl)»
@@ -132,9 +140,16 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private Object generateConstructorFromIfc(Type impl) '''
         «IF (impl instanceof GeneratedType)»
             «IF impl.hasNonDefaultMethods»
-                public «type.name»(«impl.fullyQualifiedName» arg) {
+                «val typeName = impl.importedName»
+                /**
+                 * Construct a new builder initialized from specified {@link «typeName»}.
+                 *
+                 * @param arg «typeName» from which the builder should be initialized
+                 */
+                public «type.name»(«typeName» arg) {
                     «printConstructorPropertySetter(impl)»
                 }
+
             «ENDIF»
             «FOR implTypeImplement : impl.implements»
                 «generateConstructorFromIfc(implTypeImplement)»
@@ -206,7 +221,7 @@ 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>{@link «impl.importedName»}</li>
          «ENDFOR»
          * </ul>
          *
@@ -231,7 +246,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;
             }
@@ -243,7 +258,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «val ifc = implementedIfc as GeneratedType»
         «FOR getter : ifc.nonDefaultMethods»
             «IF BindingMapping.isGetterMethodName(getter.name) && !hasOverrideAnnotation(getter)»
-                «printPropertySetter(getter, '''((«ifc.fullyQualifiedName»)arg).«getter.name»()''', getter.propertyNameFromGetter)»;
+                «printPropertySetter(getter, '''((«ifc.importedName»)arg).«getter.name»()''', getter.propertyNameFromGetter)»;
             «ENDIF»
         «ENDFOR»
         «ENDIF»
@@ -255,13 +270,39 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         if (Types.strictTypeEquals(getter.returnType, ownGetterType)) {
             return "this._" + propertyName + " = " + retrieveProperty
         }
-        if (Types.isListType(ownGetterType)) {
-            val itemType = (ownGetterType as ParameterizedType).actualTypeArguments.get(0)
-            return '''
-                this._«propertyName» = «CODEHELPERS.importedName».checkListFieldCast(«itemType.fullyQualifiedName».class, "«propertyName»", «retrieveProperty»)'''
+        if (ownGetterType instanceof ParameterizedType) {
+            val itemType = ownGetterType.actualTypeArguments.get(0)
+            if (Types.isListType(ownGetterType)) {
+                val importedClass = importedClass(itemType)
+                if (importedClass !== null) {
+                    return printPropertySetter(retrieveProperty, propertyName, "checkListFieldCastIdentity", importedClass)
+                }
+                return printPropertySetter(retrieveProperty, propertyName, "checkListFieldCast", itemType.importedName)
+            }
+            if (Types.isSetType(ownGetterType)) {
+                val importedClass = importedClass(itemType)
+                if (importedClass !== null) {
+                    return printPropertySetter(retrieveProperty, propertyName, "checkSetFieldCastIdentity", importedClass)
+                }
+                return printPropertySetter(retrieveProperty, propertyName, "checkSetFieldCast", itemType.importedName)
+            }
+            if (Types.CLASS.equals(ownGetterType)) {
+                return printPropertySetter(retrieveProperty, propertyName, "checkFieldCastIdentity", itemType.identifier.importedName)
+            }
         }
-        return '''
-            this._«propertyName» = «CODEHELPERS.importedName».checkFieldCast(«ownGetter.returnType.fullyQualifiedName».class, "«propertyName»", «retrieveProperty»)'''
+        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 importedClass(Type type) {
+        if (type instanceof ParameterizedType) {
+            if (Types.CLASS.equals(type.rawType)) {
+                return type.actualTypeArguments.get(0).identifier.importedName
+            }
+        }
+        return null
     }
 
     private def List<Type> getBaseIfcs(GeneratedType type) {
@@ -291,7 +332,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
     }
@@ -318,10 +359,10 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «ENDFOR»
     '''
 
-    def private generateSetter(GeneratedProperty field) {
+    def private generateSetter(BuilderGeneratedProperty field) {
         val returnType = field.returnType
         if (returnType instanceof ParameterizedType) {
-            if (Types.isListType(returnType)) {
+            if (Types.isListType(returnType) || Types.isSetType(returnType)) {
                 val arguments = returnType.actualTypeArguments
                 if (arguments.isEmpty) {
                     return generateListSetter(field, Types.objectType)
@@ -334,11 +375,19 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         return generateSimpleSetter(field, returnType)
     }
 
-    def private generateListSetter(GeneratedProperty field, Type actualType) '''
+    def private generateListSetter(BuilderGeneratedProperty field, Type actualType) '''
         «val restrictions = restrictionsForSetter(actualType)»
         «IF restrictions !== null»
             «generateCheckers(field, restrictions, actualType)»
         «ENDIF»
+
+        /**
+         * Set the property corresponding to {@link «targetType.importedName»#«field.getterName»()} to the specified
+         * value.
+         *
+         * @param values desired value
+         * @return this builder
+         */
         public «type.getName» set«field.getName.toFirstUpper»(final «field.returnType.importedName» values) {
         «IF restrictions !== null»
             if (values != null) {
@@ -353,18 +402,23 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
     '''
 
-    // FIXME: MDSAL-540: remove the migration setter
-    def private generateMapSetter(GeneratedProperty field, Type actualType) '''
+    def private generateMapSetter(BuilderGeneratedProperty field, Type actualType) '''
         «val restrictions = restrictionsForSetter(actualType)»
-        «val actualTypeRef = actualType.importedName»
-        «val setterName = "set" + field.name.toFirstUpper»
         «IF restrictions !== null»
             «generateCheckers(field, restrictions, actualType)»
         «ENDIF»
-        public «type.getName» «setterName»(final «field.returnType.importedName» values) {
+
+        /**
+         * Set the property corresponding to {@link «targetType.importedName»#«field.getterName»()} to the specified
+         * value.
+         *
+         * @param values desired value
+         * @return this builder
+         */
+        public «type.getName» set«field.name.toFirstUpper»(final «field.returnType.importedName» values) {
         «IF restrictions !== null»
             if (values != null) {
-               for («actualTypeRef» value : values.values()) {
+               for («actualType.importedName» value : values.values()) {
                    «checkArgument(field, restrictions, actualType, "value")»
                }
             }
@@ -372,33 +426,22 @@ class BuilderTemplate extends AbstractBuilderTemplate {
             this.«field.fieldName» = values;
             return this;
         }
-
-        /**
-          * Utility migration setter.
-          *
-          * <b>IMPORTANT NOTE</b>: This method does not completely match previous mechanics, as the list is processed as
-          *                        during this method's execution. Any future modifications of the list are <b>NOT</b>
-          *                        reflected in this builder nor its products.
-          *
-          * @param values Legacy List of values
-          * @return this builder
-          * @throws IllegalArgumentException if the list contains entries with the same key
-          * @throws NullPointerException if the list contains a null entry
-          * @deprecated Use {@link #«setterName»(«JU_MAP.importedName»)} instead.
-          */
-        @«DEPRECATED.importedName»(forRemoval = true)
-        public «type.getName» «setterName»(final «JU_LIST.importedName»<«actualTypeRef»> values) {
-            return «setterName»(«CODEHELPERS.importedName».compatMap(values));
-        }
     '''
 
-    def private generateSimpleSetter(GeneratedProperty field, Type actualType) '''
+    def private generateSimpleSetter(BuilderGeneratedProperty field, Type actualType) '''
         «val restrictions = restrictionsForSetter(actualType)»
         «IF restrictions !== null»
 
             «generateCheckers(field, restrictions, actualType)»
         «ENDIF»
 
+        /**
+         * Set the property corresponding to {@link «targetType.importedName»#«field.getterName»()} to the specified
+         * value.
+         *
+         * @param value desired value
+         * @return this builder
+         */
         «val setterName = "set" + field.getName.toFirstUpper»
         public «type.getName» «setterName»(final «field.returnType.importedName» value) {
             «IF restrictions !== null»
@@ -409,21 +452,6 @@ class BuilderTemplate extends AbstractBuilderTemplate {
             this.«field.fieldName» = value;
             return this;
         }
-        «val uintType = UINT_TYPES.get(field.returnType)»
-        «IF uintType !== null»
-
-            /**
-             * Utility migration setter.
-             *
-             * @param value field value in legacy type
-             * @return this builder
-             * @deprecated Use {@link #«setterName»(«field.returnType.importedJavadocName»)} instead.
-             */
-            @Deprecated(forRemoval = true)
-            public «type.getName» «setterName»(final «uintType.importedName» value) {
-                return «setterName»(«CODEHELPERS.importedName».compatUint(value));
-            }
-        «ENDIF»
     '''
 
     /**
@@ -444,7 +472,6 @@ class BuilderTemplate extends AbstractBuilderTemplate {
 
         «IF augmentType !== null»
             «val augmentTypeRef = augmentType.importedName»
-            «val jlClassRef = CLASS.importedName»
             «val hashMapRef = JU_HASHMAP.importedName»
             /**
               * Add an augmentation to this builder's product.
@@ -454,12 +481,11 @@ class BuilderTemplate extends AbstractBuilderTemplate {
               * @throws NullPointerException if {@code augmentation} is null
               */
             public «type.name» addAugmentation(«augmentTypeRef» augmentation) {
-                «jlClassRef»<? extends «augmentTypeRef»> augmentationType = augmentation.«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»();
                 if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
                     this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
                 }
 
-                this.«AUGMENTATION_FIELD».put(augmentationType, augmentation);
+                this.«AUGMENTATION_FIELD».put(augmentation.«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»(), augmentation);
                 return this;
             }
 
@@ -470,7 +496,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
               * @param augmentationType augmentation type to be removed
               * @return this builder
               */
-            public «type.name» removeAugmentation(«jlClassRef»<? extends «augmentTypeRef»> augmentationType) {
+            public «type.name» removeAugmentation(«CLASS.importedName»<? extends «augmentTypeRef»> augmentationType) {
                 if (this.«AUGMENTATION_FIELD» instanceof «hashMapRef») {
                     this.«AUGMENTATION_FIELD».remove(augmentationType);
                 }
@@ -480,7 +506,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     '''
 
     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.
@@ -489,7 +515,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())
@@ -510,14 +536,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»
     '''
     }
 
@@ -532,6 +557,14 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     }
 
     private def generateAugmentation() '''
+        /**
+         * Return the specified augmentation, if it is present in this builder.
+         *
+         * @param <E$$> augmentation type
+         * @param augmentationType augmentation type class
+         * @return Augmentation object from this builder, or {@code null} if not present
+         * @throws «NPE.importedName» if {@code augmentType} is {@code null}
+         */
         @«SUPPRESS_WARNINGS.importedName»({ "unchecked", "checkstyle:methodTypeParameterName"})
         public <E$$ extends «augmentType.importedName»> E$$ «AUGMENTABLE_AUGMENTATION_NAME»(«CLASS.importedName»<E$$> augmentationType) {
             return (E$$) «AUGMENTATION_FIELD».get(«JU_OBJECTS.importedName».requireNonNull(augmentationType));
@@ -551,14 +584,10 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «ENDFOR»
     '''
 
-    override protected generateCopyAugmentation(Type implType) {
-        val hashMapRef = JU_HASHMAP.importedName
-        val augmentTypeRef = augmentType.importedName
-        return '''
-            «JU_MAP.importedName»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> aug = base.augmentations();
-            if (!aug.isEmpty()) {
-                this.«AUGMENTATION_FIELD» = new «hashMapRef»<>(aug);
-            }
-        '''
-    }
+    override protected generateCopyAugmentation(Type implType) '''
+       final var aug = base.augmentations();
+       if (!aug.isEmpty()) {
+           this.«AUGMENTATION_FIELD» = new «JU_HASHMAP.importedName»<>(aug);
+       }
+    '''
 }