acca70de9020dd151f5f63351562bb740c740c49
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / InterfaceTemplate.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator
9
10 import java.util.List
11 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
12 import org.opendaylight.yangtools.sal.binding.model.api.Constant
13 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
14 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
15 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
16 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature\rimport org.opendaylight.yangtools.sal.binding.model.api.AnnotationType
17
18 /**
19  * Template for generating JAVA interfaces.
20  */
21 class InterfaceTemplate extends BaseTemplate {
22
23     /**
24      * List of constant instances which are generated as JAVA public static final attributes.
25      */
26     val List<Constant> consts
27
28     /**
29      * List of method signatures which are generated as method declarations.
30      */
31     val List<MethodSignature> methods
32
33     /**
34      * List of enumeration which are generated as JAVA enum type.
35      */
36     val List<Enumeration> enums
37
38     /**
39      * List of generated types which are enclosed inside <code>genType</code>
40      */
41     val List<GeneratedType> enclosedGeneratedTypes
42
43     /**
44      * Creates the instance of this class which is used for generating the interface file source
45      * code from <code>genType</code>.
46      *
47      * @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
48      */
49     new(GeneratedType genType) {
50         super(genType)
51         if (genType == null) {
52             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
53         }
54
55         consts = genType.constantDefinitions
56         methods = genType.methodDefinitions
57         enums = genType.enumerations
58         enclosedGeneratedTypes = genType.enclosedTypes
59     }
60
61     /**
62      * Template method which generate the whole body of the interface.
63      *
64      * @return string with code for interface body in JAVA format
65      */
66     override body() '''
67         «wrapToDocumentation(formatDataForJavaDoc(type))»
68         public interface «type.name»
69             «superInterfaces»
70         {
71
72             «generateInnerClasses»
73
74             «generateEnums»
75
76             «generateConstants»
77
78             «generateMethods»
79
80         }
81
82     '''
83
84
85     def private generateAnnotations(List<AnnotationType> annotations) '''
86         «IF annotations != null && !annotations.empty»
87             «FOR annotation : annotations»
88                 @«annotation.importedName»
89                 «IF annotation.parameters != null && !annotation.parameters.empty»
90                 (
91                 «FOR param : annotation.parameters SEPARATOR ","»
92                     «param.name»=«param.value»
93                 «ENDFOR»
94                 )
95                 «ENDIF»
96             «ENDFOR»
97         «ENDIF»
98     '''
99
100     /**
101      * Template method which generates the interface name declaration.
102      *
103      * @return string with the code for the interface declaration in JAVA format
104      */
105     def private superInterfaces()
106     '''
107     «IF (!type.implements.empty)»
108          extends
109          «FOR type : type.implements SEPARATOR ","»
110              «type.importedName»
111          «ENDFOR»
112      « ENDIF»
113      '''
114
115     /**
116      * Template method which generates inner classes inside this interface.
117      *
118      * @return string with the source code for inner classes in JAVA format
119      */
120     def private generateInnerClasses() '''
121         «IF !enclosedGeneratedTypes.empty»
122             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
123                 «IF (innerClass instanceof GeneratedTransferObject)»
124                     «IF innerClass.unionType»
125                         «val unionTemplate = new UnionTemplate(innerClass)»
126                         «unionTemplate.generateAsInnerClass»
127                         «this.importMap.putAll(unionTemplate.importMap)»
128                     «ELSE»
129                         «val classTemplate = new ClassTemplate(innerClass)»
130                         «classTemplate.generateAsInnerClass»
131                         «this.importMap.putAll(classTemplate.importMap)»
132                     «ENDIF»
133
134                 «ENDIF»
135             «ENDFOR»
136         «ENDIF»
137     '''
138
139     /**
140      * Template method which generates JAVA enum type.
141      *
142      * @return string with inner enum source code in JAVA format
143      */
144     def private generateEnums() '''
145         «IF !enums.empty»
146             «FOR e : enums SEPARATOR "\n"»
147                 «val enumTemplate = new EnumTemplate(e)»
148                 «enumTemplate.generateAsInnerClass»
149             «ENDFOR»
150         «ENDIF»
151     '''
152
153     /**
154      * Template method wich generates JAVA constants.
155      *
156      * @return string with constants in JAVA format
157      */
158     def private generateConstants() '''
159         «IF !consts.empty»
160             «FOR c : consts»
161                 «IF c.name != TypeConstants.PATTERN_CONSTANT_NAME»
162                     public static final «c.type.importedName» «c.name» = «c.value»;
163                 «ENDIF»
164             «ENDFOR»
165         «ENDIF»
166     '''
167
168     /**
169      * Template method which generates the declaration of the methods.
170      *
171      * @return string with the declaration of methods source code in JAVA format
172      */
173     def private generateMethods() '''
174         «IF !methods.empty»
175             «FOR m : methods SEPARATOR "\n"»
176                 «m.comment.asJavadoc»
177                 «m.annotations.generateAnnotations»
178                 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);
179             «ENDFOR»
180         «ENDIF»
181     '''
182
183 }
184