*/
package org.opendaylight.yangtools.sal.java.api.generator
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
+import static org.opendaylight.yangtools.binding.generator.util.Types.*
+import com.google.common.base.Preconditions;
import java.beans.ConstructorProperties
+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.Enumeration
-import static org.opendaylight.yangtools.binding.generator.util.Types.*
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
/**
* Template for generating JAVA class.
*/
new(GeneratedTransferObject genType) {
super(genType)
+ if(isBaseEncodingImportRequired) {
+ this.importMap.put("BaseEncoding","com.google.common.io")
+ }
+ }
+
+ final private def boolean isBaseEncodingImportRequired() {
+ for (property : finalProperties) {
+ val propRet = property.returnType
+ if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
+ (propRet as GeneratedTransferObject).properties != null &&
+ !(propRet as GeneratedTransferObject).properties.empty &&
+ ((propRet as GeneratedTransferObject).properties.size == 1) &&
+ (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
+ "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
+ return true;
+ }
+ }
}
override constructors() '''
private def unionConstructors() '''
«FOR property : finalProperties SEPARATOR "\n"»
«val propRet = property.returnType»
- «val isCharArray = "char[]".equals(propRet.name)»
- «IF isCharArray»
+ «IF "char[]".equals(propRet.name)»
/**
* Constructor provided only for using in JMX. Don't use it for
* construction new object of this union type.
super(«parentProperties.asArguments»);
this.«property.fieldName» = «property.fieldName»;
«FOR other : finalProperties»
- «IF property != other»
- «IF "value".equals(other.name)»
- «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
- ««« type string
- this.«other.fieldName» = «property.fieldName».toCharArray();
- «ELSEIF "byte[]".equals(propRet.name)»
- ««« type binary
- this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
- «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
- || propRet instanceof Enumeration
- || propRet.fullyQualifiedName.startsWith("java.math")»
- ««« type int*, uint, decimal64 or enumeration*
- this.«other.fieldName» = «property.fieldName».toString().toCharArray();
- «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
- ««« union type
- this.«other.fieldName» = «property.fieldName».getValue();
- «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
- && (propRet as GeneratedTransferObject).typedef // Is it a typedef
- && (propRet as GeneratedTransferObject).properties != null
- && !(propRet as GeneratedTransferObject).properties.empty
- && ((propRet as GeneratedTransferObject).properties.size == 1)
- && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
- && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
- ««« generated boolean typedef
- this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
- «ELSE»
- ««« generated type
- this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
- «ENDIF»
- «ELSE»
- this.«other.fieldName» = null;
- «ENDIF»
+ «IF property != other && !"value".equals(other.name)»
+ this.«other.fieldName» = null;
«ENDIF»
«ENDFOR»
}
«ENDIF»
«ENDFOR»
'''
-
+
def typeBuilder() {
val outerCls = getOuterClassName(type);
if(outerCls !== null) {
«ENDFOR»
'''
+ override protected getterMethod(GeneratedProperty field) {
+ if (!"value".equals(field.name)) {
+ return super.getterMethod(field)
+ }
+
+ Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
+
+ '''
+ public char[] «field.getterMethodName»() {
+ if («field.fieldName» == null) {
+ «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
+ if («property.fieldName» != null) {
+ «val propRet = property.returnType»
+ «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
+ ««« type string
+ «field.fieldName» = «property.fieldName».toCharArray();
+ «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
+ ««« type instance-identifier
+ «field.fieldName» = «property.fieldName».toString().toCharArray();
+ «ELSEIF "byte[]".equals(propRet.name)»
+ ««« type binary
+ «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
+ «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
+ || propRet instanceof Enumeration
+ || propRet.fullyQualifiedName.startsWith("java.math")»
+ ««« type int*, uint, decimal64 or enumeration*
+ «field.fieldName» = «property.fieldName».toString().toCharArray();
+ «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
+ ««« union type
+ «field.fieldName» = «property.fieldName».getValue();
+ «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
+ && (propRet as GeneratedTransferObject).typedef // Is it a typedef
+ && (propRet as GeneratedTransferObject).properties != null
+ && !(propRet as GeneratedTransferObject).properties.empty
+ && ((propRet as GeneratedTransferObject).properties.size == 1)
+ && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
+ && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
+ ««« generated boolean typedef
+ «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
+ «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
+ && (propRet as GeneratedTransferObject).typedef // Is it a typedef
+ && (propRet as GeneratedTransferObject).properties != null
+ && !(propRet as GeneratedTransferObject).properties.empty
+ && ((propRet as GeneratedTransferObject).properties.size == 1)
+ && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
+ && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
+ ««« generated byte[] typedef
+ «field.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
+ «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
+ && (propRet as GeneratedTransferObject).typedef // Is it a typedef
+ && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
+ ««« generated bits typedef
+ «field.fieldName» = java.util.Arrays.toString(«property.fieldName».getValue()).toCharArray();
+ «ELSE»
+ ««« generated type
+ «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
+ «ENDIF»
+ }
+ «ENDFOR»
+ }
+ return «field.fieldName» == null ? null : «field.fieldName».clone();
+ }
+ '''
+ }
+
+ override def isReadOnly(GeneratedProperty field) {
+ return !"value".equals(field.name) && super.isReadOnly(field)
+ }
+
override protected copyConstructor() '''
/**
* Creates a copy from Source Object.
«ENDIF»
«IF !properties.empty»
«FOR p : properties»
- «IF p.returnType.importedName.contains("[]")»
+ «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
«ELSE»
this.«p.fieldName» = source.«p.fieldName»;