Reduce use of BaseTemplate.getterMethodName()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderTemplate.xtend
index 673ca581d61adea732f5c090fcb9454eaecab7f4..59aaaf9d1e51ad08c3c4bb3d389faebd796551d5 100644 (file)
@@ -11,6 +11,7 @@ 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.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 com.google.common.collect.ImmutableList
 import java.util.ArrayList
@@ -41,14 +42,13 @@ class BuilderTemplate extends AbstractBuilderTemplate {
      */
     package static val BUILDER_STR = "Builder";
 
-    static val AUGMENTATION_FIELD_UPPER = AUGMENTATION_FIELD.toFirstUpper
     static val BUILDER = JavaTypeName.create(Builder)
 
     /**
      * Constructs new instance of this class.
      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
      */
-    new(GeneratedType genType, GeneratedType targetType, Set<GeneratedProperty> properties, Type augmentType,
+    new(GeneratedType genType, GeneratedType targetType, Set<BuilderGeneratedProperty> properties, Type augmentType,
             Type keyType) {
         super(genType, targetType, properties, augmentType, keyType)
     }
@@ -341,11 +341,45 @@ class BuilderTemplate extends AbstractBuilderTemplate {
             «val augmentTypeRef = augmentType.importedName»
             «val jlClassRef = CLASS.importedName»
             «val hashMapRef = JU_HASHMAP.importedName»
-            public «type.name» add«AUGMENTATION_FIELD_UPPER»(«jlClassRef»<? extends «augmentTypeRef»> augmentationType, «augmentTypeRef» augmentationValue) {
-                if (augmentationValue == null) {
-                    return remove«AUGMENTATION_FIELD_UPPER»(augmentationType);
+            /**
+              * Add an augmentation to this builder's product.
+              *
+              * @param augmentation augmentation to be added
+              * @return this builder
+              * @throws NullPointerException if {@code augmentation} is null
+              */
+            public «type.name» addAugmentation(«augmentTypeRef» augmentation) {
+                return doAddAugmentation(augmentation.«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»(), augmentation);
+            }
+
+            /**
+              * 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);
+            }
+
+            /**
+              * Remove an augmentation from this builder's product. If this builder does not track such an augmentation
+              * type, this method does nothing.
+              *
+              * @param augmentationType augmentation type to be removed
+              * @return this builder
+              */
+            public «type.name» removeAugmentation(«jlClassRef»<? extends «augmentTypeRef»> augmentationType) {
+                if (this.«AUGMENTATION_FIELD» instanceof «hashMapRef») {
+                    this.«AUGMENTATION_FIELD».remove(augmentationType);
                 }
+                return this;
+            }
 
+            private «type.name» doAddAugmentation(«jlClassRef»<? extends «augmentTypeRef»> augmentationType, «augmentTypeRef» augmentationValue) {
                 if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
                     this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
                 }
@@ -353,13 +387,6 @@ class BuilderTemplate extends AbstractBuilderTemplate {
                 this.«AUGMENTATION_FIELD».put(augmentationType, augmentationValue);
                 return this;
             }
-
-            public «type.name» remove«AUGMENTATION_FIELD_UPPER»(«jlClassRef»<? extends «augmentTypeRef»> augmentationType) {
-                if (this.«AUGMENTATION_FIELD» instanceof «hashMapRef») {
-                    this.«AUGMENTATION_FIELD».remove(augmentationType);
-                }
-                return this;
-            }
         «ENDIF»
     '''
 
@@ -429,6 +456,12 @@ class BuilderTemplate extends AbstractBuilderTemplate {
         «ENDFOR»
     '''
 
+    override protected CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props) '''
+        «FOR field : props»
+            this.«field.fieldName» = base.«field.getterName»();
+        «ENDFOR»
+    '''
+
     override protected generateCopyAugmentation(Type implType) {
         val augmentationHolderRef = AugmentationHolder.importedName
         val typeRef = targetType.importedName