Added javadoc to generated types
[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 import com.google.common.base.Splitter
9
10 abstract class BaseTemplate {
11     
12     
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) {
17          if (_type== null) {
18             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
19         }
20         this.type = _type;
21         this.importMap = GeneratorUtil.createImports(type)
22     }
23     
24     def packageDefinition () '''package «type.packageName»;'''
25
26     
27     final public def generate() {
28     val _body = body()
29     '''
30     «packageDefinition»
31     «imports»
32     
33     «_body»
34     '''.toString
35     }
36     protected def imports()  ''' 
37         «IF !importMap.empty»
38             «FOR entry : importMap.entrySet»
39                 import «entry.value».«entry.key»;
40             «ENDFOR»
41         «ENDIF»
42         
43     '''
44     
45     protected abstract def CharSequence body();
46
47     // Helper patterns
48     
49     final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
50     
51     /**
52      * Template method which generates the getter method for <code>field</code>
53      * 
54      * @param field 
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 
57      */
58     final protected def getterMethod(GeneratedProperty field) {
59     val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
60     '''
61         public «field.returnType.importedName» «prefix»«field.name.toFirstUpper»() {
62             return «field.fieldName»;
63         }
64     '''
65     }
66     
67     /**
68      * Template method which generates the setter method for <code>field</code>
69      * 
70      * @param field 
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 
73      */
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;
78             return this;
79         }
80     '''
81     
82     final protected def importedName(Type intype) {
83         GeneratorUtil.putTypeIntoImports(type, intype, importMap);
84         GeneratorUtil.getExplicitType(type, intype, importMap)
85     }
86     
87     final protected def importedName(Class cls) {
88         importedName(Types.typeForClass(cls))
89     }
90     
91     /**
92      * Template method which generates method parameters with their types from <code>parameters</code>.
93      * 
94      * @param parameters
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
97      */
98     def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters) 
99     '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
100     
101     /**
102      * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
103      * 
104      * @param parameters 
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> 
107      */
108     def final protected asArguments(Iterable<GeneratedProperty> parameters) 
109     '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.fieldName»«ENDFOR»«ENDIF»'''
110     
111     
112         /**
113      * Template method which generates JAVA comments.
114      * 
115      * @param string with the comment for whole JAVA class
116      * @return string with comment in JAVA format
117      */
118     def protected CharSequence asJavadoc(String comment) {
119         if (comment==null) return '';
120         val paragraphs = paragraphSplitter.split(comment)
121         
122         return '''
123             /**
124               «FOR p : paragraphs SEPARATOR "<p>"»
125               «p»
126               «ENDFOR»
127             **/
128             '''
129     }
130 }