Rework inlined union generation
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / 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.mdsal.binding.java.api.generator
9
10 import java.util.List
11 import org.opendaylight.mdsal.binding.model.api.AnnotationType
12 import org.opendaylight.mdsal.binding.model.api.Constant
13 import org.opendaylight.mdsal.binding.model.api.Enumeration
14 import org.opendaylight.mdsal.binding.model.api.GeneratedType
15 import org.opendaylight.mdsal.binding.model.api.MethodSignature
16 import org.opendaylight.mdsal.binding.model.util.TypeConstants
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         «type.annotations.generateAnnotations»
69         public interface «type.name»
70             «superInterfaces»
71         {
72
73             «generateInnerClasses»
74
75             «generateEnums»
76
77             «generateConstants»
78
79             «generateMethods»
80
81         }
82
83     '''
84
85
86     def private generateAnnotations(List<AnnotationType> annotations) '''
87         «IF annotations !== null && !annotations.empty»
88             «FOR annotation : annotations»
89                 @«annotation.importedName»
90                 «IF annotation.parameters !== null && !annotation.parameters.empty»
91                 (
92                 «FOR param : annotation.parameters SEPARATOR ","»
93                     «param.name»=«param.value»
94                 «ENDFOR»
95                 )
96                 «ENDIF»
97             «ENDFOR»
98         «ENDIF»
99     '''
100
101     /**
102      * Template method which generates the interface name declaration.
103      *
104      * @return string with the code for the interface declaration in JAVA format
105      */
106     def private superInterfaces()
107     '''
108     «IF (!type.implements.empty)»
109          extends
110          «FOR type : type.implements SEPARATOR ","»
111              «type.importedName»
112          «ENDFOR»
113      « ENDIF»
114      '''
115
116     /**
117      * Template method which generates inner classes inside this interface.
118      *
119      * @return string with the source code for inner classes in JAVA format
120      */
121     def private generateInnerClasses() '''
122         «IF !enclosedGeneratedTypes.empty»
123             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
124                 «generateInnerClass(innerClass)»
125             «ENDFOR»
126         «ENDIF»
127     '''
128
129     /**
130      * Template method which generates JAVA enum type.
131      *
132      * @return string with inner enum source code in JAVA format
133      */
134     def private generateEnums() '''
135         «IF !enums.empty»
136             «FOR e : enums SEPARATOR "\n"»
137                 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
138                 «enumTemplate.generateAsInnerClass»
139             «ENDFOR»
140         «ENDIF»
141     '''
142
143     /**
144      * Template method wich generates JAVA constants.
145      *
146      * @return string with constants in JAVA format
147      */
148     def private generateConstants() '''
149         «IF !consts.empty»
150             «FOR c : consts»
151                 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
152                     «emitConstant(c)»
153                 «ENDIF»
154             «ENDFOR»
155         «ENDIF»
156     '''
157
158     /**
159      * Template method which generates the declaration of the methods.
160      *
161      * @return string with the declaration of methods source code in JAVA format
162      */
163     def private generateMethods() '''
164         «IF !methods.empty»
165             «FOR m : methods SEPARATOR "\n"»
166                 «IF !m.isAccessor»
167                     «m.comment.asJavadoc»
168                 «ELSE»
169                     «formatDataForJavaDoc(m, "@return " + asCode(m.returnType.fullyQualifiedName) + " "
170                     + asCode(propertyNameFromGetter(m)) + ", or " + asCode("null") + " if not present")»
171                 «ENDIF»
172                 «m.annotations.generateAnnotations»
173                 «m.returnType.importedName» «m.name»(«m.parameters.generateParameters»);
174             «ENDFOR»
175         «ENDIF»
176     '''
177
178 }
179