Updated code generation
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / BaseTemplate.xtend
1 package org.opendaylight.yangtools.sal.java.api.generator
2
3 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
4 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
5 import java.util.Map
6 import org.opendaylight.yangtools.sal.binding.model.api.Type
7 import org.opendaylight.yangtools.binding.generator.util.Types
8
9 abstract class BaseTemplate {
10     
11     
12     protected val GeneratedType type;
13     protected val Map<String,String> importMap;
14     
15     new(GeneratedType _type) {
16          if (_type== null) {
17             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
18         }
19         this.type = _type;
20         this.importMap = GeneratorUtil.createImports(type)
21     }
22     
23     def packageDefinition () '''package «type.packageName»;'''
24
25     
26     final public def generate() {
27     val _body = body()
28     '''
29     «packageDefinition»
30     «imports»
31     
32     «_body»
33     '''.toString
34     }
35     protected def imports()  ''' 
36         «IF !importMap.empty»
37             «FOR entry : importMap.entrySet»
38                 import «entry.value».«entry.key»;
39             «ENDFOR»
40         «ENDIF»
41         
42     '''
43     
44     protected abstract def CharSequence body();
45
46     // Helper patterns
47     
48     final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
49     
50     /**
51      * Template method which generates the getter method for <code>field</code>
52      * 
53      * @param field 
54      * generated property with data about field which is generated as the getter method
55      * @return string with the getter method source code in JAVA format 
56      */
57     final protected def getterMethod(GeneratedProperty field) {
58     val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
59     '''
60         public «field.returnType.importedName» «prefix»«field.name.toFirstUpper»() {
61             return «field.fieldName»;
62         }
63     '''
64     }
65     
66     /**
67      * Template method which generates the setter method for <code>field</code>
68      * 
69      * @param field 
70      * generated property with data about field which is generated as the setter method
71      * @return string with the setter method source code in JAVA format 
72      */
73     final protected def setterMethod(GeneratedProperty field) '''
74         «val returnType = field.returnType.importedName»
75         public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
76             this.«field.fieldName» = value;
77             return this;
78         }
79     '''
80     
81     final protected def importedName(Type intype) {
82         GeneratorUtil.putTypeIntoImports(type, intype, importMap);
83         GeneratorUtil.getExplicitType(type, intype, importMap)
84     }
85     
86     final protected def importedName(Class cls) {
87         importedName(Types.typeForClass(cls))
88     }
89     
90     /**
91      * Template method which generates method parameters with their types from <code>parameters</code>.
92      * 
93      * @param parameters
94      * group of generated property instances which are transformed to the method parameters
95      * @return string with the list of the method parameters with their types in JAVA format
96      */
97     def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) 
98     '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
99     
100     /**
101      * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
102      * 
103      * @param parameters 
104      * group of generated property instances which are transformed to the sequence of parameter names
105      * @return string with the list of the parameter names of the <code>parameters</code> 
106      */
107     def final protected asArguments(Iterable<GeneratedProperty> parameters) 
108     '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.fieldName»«ENDFOR»«ENDIF»'''
109     
110 }