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 org.opendaylight.mdsal.binding.model.api.GeneratedProperty
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
18 import org.opendaylight.mdsal.binding.model.api.Enumeration
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
22 * Template for generating JAVA class.
24 class UnionTemplate extends ClassTemplate {
27 * Creates instance of this class with concrete <code>genType</code>.
29 * @param genType generated transfer object which will be transformed to JAVA class source code
31 new(GeneratedTransferObject genType) {
33 if (isBaseEncodingImportRequired) {
34 addImport(BaseEncoding)
38 final private def boolean isBaseEncodingImportRequired() {
39 for (property : finalProperties) {
40 val propRet = property.returnType
41 if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
42 (propRet as GeneratedTransferObject).properties !== null &&
43 !(propRet as GeneratedTransferObject).properties.empty &&
44 ((propRet as GeneratedTransferObject).properties.size == 1) &&
45 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
46 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
52 override constructors() '''
53 «unionConstructorsParentProperties»
55 «IF !allProperties.empty»
58 «IF properties.empty && !parentProperties.empty»
63 private def unionConstructors() '''
64 «FOR property : finalProperties SEPARATOR "\n"»
65 «val propRet = property.returnType»
66 «IF "char[]".equals(propRet.name)»
68 * Constructor provided only for using in JMX. Don't use it for
69 * construction new object of this union type.
71 @«ConstructorProperties.importedName»("«property.name»")
72 public «type.name»(«propRet.importedName» «property.fieldName») {
73 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
74 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
75 «FOR other : finalProperties»
76 «IF other.name.equals("value")»
77 «IF other.returnType.importedName.contains("[]")»
78 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
80 this.«other.fieldName» = «other.fieldName»;
83 this.«other.fieldName» = defInst.«other.fieldName»;
88 «val propertyAndTopParentProperties = parentProperties + #[property]»
89 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
90 super(«parentProperties.asArguments»);
91 this.«property.fieldName» = «property.fieldName»;
92 «FOR other : finalProperties»
93 «IF property != other && !"value".equals(other.name)»
94 this.«other.fieldName» = null;
103 val outerCls = getOuterClassName(type);
104 if(outerCls !== null) {
105 return outerCls + type.name + "Builder"
107 return type.name + "Builder"
110 private def unionConstructorsParentProperties() '''
111 «FOR property : parentProperties SEPARATOR "\n"»
112 public «type.name»(«property.returnType.importedName» «property.fieldName») {
113 super(«property.fieldName»);
118 override protected getterMethod(GeneratedProperty field) {
119 if (!"value".equals(field.name)) {
120 return super.getterMethod(field)
123 Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
126 public char[] «field.getterMethodName»() {
127 if («field.fieldName» == null) {
128 «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
129 if («property.fieldName» != null) {
130 «val propRet = property.returnType»
131 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
133 «field.fieldName» = «property.fieldName».toCharArray();
134 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
135 ««« type instance-identifier
136 «field.fieldName» = «property.fieldName».toString().toCharArray();
137 «ELSEIF "byte[]".equals(propRet.name)»
139 «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
140 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
141 || propRet instanceof Enumeration
142 || propRet.fullyQualifiedName.startsWith("java.math")»
143 ««« type int*, uint, decimal64 or enumeration*
144 «field.fieldName» = «property.fieldName».toString().toCharArray();
145 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
147 «field.fieldName» = «property.fieldName».getValue();
148 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
149 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
150 && (propRet as GeneratedTransferObject).properties !== null
151 && !(propRet as GeneratedTransferObject).properties.empty
152 && ((propRet as GeneratedTransferObject).properties.size == 1)
153 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
154 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
155 ««« generated boolean typedef
156 «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
157 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
158 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
159 && (propRet as GeneratedTransferObject).properties !== null
160 && !(propRet as GeneratedTransferObject).properties.empty
161 && ((propRet as GeneratedTransferObject).properties.size == 1)
162 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
163 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
164 ««« generated byte[] typedef
165 «field.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
166 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
167 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
168 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
169 ««« generated bits typedef
170 «field.fieldName» = java.util.Arrays.toString(«property.fieldName».getValue()).toCharArray();
173 «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
178 return «field.fieldName» == null ? null : «field.fieldName».clone();
183 override def isReadOnly(GeneratedProperty field) {
184 return !"value".equals(field.name) && super.isReadOnly(field)
187 override protected copyConstructor() '''
189 * Creates a copy from Source Object.
191 * @param source Source object
193 public «type.name»(«type.name» source) {
194 «IF !parentProperties.empty»
197 «IF !properties.empty»
199 «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
200 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
202 this.«p.fieldName» = source.«p.fieldName»;