1 package org.opendaylight.yangtools.sal.java.api.generator
\r
3 import java.util.List
\r
4 import org.opendaylight.yangtools.binding.generator.util.TypeConstants
\r
5 import org.opendaylight.yangtools.sal.binding.model.api.Constant
\r
6 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
\r
7 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
\r
8 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
\r
9 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
\r
10 import java.util.ArrayList
\r
11 import java.util.Collections
\rimport java.util.Arrays
12 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
13 import com.google.common.collect.Range
16 * Template for generating JAVA class.
\r
18 class ClassTemplate extends BaseTemplate {
\r
20 protected val List<GeneratedProperty> properties
\r
21 protected val List<GeneratedProperty> finalProperties
\r
22 protected val List<GeneratedProperty> parentProperties
\r
23 protected val Iterable<GeneratedProperty> allProperties;
\r
24 protected val Restrictions restrictions
\r
27 * List of enumeration which are generated as JAVA enum type.
\r
29 protected val List<Enumeration> enums
\r
32 * List of constant instances which are generated as JAVA public static final attributes.
\r
34 protected val List<Constant> consts
\r
37 * List of generated types which are enclosed inside <code>genType</code>
\r
39 protected val List<GeneratedType> enclosedGeneratedTypes;
\r
42 protected val GeneratedTransferObject genTO;
\r
45 * Creates instance of this class with concrete <code>genType</code>.
\r
47 * @param genType generated transfer object which will be transformed to JAVA class source code
\r
49 new(GeneratedTransferObject genType) {
\r
51 this.genTO = genType
\r
52 this.properties = genType.properties
\r
53 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
\r
54 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
\r
55 this.restrictions = genType.restrictions
\r
57 var List<GeneratedProperty> sorted = new ArrayList<GeneratedProperty>();
\r
58 sorted.addAll(properties);
\r
59 sorted.addAll(parentProperties);
\r
60 Collections.sort(sorted, new PropertyComparator());
\r
62 this.allProperties = sorted
\r
63 this.enums = genType.enumerations
\r
64 this.consts = genType.constantDefinitions
\r
65 this.enclosedGeneratedTypes = genType.enclosedTypes
\r
73 * Generates JAVA class source code (class body only).
\r
75 * @return string with JAVA class body source code
\r
77 def CharSequence generateAsInnerClass() {
\r
78 return generateBody(true)
\r
83 override protected body() {
\r
84 generateBody(false);
\r
88 * Template method which generates class body.
\r
90 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
91 * @return string with class source code in JAVA format
\r
93 def protected generateBody(boolean isInnerClass) '''
\r
94 «type.comment.asJavadoc»
\r
95 «generateClassDeclaration(isInnerClass)» {
\r
96 «innerClassesDeclarations»
\r
98 «constantsDeclarations»
\r
101 «FOR field : properties SEPARATOR "\n"»
\r
102 «field.getterMethod»
\r
103 «IF !field.readOnly»
\r
104 «field.setterMethod»
\r
114 «generateGetLength»
\r
121 * Template method which generates inner classes inside this interface.
\r
123 * @return string with the source code for inner classes in JAVA format
\r
125 def protected innerClassesDeclarations() '''
\r
126 «IF !enclosedGeneratedTypes.empty»
\r
127 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
\r
128 «IF (innerClass instanceof GeneratedTransferObject)»
\r
129 «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
\r
130 «classTemplate.generateAsInnerClass»
\r
138 def protected constructors() '''
\r
139 «IF genTO.unionType»
\r
140 «genUnionConstructor»
\r
142 «allValuesConstructor»
\r
144 «IF !allProperties.empty»
\r
147 «IF properties.empty && !parentProperties.empty »
\r
148 «parentConstructor»
\r
152 def protected allValuesConstructor() '''
\r
153 public «type.name»(«allProperties.asArgumentsDeclaration») {
\r
154 «IF false == parentProperties.empty»
\r
155 super(«parentProperties.asArguments»);
\r
157 «FOR p : allProperties»
\r
158 «generateLengthRestrictions(type, p.fieldName.toString, p.returnType)»
\r
160 «FOR p : properties»
\r
161 this.«p.fieldName» = «p.fieldName»;
\r
166 def protected genUnionConstructor() '''
\r
167 «FOR p : allProperties»
\r
168 «val List<GeneratedProperty> other = new ArrayList(properties)»
\r
169 «val added = other.remove(p)»
\r
170 «genConstructor(p, other)»
\r
175 def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
\r
176 public «type.name»(«property.returnType.importedName + " " + property.name») {
\r
177 «IF false == parentProperties.empty»
\r
178 super(«parentProperties.asArguments»);
\r
180 «generateLengthRestrictions(type, property.fieldName.toString, property.returnType)»
\r
181 this.«property.fieldName» = «property.name»;
\r
183 this.«p.fieldName» = null;
\r
188 def protected copyConstructor() '''
\r
190 * Creates a copy from Source Object.
\r
192 * @param source Source object
\r
194 public «type.name»(«type.name» source) {
\r
195 «IF false == parentProperties.empty»
\r
198 «FOR p : properties»
\r
199 this.«p.fieldName» = source.«p.fieldName»;
\r
204 def protected parentConstructor() '''
\r
206 * Creates a new instance from «genTO.superType.importedName»
\r
208 * @param source Source object
\r
210 public «type.name»(«genTO.superType.importedName» source) {
\r
218 * Template method which generates JAVA class declaration.
\r
220 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
221 * @return string with class declaration in JAVA format
\r
223 def protected generateClassDeclaration(boolean isInnerClass) '''
\r
225 IF (isInnerClass)»«
\r
227 ELSEIF (type.abstract)»«
\r
231 ENDIF»class «type.name»«
\r
232 IF (genTO.superType != null)»«
\r
233 " extends "»«genTO.superType.importedName»«
\r
235 «IF (!type.implements.empty)»«
\r
237 FOR type : type.implements SEPARATOR ", "»«
\r
238 type.importedName»«
\r
244 * Template method which generates JAVA enum type.
\r
246 * @return string with inner enum source code in JAVA format
\r
248 def protected enumDeclarations() '''
\r
250 «FOR e : enums SEPARATOR "\n"»
\r
251 «val enumTemplate = new EnumTemplate(e)»
\r
252 «enumTemplate.generateAsInnerClass»
\r
258 * Template method wich generates JAVA constants.
\r
260 * @return string with constants in JAVA format
\r
262 def protected constantsDeclarations() '''
\r
265 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
\r
266 «val cValue = c.value»
\r
267 «IF cValue instanceof List<?>»
\r
268 «val cValues = cValue as List<?>»
\r
269 private static final List<Pattern> «Constants.MEMBER_PATTERN_LIST» = new ArrayList<Pattern>();
\r
270 public static final List<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «Arrays.importedName».asList(«
\r
271 FOR v : cValues SEPARATOR ", "»«
\r
272 IF v instanceof String»"«
\r
277 «generateStaticInicializationBlock»
\r
280 public static final «c.type.importedName» «c.name» = «c.value»;
\r
287 * Template method which generates JAVA static initialization block.
\r
289 * @return string with static initialization block in JAVA format
\r
291 def protected generateStaticInicializationBlock() '''
\r
293 for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
\r
294 «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
\r
300 * Template method which generates JAVA class attributes.
\r
302 * @return string with the class attributes in JAVA format
\r
304 def protected generateFields() '''
\r
305 «IF !properties.empty»
\r
306 «FOR f : properties»
\r
307 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
\r
314 * Template method which generates the method <code>hashCode()</code>.
\r
316 * @return string with the <code>hashCode()</code> method definition in JAVA format
\r
318 def protected generateHashCode() '''
\r
319 «IF !genTO.hashCodeIdentifiers.empty»
\r
321 public int hashCode() {
\r
322 final int prime = 31;
\r
324 «FOR property : genTO.hashCodeIdentifiers»
\r
325 «IF property.returnType.name.contains("[")»
\r
326 result = prime * result + ((«property.fieldName» == null) ? 0 : «Arrays.importedName».hashCode(«property.fieldName»));
\r
328 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
\r
337 * Template method which generates the method <code>equals()</code>.
\r
339 * @return string with the <code>equals()</code> method definition in JAVA format
\r
341 def protected generateEquals() '''
\r
342 «IF !genTO.equalsIdentifiers.empty»
\r
344 public boolean equals(java.lang.Object obj) {
\r
351 if (getClass() != obj.getClass()) {
\r
354 «type.name» other = («type.name») obj;
\r
355 «FOR property : genTO.equalsIdentifiers»
\r
356 «val fieldName = property.fieldName»
\r
357 if («fieldName» == null) {
\r
358 if (other.«fieldName» != null) {
\r
361 «IF property.returnType.name.contains("[")»
\r
362 } else if(!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) {
\r
364 } else if(!«fieldName».equals(other.«fieldName»)) {
\r
375 * Template method which generates the method <code>toString()</code>.
\r
377 * @return string with the <code>toString()</code> method definition in JAVA format
\r
379 def protected generateToString() '''
\r
380 «IF !genTO.toStringIdentifiers.empty»
\r
382 public String toString() {
\r
383 StringBuilder builder = new StringBuilder();
\r
384 «val properties = genTO.toStringIdentifiers»
\r
385 builder.append("«type.name» [«properties.get(0).fieldName»=");
\r
386 «IF properties.get(0).returnType.name.contains("[")»
\r
387 builder.append(«Arrays.importedName».toString(«properties.get(0).fieldName»));
\r
389 builder.append(«properties.get(0).fieldName»);
\r
391 «FOR i : 1..<genTO.toStringIdentifiers.size»
\r
392 builder.append(", «properties.get(i).fieldName»=");
\r
393 «IF properties.get(i).returnType.name.contains("[")»
\r
394 builder.append(«Arrays.importedName».toString(«properties.get(i).fieldName»));
\r
396 builder.append(«properties.get(i).fieldName»);
\r
399 builder.append("]");
\r
400 return builder.toString();
\r
405 def private generateGetLength() '''
\r
406 «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
\r
407 public static «List.importedName»<«Range.importedName»<Integer>> getLength() {
\r
408 final «List.importedName»<«Range.importedName»<Integer>> result = new «ArrayList.importedName»<>();
\r
409 «List.importedName»<«Range.importedName»<«Integer.importedName»>> lengthConstraints = new «ArrayList.importedName»<>();
\r
410 «FOR r : restrictions.lengthConstraints»
\r
411 result.add(«Range.importedName».closed(«r.min», «r.max»));
\r