1 package org.opendaylight.yangtools.sal.java.api.generator
3 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
4 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
6 import org.opendaylight.yangtools.sal.binding.model.api.Type
7 import org.opendaylight.yangtools.binding.generator.util.Types
8 import com.google.common.base.Splitter
10 abstract class BaseTemplate {
13 protected val GeneratedType type;
14 protected val Map<String,String> importMap;
15 static val paragraphSplitter = Splitter.on("\n\n").omitEmptyStrings();
16 new(GeneratedType _type) {
18 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
21 this.importMap = GeneratorUtil.createImports(type)
24 def packageDefinition () '''package «type.packageName»;'''
27 protected def getFullyQualifiedName() {
28 return type.fullyQualifiedName
31 final public def generate() {
40 protected def imports() '''
42 «FOR entry : importMap.entrySet»
43 «IF entry.value != fullyQualifiedName»
44 import «entry.value».«entry.key»;
51 protected abstract def CharSequence body();
55 final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
58 * Template method which generates the getter method for <code>field</code>
61 * generated property with data about field which is generated as the getter method
62 * @return string with the getter method source code in JAVA format
64 final protected def getterMethod(GeneratedProperty field) {
66 public «field.returnType.importedName» «field.getterMethodName»() {
67 return «field.fieldName»;
72 final protected def getterMethodName(GeneratedProperty field) {
73 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
74 return '''«prefix»«field.name.toFirstUpper»'''
78 * Template method which generates the setter method for <code>field</code>
81 * generated property with data about field which is generated as the setter method
82 * @return string with the setter method source code in JAVA format
84 final protected def setterMethod(GeneratedProperty field) '''
85 «val returnType = field.returnType.importedName»
86 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
87 this.«field.fieldName» = value;
92 final protected def importedName(Type intype) {
93 GeneratorUtil.putTypeIntoImports(type, intype, importMap);
94 GeneratorUtil.getExplicitType(type, intype, importMap)
97 final protected def importedName(Class cls) {
98 importedName(Types.typeForClass(cls))
102 * Template method which generates method parameters with their types from <code>parameters</code>.
105 * group of generated property instances which are transformed to the method parameters
106 * @return string with the list of the method parameters with their types in JAVA format
108 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters)
109 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
112 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
115 * group of generated property instances which are transformed to the sequence of parameter names
116 * @return string with the list of the parameter names of the <code>parameters</code>
118 def final protected asArguments(Iterable<GeneratedProperty> parameters)
119 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.fieldName»«ENDFOR»«ENDIF»'''
123 * Template method which generates JAVA comments.
125 * @param comment string with the comment for whole JAVA class
126 * @return string with comment in JAVA format
128 def protected CharSequence asJavadoc(String comment) {
129 if (comment==null) return '';
130 val paragraphs = paragraphSplitter.split(comment)
134 «FOR p : paragraphs SEPARATOR "<p>"»