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
14 import java.util.regex.Pattern
15 import com.google.common.io.BaseEncoding
16 import java.beans.ConstructorProperties
19 * Template for generating JAVA class.
\r
21 class ClassTemplate extends BaseTemplate {
\r
23 protected val List<GeneratedProperty> properties
\r
24 protected val List<GeneratedProperty> finalProperties
\r
25 protected val List<GeneratedProperty> parentProperties
\r
26 protected val Iterable<GeneratedProperty> allProperties;
\r
27 protected val Restrictions restrictions
\r
30 * List of enumeration which are generated as JAVA enum type.
\r
32 protected val List<Enumeration> enums
\r
35 * List of constant instances which are generated as JAVA public static final attributes.
\r
37 protected val List<Constant> consts
\r
40 * List of generated types which are enclosed inside <code>genType</code>
\r
42 protected val List<GeneratedType> enclosedGeneratedTypes;
\r
45 protected val GeneratedTransferObject genTO;
\r
48 * Creates instance of this class with concrete <code>genType</code>.
\r
50 * @param genType generated transfer object which will be transformed to JAVA class source code
\r
52 new(GeneratedTransferObject genType) {
\r
54 this.genTO = genType
\r
55 this.properties = genType.properties
\r
56 this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties)
\r
57 this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO)
\r
58 this.restrictions = genType.restrictions
\r
60 var List<GeneratedProperty> sorted = new ArrayList<GeneratedProperty>();
\r
61 sorted.addAll(properties);
\r
62 sorted.addAll(parentProperties);
\r
63 Collections.sort(sorted, new PropertyComparator());
\r
65 this.allProperties = sorted
\r
66 this.enums = genType.enumerations
\r
67 this.consts = genType.constantDefinitions
\r
68 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
82 override protected body() {
\r
83 generateBody(false);
\r
87 * Template method which generates class body.
\r
89 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
90 * @return string with class source code in JAVA format
\r
92 def protected generateBody(boolean isInnerClass) '''
\r
93 «type.comment.asJavadoc»
\r
94 «generateClassDeclaration(isInnerClass)» {
\r
96 «innerClassesDeclarations»
\r
98 «constantsDeclarations»
\r
103 «FOR field : properties SEPARATOR "\n"»
\r
104 «field.getterMethod»
\r
105 «IF !field.readOnly»
\r
106 «field.setterMethod»
\r
114 «generateToString(genTO.toStringIdentifiers)»
\r
116 «generateGetLength»
\r
123 * Template method which generates inner classes inside this interface.
\r
125 * @return string with the source code for inner classes in JAVA format
\r
127 def protected innerClassesDeclarations() '''
\r
128 «IF !enclosedGeneratedTypes.empty»
\r
129 «FOR innerClass : enclosedGeneratedTypes SEPARATOR "\n"»
\r
130 «IF (innerClass instanceof GeneratedTransferObject)»
\r
131 «val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
\r
132 «classTemplate.generateAsInnerClass»
\r
140 def protected constructors() '''
\r
141 «IF genTO.unionType»
\r
142 «genUnionConstructor»
\r
144 «allValuesConstructor»
\r
146 «IF !allProperties.empty»
\r
149 «IF properties.empty && !parentProperties.empty »
\r
150 «parentConstructor»
\r
154 def protected allValuesConstructor() '''
\r
155 «IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
\r
156 @«ConstructorProperties.importedName»("value")
\r
158 public «type.name»(«allProperties.asArgumentsDeclaration») {
\r
159 «IF false == parentProperties.empty»
\r
160 super(«parentProperties.asArguments»);
\r
162 «FOR p : allProperties»
\r
163 «generateRestrictions(type, p.fieldName.toString, p.returnType)»
\r
165 «FOR p : properties»
\r
166 this.«p.fieldName» = «p.fieldName»;
\r
171 def protected genUnionConstructor() '''
\r
172 «FOR p : allProperties»
\r
173 «val List<GeneratedProperty> other = new ArrayList(properties)»
\r
174 «val added = other.remove(p)»
\r
175 «genConstructor(p, other)»
\r
180 def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) '''
\r
181 public «type.name»(«property.returnType.importedName + " " + property.name») {
\r
182 «IF false == parentProperties.empty»
\r
183 super(«parentProperties.asArguments»);
\r
185 «generateRestrictions(type, property.fieldName.toString, property.returnType)»
\r
186 this.«property.fieldName» = «property.name»;
\r
188 this.«p.fieldName» = null;
\r
193 def protected copyConstructor() '''
\r
195 * Creates a copy from Source Object.
\r
197 * @param source Source object
\r
199 public «type.name»(«type.name» source) {
\r
200 «IF false == parentProperties.empty»
\r
203 «FOR p : properties»
\r
204 this.«p.fieldName» = source.«p.fieldName»;
\r
209 def protected parentConstructor() '''
\r
211 * Creates a new instance from «genTO.superType.importedName»
\r
213 * @param source Source object
\r
215 public «type.name»(«genTO.superType.importedName» source) {
\r
220 def protected defaultInstance() '''
\r
221 «IF genTO.typedef && !allProperties.empty && !genTO.unionType»
\r
222 «val prop = allProperties.get(0)»
\r
223 public static «genTO.name» getDefaultInstance(String defaultValue) {
\r
224 «IF "byte[]".equals(prop.returnType.name)»
\r
225 «BaseEncoding.importedName» baseEncoding = «BaseEncoding.importedName».base64();
\r
226 return new «genTO.name»(baseEncoding.decode(defaultValue));
\r
228 return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
\r
235 * Template method which generates JAVA class declaration.
\r
237 * @param isInnerClass boolean value which specify if generated class is|isn't inner
\r
238 * @return string with class declaration in JAVA format
\r
240 def protected generateClassDeclaration(boolean isInnerClass) '''
\r
242 IF (isInnerClass)»«
\r
244 ELSEIF (type.abstract)»«
\r
248 ENDIF»class «type.name»«
\r
249 IF (genTO.superType != null)»«
\r
250 " extends "»«genTO.superType.importedName»«
\r
252 «IF (!type.implements.empty)»«
\r
254 FOR type : type.implements SEPARATOR ", "»«
\r
255 type.importedName»«
\r
261 * Template method which generates JAVA enum type.
\r
263 * @return string with inner enum source code in JAVA format
\r
265 def protected enumDeclarations() '''
\r
267 «FOR e : enums SEPARATOR "\n"»
\r
268 «val enumTemplate = new EnumTemplate(e)»
\r
269 «enumTemplate.generateAsInnerClass»
\r
274 def protected suidDeclaration() '''
\r
275 «IF genTO.SUID != null»
\r
276 private static final long serialVersionUID = «genTO.SUID.value»L;
\r
281 * Template method wich generates JAVA constants.
\r
283 * @return string with constants in JAVA format
\r
285 def protected constantsDeclarations() '''
\r
288 «IF c.name == TypeConstants.PATTERN_CONSTANT_NAME»
\r
289 «val cValue = c.value»
\r
290 «IF cValue instanceof List<?>»
\r
291 «val cValues = cValue as List<?>»
\r
292 private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST» = new «ArrayList.importedName»<«Pattern.importedName»>();
\r
293 public static final «List.importedName»<String> «TypeConstants.PATTERN_CONSTANT_NAME» = «Arrays.importedName».asList(«
\r
294 FOR v : cValues SEPARATOR ", "»«
\r
295 IF v instanceof String»"«
\r
300 «generateStaticInicializationBlock»
\r
303 public static final «c.type.importedName» «c.name» = «c.value»;
\r
310 * Template method which generates JAVA static initialization block.
\r
312 * @return string with static initialization block in JAVA format
\r
314 def protected generateStaticInicializationBlock() '''
\r
316 for (String regEx : «TypeConstants.PATTERN_CONSTANT_NAME») {
\r
317 «Constants.MEMBER_PATTERN_LIST».add(Pattern.compile(regEx));
\r
323 * Template method which generates JAVA class attributes.
\r
325 * @return string with the class attributes in JAVA format
\r
327 def protected generateFields() '''
\r
328 «IF !properties.empty»
\r
329 «FOR f : properties»
\r
330 «IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
\r
337 * Template method which generates the method <code>hashCode()</code>.
\r
339 * @return string with the <code>hashCode()</code> method definition in JAVA format
\r
341 def protected generateHashCode() '''
\r
342 «IF !genTO.hashCodeIdentifiers.empty»
\r
344 public int hashCode() {
\r
345 final int prime = 31;
\r
347 «FOR property : genTO.hashCodeIdentifiers»
\r
348 «IF property.returnType.name.contains("[")»
\r
349 result = prime * result + ((«property.fieldName» == null) ? 0 : «Arrays.importedName».hashCode(«property.fieldName»));
\r
351 result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode());
\r
360 * Template method which generates the method <code>equals()</code>.
\r
362 * @return string with the <code>equals()</code> method definition in JAVA format
\r
364 def protected generateEquals() '''
\r
365 «IF !genTO.equalsIdentifiers.empty»
\r
367 public boolean equals(java.lang.Object obj) {
\r
374 if (getClass() != obj.getClass()) {
\r
377 «type.name» other = («type.name») obj;
\r
378 «FOR property : genTO.equalsIdentifiers»
\r
379 «val fieldName = property.fieldName»
\r
380 if («fieldName» == null) {
\r
381 if (other.«fieldName» != null) {
\r
384 «IF property.returnType.name.contains("[")»
\r
385 } else if(!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) {
\r
387 } else if(!«fieldName».equals(other.«fieldName»)) {
\r
397 def private generateGetLength() '''
\r
398 «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
\r
399 public static «List.importedName»<«Range.importedName»<Integer>> getLength() {
\r
400 final «List.importedName»<«Range.importedName»<Integer>> result = new «ArrayList.importedName»<>();
\r
401 «List.importedName»<«Range.importedName»<«Integer.importedName»>> lengthConstraints = new «ArrayList.importedName»<>();
\r
402 «FOR r : restrictions.lengthConstraints»
\r
403 result.add(«Range.importedName».closed(«r.min», «r.max»));
\r