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 final public def generate() {
36 protected def imports() '''
38 «FOR entry : importMap.entrySet»
39 import «entry.value».«entry.key»;
45 protected abstract def CharSequence body();
49 final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
52 * Template method which generates the getter method for <code>field</code>
55 * generated property with data about field which is generated as the getter method
56 * @return string with the getter method source code in JAVA format
58 final protected def getterMethod(GeneratedProperty field) {
59 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
61 public «field.returnType.importedName» «prefix»«field.name.toFirstUpper»() {
62 return «field.fieldName»;
68 * Template method which generates the setter method for <code>field</code>
71 * generated property with data about field which is generated as the setter method
72 * @return string with the setter method source code in JAVA format
74 final protected def setterMethod(GeneratedProperty field) '''
75 «val returnType = field.returnType.importedName»
76 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
77 this.«field.fieldName» = value;
82 final protected def importedName(Type intype) {
83 GeneratorUtil.putTypeIntoImports(type, intype, importMap);
84 GeneratorUtil.getExplicitType(type, intype, importMap)
87 final protected def importedName(Class cls) {
88 importedName(Types.typeForClass(cls))
92 * Template method which generates method parameters with their types from <code>parameters</code>.
95 * group of generated property instances which are transformed to the method parameters
96 * @return string with the list of the method parameters with their types in JAVA format
98 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters)
99 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
102 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
105 * group of generated property instances which are transformed to the sequence of parameter names
106 * @return string with the list of the parameter names of the <code>parameters</code>
108 def final protected asArguments(Iterable<GeneratedProperty> parameters)
109 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.fieldName»«ENDFOR»«ENDIF»'''
113 * Template method which generates JAVA comments.
115 * @param string with the comment for whole JAVA class
116 * @return string with comment in JAVA format
118 def protected CharSequence asJavadoc(String comment) {
119 if (comment==null) return '';
120 val paragraphs = paragraphSplitter.split(comment)
124 «FOR p : paragraphs SEPARATOR "<p>"»