2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.java.api.generator
10 import static org.opendaylight.mdsal.binding.model.util.Types.BOOLEAN;
11 import static org.opendaylight.mdsal.binding.model.util.Types.getOuterClassName;
13 import com.google.common.base.Preconditions;
14 import com.google.common.io.BaseEncoding
15 import java.beans.ConstructorProperties
16 import java.util.Arrays
17 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
19 import org.opendaylight.mdsal.binding.model.api.Enumeration
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
23 * Template for generating JAVA class.
25 class UnionTemplate extends ClassTemplate {
28 * Creates instance of this class with concrete <code>genType</code>.
30 * @param genType generated transfer object which will be transformed to JAVA class source code
32 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
33 super(javaType, genType)
37 * Creates instance of this class with concrete <code>genType</code>.
39 * @param genType generated transfer object which will be transformed to JAVA class source code
41 new(GeneratedTransferObject genType) {
45 override constructors() '''
46 «unionConstructorsParentProperties»
48 «IF !allProperties.empty»
51 «IF properties.empty && !parentProperties.empty»
56 private def unionConstructors() '''
57 «FOR property : finalProperties SEPARATOR "\n"»
58 «val propRet = property.returnType»
59 «IF "char[]".equals(propRet.name)»
61 * Constructor provided only for using in JMX. Don't use it for
62 * construction new object of this union type.
64 @«ConstructorProperties.importedName»("«property.name»")
65 public «type.name»(«propRet.importedName» «property.fieldName») {
66 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
67 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
68 «FOR other : finalProperties»
69 «IF other.name.equals("value")»
70 «IF other.returnType.importedName.contains("[]")»
71 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
73 this.«other.fieldName» = «other.fieldName»;
76 this.«other.fieldName» = defInst.«other.fieldName»;
81 «val propertyAndTopParentProperties = parentProperties + #[property]»
82 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
83 super(«parentProperties.asArguments»);
84 this.«property.fieldName» = «property.fieldName»;
85 «FOR other : finalProperties»
86 «IF property != other && !"value".equals(other.name)»
87 this.«other.fieldName» = null;
96 val outerCls = getOuterClassName(type);
97 if(outerCls !== null) {
98 return outerCls + type.name + "Builder"
100 return type.name + "Builder"
103 private def unionConstructorsParentProperties() '''
104 «FOR property : parentProperties SEPARATOR "\n"»
105 public «type.name»(«property.returnType.importedName» «property.fieldName») {
106 super(«property.fieldName»);
111 override protected getterMethod(GeneratedProperty field) {
112 if (!"value".equals(field.name)) {
113 return super.getterMethod(field)
116 Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
119 public char[] «field.getterMethodName»() {
120 if («field.fieldName» == null) {
121 «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
122 if («property.fieldName» != null) {
123 «val propRet = property.returnType»
124 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
126 «field.fieldName» = «property.fieldName».toCharArray();
127 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
128 ««« type instance-identifier
129 «field.fieldName» = «property.fieldName».toString().toCharArray();
130 «ELSEIF "byte[]".equals(propRet.name)»
132 «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
133 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
134 || propRet instanceof Enumeration
135 || propRet.fullyQualifiedName.startsWith("java.math")»
136 ««« type int*, uint, decimal64 or enumeration*
137 «field.fieldName» = «property.fieldName».toString().toCharArray();
138 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
140 «field.fieldName» = «property.fieldName».getValue();
141 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
142 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
143 && (propRet as GeneratedTransferObject).properties !== null
144 && !(propRet as GeneratedTransferObject).properties.empty
145 && ((propRet as GeneratedTransferObject).properties.size == 1)
146 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
147 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
148 ««« generated boolean typedef
149 «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
150 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
151 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
152 && (propRet as GeneratedTransferObject).properties !== null
153 && !(propRet as GeneratedTransferObject).properties.empty
154 && ((propRet as GeneratedTransferObject).properties.size == 1)
155 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
156 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
157 ««« generated byte[] typedef
158 «field.fieldName» = «BaseEncoding.importedName».base64().encode(«property.fieldName».getValue()).toCharArray();
159 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
160 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
161 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
162 ««« generated bits typedef
163 «field.fieldName» = «Arrays.importedName».toString(«property.fieldName».getValue()).toCharArray();
166 «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
171 return «field.fieldName» == null ? null : «field.fieldName».clone();
176 override def isReadOnly(GeneratedProperty field) {
177 return !"value".equals(field.name) && super.isReadOnly(field)
180 override protected copyConstructor() '''
182 * Creates a copy from Source Object.
184 * @param source Source object
186 public «type.name»(«type.name» source) {
187 «IF !parentProperties.empty»
190 «IF !properties.empty»
192 «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
193 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
195 this.«p.fieldName» = source.«p.fieldName»;