Speed up AbstractBuilderTemplate.removeProperty()
[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 static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
11 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.getGetterMethodForNonnull
12 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isGetterMethodName
13 import static extension org.opendaylight.mdsal.binding.spec.naming.BindingMapping.isNonnullMethodName
14
15 import java.util.List
16 import org.opendaylight.mdsal.binding.model.api.AnnotationType
17 import org.opendaylight.mdsal.binding.model.api.Constant
18 import org.opendaylight.mdsal.binding.model.api.Enumeration
19 import org.opendaylight.mdsal.binding.model.api.GeneratedType
20 import org.opendaylight.mdsal.binding.model.api.MethodSignature
21 import org.opendaylight.mdsal.binding.model.api.Type
22 import org.opendaylight.mdsal.binding.model.util.TypeConstants
23 import org.opendaylight.yangtools.yang.binding.CodeHelpers
24
25 /**
26  * Template for generating JAVA interfaces.
27  */
28 class InterfaceTemplate extends BaseTemplate {
29     /**
30      * List of constant instances which are generated as JAVA public static final attributes.
31      */
32     val List<Constant> consts
33
34     /**
35      * List of method signatures which are generated as method declarations.
36      */
37     val List<MethodSignature> methods
38
39     /**
40      * List of enumeration which are generated as JAVA enum type.
41      */
42     val List<Enumeration> enums
43
44     /**
45      * List of generated types which are enclosed inside <code>genType</code>
46      */
47     val List<GeneratedType> enclosedGeneratedTypes
48
49     /**
50      * Creates the instance of this class which is used for generating the interface file source
51      * code from <code>genType</code>.
52      *
53      * @throws NullPointerException if <code>genType</code> is <code>null</code>
54      */
55     new(GeneratedType genType) {
56         super(genType)
57         consts = genType.constantDefinitions
58         methods = genType.methodDefinitions
59         enums = genType.enumerations
60         enclosedGeneratedTypes = genType.enclosedTypes
61     }
62
63     /**
64      * Template method which generate the whole body of the interface.
65      *
66      * @return string with code for interface body in JAVA format
67      */
68     override body() '''
69         «wrapToDocumentation(formatDataForJavaDoc(type))»
70         «type.annotations.generateAnnotations»
71         public interface «type.name»
72             «superInterfaces»
73         {
74
75             «generateInnerClasses»
76
77             «generateEnums»
78
79             «generateConstants»
80
81             «generateMethods»
82
83         }
84
85     '''
86
87     def private generateAnnotations(List<AnnotationType> annotations) '''
88         «IF annotations !== null && !annotations.empty»
89             «FOR annotation : annotations»
90                 «annotation.generateAnnotation»
91             «ENDFOR»
92         «ENDIF»
93     '''
94
95     /**
96      * Template method which generates the interface name declaration.
97      *
98      * @return string with the code for the interface declaration in JAVA format
99      */
100     def private superInterfaces()
101     '''
102     «IF (!type.implements.empty)»
103          extends
104          «FOR type : type.implements SEPARATOR ","»
105              «type.importedName»
106          «ENDFOR»
107      « ENDIF»
108      '''
109
110     /**
111      * Template method which generates inner classes inside this interface.
112      *
113      * @return string with the source code for inner classes in JAVA format
114      */
115     def private generateInnerClasses() '''
116         «IF !enclosedGeneratedTypes.empty»
117             «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
118                 «generateInnerClass(innerClass)»
119             «ENDFOR»
120         «ENDIF»
121     '''
122
123     /**
124      * Template method which generates JAVA enum type.
125      *
126      * @return string with inner enum source code in JAVA format
127      */
128     def private generateEnums() '''
129         «IF !enums.empty»
130             «FOR e : enums SEPARATOR "\n"»
131                 «val enumTemplate = new EnumTemplate(javaType.getEnclosedType(e.identifier), e)»
132                 «enumTemplate.generateAsInnerClass»
133             «ENDFOR»
134         «ENDIF»
135     '''
136
137     /**
138      * Template method wich generates JAVA constants.
139      *
140      * @return string with constants in JAVA format
141      */
142     def private generateConstants() '''
143         «IF !consts.empty»
144             «FOR c : consts»
145                 «IF !c.name.startsWith(TypeConstants.PATTERN_CONSTANT_NAME)»
146                     «emitConstant(c)»
147                 «ENDIF»
148             «ENDFOR»
149         «ENDIF»
150     '''
151
152     /**
153      * Template method which generates the declaration of the methods.
154      *
155      * @return string with the declaration of methods source code in JAVA format
156      */
157     def private generateMethods() '''
158         «IF !methods.empty»
159             «FOR m : methods SEPARATOR "\n"»
160                 «IF m.isDefault»
161                     «generateDefaultMethod(m)»
162                 «ELSEIF m.parameters.empty && m.name.isGetterMethodName»
163                     «generateAccessorMethod(m)»
164                 «ELSE»
165                     «generateMethod(m)»
166                 «ENDIF»
167             «ENDFOR»
168         «ENDIF»
169     '''
170
171     def private generateDefaultMethod(MethodSignature method) {
172         if (method.name.isNonnullMethodName) {
173             generateNonnullMethod(method)
174         } else {
175             switch method.name {
176                 case DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME : generateDefaultImplementedInterface
177             }
178         }
179     }
180
181     def private generateMethod(MethodSignature method) '''
182         «method.comment.asJavadoc»
183         «method.annotations.generateAnnotations»
184         «method.returnType.importedName» «method.name»(«method.parameters.generateParameters»);
185     '''
186
187     def private generateAccessorMethod(MethodSignature method) '''
188         «val ret = method.returnType»
189         «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or " + asCode("null") + " if not present")»
190         «method.annotations.generateAnnotations»
191         «nullableType(ret)» «method.name»();
192     '''
193
194     def private generateDefaultImplementedInterface() '''
195         @«Override.importedName»
196         default «Class.importedName»<«type.fullyQualifiedName»> «DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»() {
197             return «type.fullyQualifiedName».class;
198         }
199     '''
200
201     def private generateNonnullMethod(MethodSignature method) '''
202         «val ret = method.returnType»
203         «val name = method.name»
204         «formatDataForJavaDoc(method, "@return " + asCode(ret.fullyQualifiedName) + " " + asCode(propertyNameFromGetter(method)) + ", or an empty list if it is not present")»
205         «method.annotations.generateAnnotations»
206         default «ret.importedNonNull» «name»() {
207             return «CodeHelpers.importedName».nonnull(«getGetterMethodForNonnull(name)»());
208         }
209     '''
210
211     def private String nullableType(Type type) {
212         if (type.isObject) {
213             return type.importedNullable
214         }
215         return type.importedName
216     }
217
218     def private static boolean isObject(Type type) {
219         // The return type has a package, so it's not a primitive type
220         return !type.getPackageName().isEmpty()
221     }
222 }