Skip to content

Commit

Permalink
[MNG-8214] Improve model velocity template to support subclasses
Browse files Browse the repository at this point in the history
This makes the constructor protected and takes the Builder as argument
  • Loading branch information
kwin committed Aug 17, 2024
1 parent 1ee18d3 commit 93079ea
Showing 1 changed file with 17 additions and 45 deletions.
62 changes: 17 additions & 45 deletions src/mdo/model.vm
Original file line number Diff line number Diff line change
Expand Up @@ -142,46 +142,33 @@ public class ${class.name}
#end

/**
* Constructor for this class, package protected.
* Constructor for this class, to be called from {@link Builder} and its subclasses.
* @see Builder#build()
*/
${class.name}(
#if ( $class == $root )
String namespaceUri,
String modelEncoding,
#end
#foreach ( $field in $allFields )
#set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $type.startsWith("List<") )
#set ( $type = ${type.replace('List<','Collection<')} )
#end
$type $field.name${sep}
#end
#if ( $locationTracking )
Map<Object, InputLocation> locations
#end
) {
#set ( $additionalArguments = "#if(${locationTracking}), Map<Object, InputLocation> locations#end" )
protected ${class.name}(Builder builder${additionalArguments}) {
#if ( $class.superClass )
super(
#foreach ( $field in $inheritedFields )
#set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
${field.name}${sep}
#end
#set ( $sep = "#if(${locationTracking}),#end" )
builder${sep}
#if ( $locationTracking )
locations
#end
);
#end
#if ( $class == $root )
this.namespaceUri = namespaceUri;
this.modelEncoding = modelEncoding;
this.namespaceUri = builder.namespaceUri != null ? builder.namespaceUri : (builder.base != null ? builder.base.namespaceUri : null);
this.modelEncoding = builder.modelEncoding != null ? builder.modelEncoding : (builder.base != null ? builder.base.modelEncoding : null);
#end
#foreach ( $field in $class.getFields($version) )
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" || $field.type == "java.util.Map" )
this.${field.name} = ImmutableCollections.copy(${field.name});
this.${field.name} = ImmutableCollections.copy(builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : null));
#else
this.${field.name} = ${field.name};
#if ( $field.type == "boolean" || $field.type == "int" )
this.${field.name} = builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : ${field.defaultValue});
#else
this.${field.name} = builder.${field.name} != null ? builder.${field.name} : (builder.base != null ? builder.base.${field.name} : null);
#end
#end
#end
#if ( $locationTracking )
Expand Down Expand Up @@ -384,7 +371,7 @@ public class ${class.name}
Map<Object, InputLocation> locations;
#end

Builder(boolean withDefaults) {
protected Builder(boolean withDefaults) {
#if ( $class.superClass )
super(withDefaults);
#end
Expand All @@ -402,7 +389,7 @@ public class ${class.name}
}
}

Builder(${class.name} base, boolean forceCopy) {
protected Builder(${class.name} base, boolean forceCopy) {
#if ( $class.superClass )
super(base, forceCopy);
#end
Expand Down Expand Up @@ -480,23 +467,8 @@ public class ${class.name}
locations.put("${field.name}", newlocs.containsKey("${field.name}") ? newlocs.get("${field.name}") : oldlocs.get("${field.name}"));
#end
#end
return new ${class.name}(
#if ( $class == $root )
namespaceUri != null ? namespaceUri : (base != null ? base.namespaceUri : ""),
modelEncoding != null ? modelEncoding : (base != null ? base.modelEncoding : "UTF-8"),
#end
#foreach ( $field in $allFields )
#set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" )
#if ( $field.type == "boolean" || $field.type == "int" )
${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : ${field.defaultValue})${sep}
#else
${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : null)${sep}
#end
#end
#if ( $locationTracking )
locations
#end
);
#set ( $additionalArguments = "#if(${locationTracking}), locations#end" )
return new ${class.name}(this${additionalArguments});
}
}

Expand Down

0 comments on commit 93079ea

Please sign in to comment.