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»
58 private def unionConstructors() '''
59 «FOR property : finalProperties SEPARATOR "\n"»
60 «val propRet = property.returnType»
61 «IF "char[]".equals(propRet.name)»
63 * Constructor provided only for using in JMX. Don't use it for
64 * construction new object of this union type.
66 @«ConstructorProperties.importedName»("«property.name»")
67 public «type.name»(«propRet.importedName» «property.fieldName») {
68 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
69 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
70 «FOR other : finalProperties»
71 «IF other.name.equals("value")»
72 «IF other.returnType.importedName.contains("[]")»
73 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
75 this.«other.fieldName» = «other.fieldName»;
78 this.«other.fieldName» = defInst.«other.fieldName»;
83 «val actualType = property.returnType»
84 «val restrictions = restrictionsForSetter(actualType)»
85 «IF restrictions !== null»
86 «generateCheckers(property, restrictions, actualType)»
89 «val propertyAndTopParentProperties = parentProperties + #[property]»
90 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
91 super(«parentProperties.asArguments»);
92 «IF restrictions !== null»
93 «checkArgument(property, restrictions, actualType, property.fieldName.toString)»
95 this.«property.fieldName» = «property.fieldName»;
96 «FOR other : finalProperties»
97 «IF property != other && !"value".equals(other.name)»
98 this.«other.fieldName» = null;
107 val outerCls = getOuterClassName(type);
108 if(outerCls !== null) {
109 return outerCls + type.name + "Builder"
111 return type.name + "Builder"
114 private def unionConstructorsParentProperties() '''
115 «FOR property : parentProperties SEPARATOR "\n"»
116 public «type.name»(«property.returnType.importedName» «property.fieldName») {
117 super(«property.fieldName»);
122 def generateStringValue()
124 public «String.importedName» stringValue() {
125 «FOR property : finalProperties.filter([ p | !"value".equals(p.name)])»
126 «val field = property.fieldName»
127 if («field» != null) {
128 «val propRet = property.returnType»
129 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
132 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
133 ««« type instance-identifier
134 return «field».toString();
135 «ELSEIF "byte[]".equals(propRet.name)»
137 return new «String.importedName»(«field»);
138 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
139 || propRet.fullyQualifiedName.startsWith("java.math")»
140 ««« type int*, uint, decimal64 or enumeration*
141 return «field».toString();
142 «ELSEIF propRet instanceof GeneratedTransferObject
143 && (propRet as GeneratedTransferObject).unionType»
145 return «field».stringValue();
146 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
147 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
148 && (propRet as GeneratedTransferObject).properties !== null
149 && !(propRet as GeneratedTransferObject).properties.empty
150 && ((propRet as GeneratedTransferObject).properties.size == 1)
151 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
152 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType) // And the property value is of type boolean»
153 ««« generated boolean typedef
154 return «field».isValue().toString();
155 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
156 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
157 && (propRet as GeneratedTransferObject).properties !== null
158 && !(propRet as GeneratedTransferObject).properties.empty
159 && ((propRet as GeneratedTransferObject).properties.size == 1)
160 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
161 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
162 ««« generated byte[] typedef
163 return «BaseEncoding.importedName».base64().encode(«field».getValue());
164 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
165 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
166 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
167 ««« generated bits typedef
168 return «Arrays.importedName».toString(«field».getValue());
171 return «field».getValue().toString();
176 throw new IllegalStateException("No value assinged");
180 override protected getterMethod(GeneratedProperty field) {
181 if (!"value".equals(field.name)) {
182 return super.getterMethod(field)
185 Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
188 @«Deprecated.importedName»
189 public char[] «field.getterMethodName»() {
190 if («field.fieldName» == null) {
191 «field.fieldName» = stringValue().toCharArray();
193 return «field.fieldName».clone();
198 override isReadOnly(GeneratedProperty field) {
199 return !"value".equals(field.name) && super.isReadOnly(field)
202 override protected copyConstructor() '''
204 * Creates a copy from Source Object.
206 * @param source Source object
208 public «type.name»(«type.name» source) {
209 «IF !parentProperties.empty»
212 «IF !properties.empty»
214 «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
215 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
217 this.«p.fieldName» = source.«p.fieldName»;