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 5e8040cfcfdddc22c2e905fac00e33ced80a3987..dbc7846d57b4a36d7bcfbfa8bc098e5a58be80ac 100644 (file)
@@ -11,7 +11,7 @@ import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
 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
@@ -33,14 +33,11 @@ import org.opendaylight.mdsal.binding.model.api.Type
 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 {
-    static val BUILDER = JavaTypeName.create(Builder)
-
     val BuilderImplTemplate implTemplate
 
     /**
@@ -66,7 +63,7 @@ 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)»
 
@@ -90,7 +87,11 @@ 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);
             }
@@ -250,15 +251,21 @@ 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.importedName».class, "«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 '''
-            this._«propertyName» = «CODEHELPERS.importedName».checkFieldCast(«ownGetter.returnType.importedName».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 List<Type> getBaseIfcs(GeneratedType type) {
         val List<Type> baseIfcs = new ArrayList();
         for (ifc : type.implements) {
@@ -316,7 +323,7 @@ class BuilderTemplate extends AbstractBuilderTemplate {
     def private generateSetter(GeneratedProperty 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)
@@ -413,7 +420,7 @@ 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»();
+                «jlClassRef»<? extends «augmentTypeRef»> augmentationType = augmentation.«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»();
                 if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
                     this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
                 }
@@ -439,7 +446,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.
@@ -448,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())
@@ -469,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»
     '''
     }