*/
package org.opendaylight.yangtools.sal.java.api.generator
+import com.google.common.collect.ImmutableList
+import com.google.common.collect.Lists
+import com.google.common.collect.Range
+import com.google.common.io.BaseEncoding
+import java.beans.ConstructorProperties
+import java.math.BigDecimal
+import java.math.BigInteger
+import java.util.ArrayList
+import java.util.Arrays
+import java.util.Collections
import java.util.List
+import java.util.regex.Pattern
import org.opendaylight.yangtools.binding.generator.util.TypeConstants
import org.opendaylight.yangtools.sal.binding.model.api.Constant
import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
-import java.util.ArrayList
-import java.util.Collections\rimport java.util.Arrays
import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
-import com.google.common.collect.Range
-import java.util.regex.Pattern
-import com.google.common.io.BaseEncoding
-import java.beans.ConstructorProperties
-import com.google.common.collect.Lists
/**
- * Template for generating JAVA class.
+ * Template for generating JAVA class.
*/
class ClassTemplate extends BaseTemplate {
protected val List<GeneratedProperty> parentProperties
protected val Iterable<GeneratedProperty> allProperties;
protected val Restrictions restrictions
-
+
/**
* List of enumeration which are generated as JAVA enum type.
*/
protected val List<Enumeration> enums
-
+
/**
* List of constant instances which are generated as JAVA public static final attributes.
*/
protected val List<Constant> consts
-
+
/**
* List of generated types which are enclosed inside <code>genType</code>
*/
protected val List<GeneratedType> enclosedGeneratedTypes;
-
protected val GeneratedTransferObject genTO;
/**
this.enclosedGeneratedTypes = genType.enclosedTypes
}
-
/**
* Generates JAVA class source code (class body only).
*
return generateBody(true)
}
-
override protected body() {
generateBody(false);
}
* @return string with class source code in JAVA format
*/
def protected generateBody(boolean isInnerClass) '''
- «type.comment.asJavadoc»
+ «wrapToDocumentation(formatDataForJavaDoc(type))»
«generateClassDeclaration(isInnerClass)» {
«suidDeclaration»
«innerClassesDeclarations»
«enumDeclarations»
«constantsDeclarations»
«generateFields»
-
- «constructors»
+ «IF restrictions != null && (!restrictions.rangeConstraints.nullOrEmpty ||
+ !restrictions.lengthConstraints.nullOrEmpty)»
+ «generateConstraints»
+
+ «ENDIF»
+ «constructors»
+
«defaultInstance»
«FOR field : properties SEPARATOR "\n"»
«generateToString(genTO.toStringIdentifiers)»
- «generateGetLength»
+ «generateLengthMethod("length", "_length")»
- «generateGetRange»
+ «generateRangeMethod("range", "_range")»
}
'''
+ def private generateLengthMethod(String methodName, String varName) '''
+ «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
+ «val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
+ public static «List.importedName»<«Range.importedName»<«numberClass.importedNumber»>> «methodName»() {
+ return «varName»;
+ }
+ «ENDIF»
+ '''
+
+ def private generateRangeMethod(String methodName, String varName) '''
+ «IF restrictions != null && !(restrictions.rangeConstraints.empty)»
+ «val returnType = allProperties.iterator.next.returnType»
+ public static «List.importedName»<«Range.importedName»<«returnType.importedNumber»>> «methodName»() {
+ return «varName»;
+ }
+ «ENDIF»
+ '''
/**
* Template method which generates inner classes inside this interface.
«IF (innerClass instanceof GeneratedTransferObject)»
«val classTemplate = new ClassTemplate(innerClass as GeneratedTransferObject)»
«classTemplate.generateAsInnerClass»
-
+
«ENDIF»
«ENDFOR»
«ENDIF»
'''
-
-
+
def protected constructors() '''
«IF genTO.unionType»
«genUnionConstructor»
«parentConstructor»
«ENDIF»
'''
-
+
+ def private generateConstraints() '''
+ static {
+ «IF !restrictions.rangeConstraints.nullOrEmpty»
+ «generateRangeConstraints»
+ «ENDIF»
+ «IF !restrictions.lengthConstraints.nullOrEmpty»
+ «generateLengthConstraints»
+ «ENDIF»
+ }
+ '''
+
+ private def generateRangeConstraints() '''
+ «IF !allProperties.nullOrEmpty»
+ «val returnType = allProperties.iterator.next.returnType»
+ «IF returnType.fullyQualifiedName.equals(BigDecimal.canonicalName)»
+ «rangeBody(restrictions, BigDecimal, genTO.importedName, "_range")»
+ «ELSE»
+ «rangeBody(restrictions, BigInteger, genTO.importedName, "_range")»
+ «ENDIF»
+ «ENDIF»
+ '''
+
+ private def rangeBody(Restrictions restrictions, Class<? extends Number> numberClass, String className, String varName) '''
+ «ImmutableList.importedName».Builder<«Range.importedName»<«numberClass.importedName»>> builder = «ImmutableList.importedName».builder();
+ «FOR r : restrictions.rangeConstraints»
+ builder.add(«Range.importedName».closed(«numericValue(numberClass, r.min)», «numericValue(numberClass, r.max)»));
+ «ENDFOR»
+ «varName» = builder.build();
+ '''
+
+ private def lengthBody(Restrictions restrictions, Class<? extends Number> numberClass, String className, String varName) '''
+ «ImmutableList.importedName».Builder<«Range.importedName»<«numberClass.importedName»>> builder = «ImmutableList.importedName».builder();
+ «FOR r : restrictions.lengthConstraints»
+ builder.add(«Range.importedName».closed(«numericValue(numberClass, r.min)», «numericValue(numberClass, r.max)»));
+ «ENDFOR»
+ «varName» = builder.build();
+ '''
+
+ private def generateLengthConstraints() '''
+ «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
+ «val numberClass = restrictions.lengthConstraints.iterator.next.min.class»
+ «IF numberClass.equals(typeof(BigDecimal))»
+ «lengthBody(restrictions, numberClass, genTO.importedName, "_length")»
+ «ELSE»
+ «lengthBody(restrictions, typeof(BigInteger), genTO.importedName, "_length")»
+ «ENDIF»
+ «ENDIF»
+ '''
+
def protected allValuesConstructor() '''
«IF genTO.typedef && !allProperties.empty && allProperties.size == 1 && allProperties.get(0).name.equals("value")»
@«ConstructorProperties.importedName»("value")
this.«p.fieldName» = «p.fieldName»;
«ENDFOR»
}
+
'''
def protected genUnionConstructor() '''
return new «genTO.name»(defaultValue);
«ELSEIF allProperties.size > 1»
«bitsArgs»
+ «ELSEIF "java.lang.Boolean".equals(prop.returnType.fullyQualifiedName)»
+ return new «genTO.name»(Boolean.valueOf(defaultValue));
+ «ELSEIF "java.lang.Byte".equals(prop.returnType.fullyQualifiedName)»
+ return new «genTO.name»(Byte.valueOf(defaultValue));
+ «ELSEIF "java.lang.Short".equals(prop.returnType.fullyQualifiedName)»
+ return new «genTO.name»(Short.valueOf(defaultValue));
+ «ELSEIF "java.lang.Integer".equals(prop.returnType.fullyQualifiedName)»
+ return new «genTO.name»(Integer.valueOf(defaultValue));
+ «ELSEIF "java.lang.Long".equals(prop.returnType.fullyQualifiedName)»
+ return new «genTO.name»(Long.valueOf(defaultValue));
«ELSE»
return new «genTO.name»(new «prop.returnType.importedName»(defaultValue));
«ENDIF»
int i = 0;
return new «genTO.name»(
«FOR prop : allProperties SEPARATOR ","»
- properties.get(i++).equals(defaultValue) ? new «Boolean.importedName»("true") : null
+ properties.get(i++).equals(defaultValue) ? «Boolean.importedName».TRUE : null
«ENDFOR»
);
'''
ENDFOR»«
ENDIF
»'''
-
+
/**
* Template method which generates JAVA enum type.
*
* @return string with the class attributes in JAVA format
*/
def protected generateFields() '''
+ «IF restrictions != null»
+ «val prop = getPropByName("value")»
+ «IF prop != null»
+ «IF !(restrictions.lengthConstraints.empty)»
+ private static final «List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _length;
+ «ENDIF»
+ «IF !(restrictions.rangeConstraints.empty)»
+ private static final «List.importedName»<«Range.importedName»<«prop.returnType.importedNumber»>> _range;
+ «ENDIF»
+ «ENDIF»
+ «ENDIF»
«IF !properties.empty»
«FOR f : properties»
«IF f.readOnly»final«ENDIF» private «f.returnType.importedName» «f.fieldName»;
«ENDIF»
'''
-
/**
* Template method which generates the method <code>hashCode()</code>.
*
«ENDIF»
'''
- def private generateGetLength() '''
- «IF restrictions != null && !(restrictions.lengthConstraints.empty)»
- «val clazz = restrictions.lengthConstraints.iterator.next.min.class»
- public static «List.importedName»<«Range.importedName»<«clazz.importedName»>> length() {
- final «List.importedName»<«Range.importedName»<«clazz.importedName»>> result = new «ArrayList.importedName»<>();
- «FOR r : restrictions.lengthConstraints»
- result.add(«Range.importedName».closed(new «clazz.importedName»("«r.min»"), new «clazz.importedName»("«r.max»")));
- «ENDFOR»
- return result;
+ def GeneratedProperty getPropByName(String name) {
+ for (GeneratedProperty prop : allProperties) {
+ if (prop.name.equals(name)) {
+ return prop;
}
- «ENDIF»
- '''
-
- def private generateGetRange() '''
- «IF restrictions != null && !(restrictions.rangeConstraints.empty)»
- «val clazz = restrictions.rangeConstraints.iterator.next.min.class»
- public static «List.importedName»<«Range.importedName»<«clazz.importedName»>> range() {
- final «List.importedName»<«Range.importedName»<«clazz.importedName»>> result = new «ArrayList.importedName»<>();
- «FOR r : restrictions.rangeConstraints»
- result.add(«Range.importedName».closed(new «clazz.importedName»("«r.min»"), new «clazz.importedName»("«r.max»")));
- «ENDFOR»
- return result;
- }
- «ENDIF»
- '''
+ }
+ return null;
+ }
}