blob: fa66957365f6c546e577a8a5831d0ee01f0705a9 [file] [log] [blame]
#*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*#
#parse ( "common.vm" )
#
#set ( $package = "${packageModelV4}" )
#set ( $root = $model.getClass( $model.getRoot($version), $version ) )
#foreach ( $class in $model.allClasses )
#set ( $ancestors = $Helper.ancestors( $class ) )
#set ( $allFields = [] )
#set ( $inheritedFields = [] )
#foreach ( $cl in $ancestors )
#if ( $cl != $class )
#set ( $dummy = $inheritedFields.addAll( $cl.getFields($version) ) )
#end
#set ( $dummy = $allFields.addAll( $cl.getFields($version) ) )
#end
#set ( $className = "${class.name}" )
#MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/ImmutableCollections.java
#parse ( "java/ImmutableCollections.java" )
#MODELLO-VELOCITY#SAVE-OUTPUT-TO ${package.replace('.','/')}/${className}.java
#if ( $class.name != "InputLocation" && $class.name != "InputSource" )
#set ( $types = { } )
#set ( $imports = $class.getClass().forName("java.util.TreeSet").newInstance() )
#set ( $dummy = $imports.add( "java.io.Serializable" ) )
#set ( $dummy = $imports.add( "java.util.Collections" ) )
#set ( $dummy = $imports.add( "java.util.HashMap" ) )
#set ( $dummy = $imports.add( "java.util.Map" ) )
#set ( $dummy = $imports.add( "org.apache.maven.api.annotations.Experimental" ) )
#set ( $dummy = $imports.add( "org.apache.maven.api.annotations.Generated" ) )
#set ( $dummy = $imports.add( "org.apache.maven.api.annotations.Immutable" ) )
#set ( $dummy = $imports.add( "org.apache.maven.api.annotations.Nonnull" ) )
#set ( $dummy = $imports.add( "org.apache.maven.api.annotations.NotThreadSafe" ) )
#set ( $dummy = $imports.add( "org.apache.maven.api.annotations.ThreadSafe" ) )
#foreach ( $field in $allFields )
#if ( $field.type == "java.util.List" )
#set ( $dummy = $imports.add( "java.util.ArrayList" ) )
#set ( $dummy = $imports.add( "java.util.Collection" ) )
#set ( $dummy = $imports.add( "java.util.List" ) )
#set ( $dummy = $types.put( $field, "List<" + $field.to + ">" ) )
#elseif ( $field.type == "java.util.Properties" )
#set ( $dummy = $imports.add( "java.util.Map" ) )
#set ( $dummy = $types.put( $field, "Map<String, String>" ) )
#elseif ( $field.type == "DOM" )
#set ( $dummy = $imports.add( "org.apache.maven.api.xml.XmlNode" ) )
#set ( $dummy = $types.put( $field, "XmlNode" ) )
#else
#set ( $fieldType = ${types.getOrDefault($field.type,$field.type)} )
#set ( $idx = $fieldType.lastIndexOf('.') )
#if ( $idx > 0 )
#set ( $dummy = $imports.add( $fieldType ) )
#set ( $dummy = $types.put( $fieldType, $fieldType.substring( $idx + 1 ) ) )
#end
#end
#end
#set ( $eq = "" )
#set ( $hc = "" )
#foreach ( $field in $allFields )
#if ( $field.identifier )
#set ( $dummy = $imports.add( "java.util.Objects" ) )
#set ( $dummy = $identifiers.add( $field ) )
#if ( $eq == "" )
#set ( $eq = "Objects.equals( this.${field.name}, that.${field.name} )" )
#else
#set ( $eq = "$eq && Objects.equals( this.${field.name}, that.${field.name} )" )
#end
#if ( $hc == "" )
#set ( $hc = "${field.name}" )
#else
#set ( $hc = "$hc, this.${field.name}" )
#end
#end
#end
// =================== DO NOT EDIT THIS FILE ====================
// Generated by Modello Velocity from ${template}
// template, any modifications will be overwritten.
// ==============================================================
package ${package};
#foreach ( $imp in $imports )
import $imp;
#end
/**
#foreach ( $line in ${class.description.trim().split("\n")} )
* ${line.trim()}
#end
*/
@Experimental
@Generated @ThreadSafe @Immutable
public class ${class.name}
#if ( $class.superClass )
extends ${class.superClass}
#end
#if ( $locationTracking )
implements Serializable, InputLocationTracker
#else
implements Serializable
#end
{
#if ( $class == $root )
final String modelEncoding;
#end
#foreach ( $field in $class.getFields($version) )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
/**
#foreach ( $line in ${field.description.trim().split("\n")} )
* ${line.trim()}
#end
*/
final ${type} $field.name;
#end
#if ( $locationTracking )
#if ( ! $class.superClass )
/** Locations */
final Map<Object, InputLocation> locations;
#end
#end
/**
* Constructor for this class, package protected.
* @see Builder#build()
*/
${class.name}(
#if ( $class == $root )
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
) {
#if ( $class.superClass )
super(
#foreach ( $field in $inheritedFields )
#set ( $sep = "#if(${locationTracking}||$field!=${inheritedFields[${inheritedFields.size()} - 1]}),#end" )
${field.name}${sep}
#end
#if ( $locationTracking )
locations
#end
);
#end
#if ( $class == $root )
this.modelEncoding = modelEncoding;
#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});
#else
this.${field.name} = ${field.name};
#end
#end
#if ( $locationTracking )
#if ( ! $class.superClass )
this.locations = ImmutableCollections.copy(locations);
#end
#end
}
#if ( ! $eq.empty )
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || !(o instanceof ${class.name})) {
return false;
}
${class.name} that = (${class.name}) o;
return ${eq};
}
@Override
public int hashCode() {
return Objects.hash(${hc});
}
#end
#if ( $class == $root )
public String getModelEncoding() {
return modelEncoding;
}
#end
#foreach ( $field in $class.getFields($version) )
#set ( $cap = $Helper.capitalise( $field.name ) )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $type == "boolean" || $type == "Boolean" )
#set ( $pfx = "is" )
#else
#set ( $pfx = "get" )
#end
/**
#set ( $desc = ${field.description.trim()} )
#foreach ( $line in ${desc.split("\n")} )
* ${line.trim()}
#end
*
* @return a {@code ${type}}
*/
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" )
@Nonnull
#end
public ${type} ${pfx}${cap}() {
return this.${field.name};
}
#end
#if ( $locationTracking && !$class.superClass )
/**
* Gets the location of the specified field in the input source.
*/
public InputLocation getLocation(Object key) {
return locations != null ? locations.get(key) : null;
}
#end
/**
* Creates a new builder with this object as the basis.
*
* @return a {@code Builder}
*/
@Nonnull
public Builder with() {
return newBuilder(this);
}
#foreach ( $field in $allFields )
#set ( $cap = $Helper.capitalise( $field.name ) )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $type.startsWith("List<") )
#set ( $type = ${type.replace('List<','Collection<')} )
#end
/**
* Creates a new {@code ${class.name}} instance using the specified ${field.name}.
*
* @param ${field.name} the new {@code $type} to use
* @return a {@code ${class.name}} with the specified ${field.name}
*/
@Nonnull
public ${class.name} with${cap}($type $field.name) {
return newBuilder(this, true).${field.name}($field.name).build();
}
#end
/**
* Creates a new {@code ${class.name}} instance.
* Equivalent to {@code newInstance(true)}.
* @see #newInstance(boolean)
*
* @return a new {@code ${class.name}}
*/
@Nonnull
public static ${class.name} newInstance() {
return newInstance(true);
}
/**
* Creates a new {@code ${class.name}} instance using default values or not.
* Equivalent to {@code newBuilder(withDefaults).build()}.
*
* @param withDefaults the boolean indicating whether default values should be used
* @return a new {@code ${class.name}}
*/
@Nonnull
public static ${class.name} newInstance(boolean withDefaults) {
return newBuilder(withDefaults).build();
}
/**
* Creates a new {@code ${class.name}} builder instance.
* Equivalent to {@code newBuilder(true)}.
* @see #newBuilder(boolean)
*
* @return a new {@code Builder}
*/
@Nonnull
public static Builder newBuilder() {
return newBuilder(true);
}
/**
* Creates a new {@code ${class.name}} builder instance using default values or not.
*
* @param withDefaults the boolean indicating whether default values should be used
* @return a new {@code Builder}
*/
@Nonnull
public static Builder newBuilder(boolean withDefaults) {
return new Builder(withDefaults);
}
/**
* Creates a new {@code ${class.name}} builder instance using the specified object as a basis.
* Equivalent to {@code newBuilder(from, false)}.
*
* @param from the {@code ${class.name}} instance to use as a basis
* @return a new {@code Builder}
*/
@Nonnull
public static Builder newBuilder(${class.name} from) {
return newBuilder(from, false);
}
/**
* Creates a new {@code ${class.name}} builder instance using the specified object as a basis.
*
* @param from the {@code ${class.name}} instance to use as a basis
* @param forceCopy the boolean indicating if a copy should be forced
* @return a new {@code Builder}
*/
@Nonnull
public static Builder newBuilder(${class.name} from, boolean forceCopy) {
return new Builder(from, forceCopy);
}
/**
* Builder class used to create ${class.name} instances.
* @see #with()
* @see #newBuilder()
*/
@NotThreadSafe
public static class Builder
#if ( $class.superClass )
extends ${class.superClass}.Builder
#end
{
${class.name} base;
#if ( $class == $root )
String modelEncoding;
#end
#foreach ( $field in $class.getFields($version) )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $type.startsWith("List<") )
#set ( $type = ${type.replace('List<','Collection<')} )
#end
#if ( $type == 'boolean' )
Boolean ${field.name};
#elseif ( $type == 'int' )
Integer ${field.name};
#else
${type} ${field.name};
#end
#end
#if ( ! $class.superClass && $locationTracking )
Map<Object, InputLocation> locations;
#end
Builder(boolean withDefaults) {
#if ( $class.superClass )
super(withDefaults);
#end
if (withDefaults) {
#foreach ( $field in $class.getFields($version) )
#if ( $field.defaultValue )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $field.type == "String" )
this.${field.name} = "${field.defaultValue}";
#elseif ( $field.type != "java.util.List" && $field.type != "java.util.Properties" )
this.${field.name} = ${field.defaultValue};
#end
#end
#end
}
}
Builder(${class.name} base, boolean forceCopy) {
#if ( $class.superClass )
super(base, forceCopy);
#end
if (forceCopy) {
#foreach ( $field in $class.getFields($version) )
this.${field.name} = base.${field.name};
#end
#if ( $locationTracking )
this.locations = base.locations;
#end
} else {
this.base = base;
}
}
#if ( $class == $root )
@Nonnull
public Builder modelEncoding(String modelEncoding) {
this.modelEncoding = modelEncoding;
return this;
}
#end
#foreach ( $field in $allFields )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $type.startsWith("List<") )
#set ( $type = ${type.replace('List<','Collection<')} )
#end
@Nonnull
public Builder ${field.name}(${type} ${field.name}) {
this.${field.name} = ${field.name};
return this;
}
#end
#if ( $locationTracking )
@Nonnull
public Builder location(Object key, InputLocation location) {
if (location != null) {
if (!(this.locations instanceof HashMap)) {
this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
}
this.locations.put(key, location);
}
return this;
}
#end
@Nonnull
public ${class.name} build() {
if (base != null
#foreach ( $field in $allFields )
&& (${field.name} == null || ${field.name} == base.${field.name})
#end
) {
return base;
}
#if ( $locationTracking )
Map<Object, InputLocation> locations = null;
InputLocation location = null;
#foreach ( $field in $allFields )
InputLocation ${field.name}Location = null;
#end
if (this.locations != null) {
locations = this.locations;
}
#end
return new ${class.name}(
#if ( $class == $root )
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 != null ? locations : (base != null ? base.locations : null)
#end
);
}
}
#foreach ( $cs in $class.getCodeSegments($version) )
$cs.code
#end
}
#end
#end