Code generator prototype - Binding specification v2
[mdsal.git] / binding2 / mdsal-binding2-java-api-generator / src / main / twirl / org / opendaylight / mdsal / binding / javav2 / java / api / generator / builderTemplate.scala.txt
index d4324f22bb9bb547e74b18398827144b07645ff9..e7f212af5a03c1cab43f160a9cba01e4e2859d38 100644 (file)
@@ -29,7 +29,7 @@
 
 @(genType: GeneratedType, properties: Set[GeneratedProperty], importedNames: Map[String, String],
 ImportedNamesWithProperties: Map[GeneratedProperty, String], augmentField: GeneratedProperty, copyConstructorHelper: String,
-getterMethods: List[String])
+getterMethods: List[String], parentTypeForBuilderName: String)
 @if(genType != null) {
 @{wrapToDocumentation(formatDataForJavaDocBuilder(importedNames.get("genType")))}
 public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{importedNames.get("genType")}> {
@@ -48,6 +48,7 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
 
     @generateSetters()
 
+    @@Override
     public @{genType.getName} build() {
         return new @{genType.getName}Impl(this);
     }
@@ -69,6 +70,8 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
         @generateEquals()
 
         @generateToString()
+
+        @generateImplementedMethods()
     }
 }
 }
@@ -98,13 +101,14 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
 @generateAugmentField(isPrivate: Boolean) = {
     @if(augmentField != null) {
         @if(isPrivate) {private }
-        @{importedNames.get("map")}<@{importedNames.get("class")}<? extends@{importedNames.get("augmentFieldReturnType")}>,
-        @{importedNames.get("augmentFieldReturnType")}>@{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
+        @{importedNames.get("map")}<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>,
+        @{importedNames.get("augmentFieldReturnType")}> @{augmentField.getName} = @{importedNames.get("collections")}.emptyMap();
     }
 }
 
 @implementedInterfaceGetter() = {
-    public @{importedNames.get("class")}<@{importedNames.get("genType")}> getImplementedInterface() {
+    @@Override
+    public @{importedNames.get("class")}<@{importedNames.get("genType")}> implementedInterface() {
     return @{importedNames.get("genType")}.class;
     }
 }
@@ -190,12 +194,27 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
     }
 }
 
+@generateImplType() = {
+@defining(genType.getName + "Impl") {typeImpl => @typeImpl}
+}
+
+@generateBuilderType() = {
+@defining(genType.getName + "Builder") {typeBuilder => @typeBuilder}
+}
+
+@generateInnerBuilderType() = {
+@defining("(" + genType.getName + "Builder base)") {typeInnerBuilder => @typeInnerBuilder}
+}
+
+@generateInnerType() = {
+@defining("(" + genType.getName + " base)") {innerType => @innerType}
+}
+
 @generateCopyConstructor(impl: Boolean) = {
     @if(impl) {private} else {public}
-    @{genType.getName}
-    @if(impl) {Impl} else {Builder}
-    (@{genType.getName}
-    @if(impl) {Builder} base) {
+    @if(impl) {@generateImplType()} else {@generateBuilderType()}
+    @if(impl) {@generateInnerBuilderType()} else {@generateInnerType()}
+    {
     @{copyConstructorHelper}
     @if(augmentField != null) {
         @if(impl) {
@@ -205,7 +224,7 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
                 break;
             case 1:
                 final @{importedNames.get("map")}.Entry<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> e = base.@{augmentField.getName}.entrySet().iterator().next();
-                this.@{augmentField.getName} = @{importedNames.get("collections")}.<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}>singletonMap(e.getKey(), e.getValue());
+                this.@{augmentField.getName} = @{importedNames.get("collections")}.<@{importedNames.get("class")}<? extends @{importedNames.get("augmentFieldReturnType")}>, @{importedNames.get("augmentFieldReturnType")}> singletonMap(e.getKey(), e.getValue());
                 break;
             default :
                 this.@{augmentField.getName} = new @{importedNames.get("hashMap")}<>(base.@{augmentField.getName});
@@ -241,7 +260,7 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
                 getRestrictions(field.getReturnType).getLengthConstraints)}
             }
         }
-        public @{genType.getName}Builder set@{toFirstUpper(field.getName)}(final @{importedNames.get("augmentFieldReturnType")} value) {
+        public @{genType.getName}Builder set@{toFirstUpper(field.getName)}(final @{toFirstUpper(field.getReturnType.getName)} value) {
         @if(!field.getReturnType.isInstanceOf[GeneratedType] && getRestrictions(field.getReturnType) != null) {
             if (value != null) {
             @if(getRestrictions(field.getReturnType).getRangeConstraints != null && !getRestrictions(field.getReturnType).getRangeConstraints.isEmpty) {
@@ -298,7 +317,7 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
     @if(augmentField != null) {
         @@SuppressWarnings("unchecked")
         @if(addOverride) {@@Override}
-        public <E extends @{importedNames.get("augmentFieldReturnType")}> E get@{toFirstUpper(augmentField.getName)}
+        public <E extends @{importedNames.get("augmentation")}<? super @{genType.getName}>> E get@{toFirstUpper(augmentField.getName)}
         (@{importedNames.get("class")}<E> augmentationType) {
             if (augmentationType == null) {
                 throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
@@ -343,37 +362,58 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
     @if(properties != null) {
         @@Override
         public @{importedNames.get("string")} toString() {
-            @{importedNames.get("stringBuilder")} builder = new @{importedNames.get("stringBuilder")} ("@{genType.getName} [");
-            boolean first = true;
-            @for(property <- properties) {
+            @{importedNames.get("string")} name = "@{genType.getName} [";
+            @{importedNames.get("stringBuilder")} builder = new @{importedNames.get("stringBuilder")}(name);
+            @for((property, index) <- properties.zipWithIndex) {
                 if (@{fieldName(property)} != null) {
-                    if (first) {
-                        first = false;
-                    } else {
-                        builder.append(", ");
-                    }
                     builder.append("@{fieldName(property)}=");
                     @if(property.getReturnType.getName.contains("[")) {
                         builder.append(@{importedNames.get("arrays")}.toString(@{fieldName(property)}));
                     } else {
                         builder.append(@{fieldName(property)});
                     }
+                    @if(properties.size() > 1 && index < properties.size()-1){
+                        builder.append(", ");
+                    }
                 }
             }
             @if(augmentField != null) {
-                if (first) {
-                    first = false;
-                } @{"else"} {
-                    builder.append(", ");
+                @if(!properties.isEmpty()){
+                    final int builderLength = builder.length();
+                    final int builderAdditionalLength = builder.substring(name.length(), builderLength).length();
+                    if (builderAdditionalLength > 2 && !builder.substring(builderLength - 2, builderLength).equals(", ")) {
+                        builder.append(", ");
+                    }
                 }
                 builder.append("@{augmentField.getName}=");
                 builder.append(@{augmentField.getName}.values());
+                return builder.append(']').toString();
+            } else {
+                @if(properties.isEmpty()){
+                    return builder.append(']').toString();
+                } else {
+                    return builder.append(']').toString();
+                }
             }
-            return builder.append(']').toString();
         }
     }
 }
 
+@generateImplementedMethods() = {
+        @@Override
+        public @{importedNames.get("item")}<@{parentTypeForBuilderName}> treeIdentifier() {
+            //TODO implement
+            return null;
+        }
+
+        @@Override
+        public @{importedNames.get("classInstMap")}<@{importedNames.get("augmentation")}<? super @{genType.getName}>>
+        augments() {
+            //TODO implement
+            return null;
+        }
+}
+
 @generateEquals() = {
     @if(!properties.isEmpty || augmentField != null) {
         @@Override
@@ -384,7 +424,11 @@ public class @{genType.getName}Builder implements @{getSimpleNameForBuilder} <@{
             if (!(obj instanceof @{importedNames.get("treeNode")})) {
                 return false;
             }
-            if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("treeNode")})obj).getImplementedInterface)) {
+            if (!(obj instanceof @{importedNames.get("instantiable")})) {
+                return false;
+            }
+            if (!@{importedNames.get("genType")}.class.equals(((@{importedNames.get("instantiable")})obj)
+            .implementedInterface())) {
                 return false;
             }
             @{importedNames.get("genType")} other = (@{importedNames.get("genType")})obj;