1 package org.opendaylight.yangtools.sal.java.api.generator
3 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
4 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
6 import org.opendaylight.yangtools.sal.binding.model.api.Type
7 import org.opendaylight.yangtools.binding.generator.util.Types
8 import com.google.common.base.Splitter
9 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature
10 import com.google.common.collect.Range
11 import java.util.ArrayList
13 import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType
14 import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
15 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
17 abstract class BaseTemplate {
20 protected val GeneratedType type;
21 protected val Map<String,String> importMap;
22 static val paragraphSplitter = Splitter.on("\n\n").omitEmptyStrings();
23 new(GeneratedType _type) {
25 throw new IllegalArgumentException("Generated type reference cannot be NULL!")
28 this.importMap = GeneratorUtil.createImports(type)
31 def packageDefinition () '''package «type.packageName»;'''
34 protected def getFullyQualifiedName() {
35 return type.fullyQualifiedName
38 final public def generate() {
47 protected def imports() '''
49 «FOR entry : importMap.entrySet»
50 «IF entry.value != fullyQualifiedName»
51 import «entry.value».«entry.key»;
58 protected abstract def CharSequence body();
62 final protected def fieldName(GeneratedProperty property) '''_«property.name»'''
65 final protected def propertyNameFromGetter(MethodSignature getter) {
67 if (getter.name.startsWith("is")) {
69 } else if (getter.name.startsWith("get")) {
72 throw new IllegalArgumentException("Not a getter")
74 return getter.name.substring(prefix).toFirstLower;
78 * Template method which generates the getter method for <code>field</code>
81 * generated property with data about field which is generated as the getter method
82 * @return string with the getter method source code in JAVA format
84 final protected def getterMethod(GeneratedProperty field) {
86 public «field.returnType.importedName» «field.getterMethodName»() {
87 return «field.fieldName»;
92 final protected def getterMethodName(GeneratedProperty field) {
93 val prefix = if(field.returnType.equals(Types.BOOLEAN)) "is" else "get"
94 return '''«prefix»«field.name.toFirstUpper»'''
98 * Template method which generates the setter method for <code>field</code>
101 * generated property with data about field which is generated as the setter method
102 * @return string with the setter method source code in JAVA format
104 final protected def setterMethod(GeneratedProperty field) '''
105 «val returnType = field.returnType.importedName»
106 public «type.name» set«field.name.toFirstUpper»(«returnType» value) {
107 this.«field.fieldName» = value;
112 final protected def importedName(Type intype) {
113 GeneratorUtil.putTypeIntoImports(type, intype, importMap);
114 GeneratorUtil.getExplicitType(type, intype, importMap)
117 final protected def importedName(Class<?> cls) {
118 importedName(Types.typeForClass(cls))
122 * Template method which generates method parameters with their types from <code>parameters</code>.
125 * group of generated property instances which are transformed to the method parameters
126 * @return string with the list of the method parameters with their types in JAVA format
128 def final protected asArgumentsDeclaration(Iterable<GeneratedProperty> parameters)
129 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.returnType.importedName» «parameter.fieldName»«ENDFOR»«ENDIF»'''
132 * Template method which generates sequence of the names of the class attributes from <code>parameters</code>.
135 * group of generated property instances which are transformed to the sequence of parameter names
136 * @return string with the list of the parameter names of the <code>parameters</code>
138 def final protected asArguments(Iterable<GeneratedProperty> parameters)
139 '''«IF !parameters.empty»«FOR parameter : parameters SEPARATOR ", "»«parameter.fieldName»«ENDFOR»«ENDIF»'''
143 * Template method which generates JAVA comments.
145 * @param comment string with the comment for whole JAVA class
146 * @return string with comment in JAVA format
148 def protected CharSequence asJavadoc(String comment) {
149 if (comment==null) return '';
150 val paragraphs = paragraphSplitter.split(comment)
154 «FOR p : paragraphs SEPARATOR "<p>"»
161 def generateLengthRestrictions(Type type, String paramName) '''
162 «IF type instanceof ConcreteType»
163 «val restrictions = (type as ConcreteType).restrictions»
164 «IF restrictions !== null && !restrictions.lengthConstraints.empty»
165 «generateLengthRestriction(type, restrictions, paramName)»
168 «IF type instanceof GeneratedTransferObject»
169 «val restrictions = (type as GeneratedTransferObject).restrictions»
170 «IF restrictions !== null && !restrictions.lengthConstraints.empty»
171 «generateLengthRestriction(type, restrictions, paramName)»
176 def generateLengthRestriction(Type type, Restrictions restrictions, String paramName) '''
177 boolean isValidLength = false;
178 «List.importedName»<«Range.importedName»<«Integer.importedName»>> lengthConstraints = new «ArrayList.importedName»<>();
179 «FOR r : restrictions.lengthConstraints»
180 lengthConstraints.add(«Range.importedName».closed(«r.min», «r.max»));
182 for («Range.importedName»<«Integer.importedName»> r : lengthConstraints) {
183 «IF type.name.contains("[")»
184 if (r.contains(«paramName».length)) {
186 if (r.contains(«paramName».length())) {
188 isValidLength = true;
191 if (!isValidLength) {
192 throw new IllegalArgumentException("illegal length");