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(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
32 super(javaType, genType)
33 if (isBaseEncodingImportRequired) {
34 addImport(BaseEncoding)
39 * Creates instance of this class with concrete <code>genType</code>.
41 * @param genType generated transfer object which will be transformed to JAVA class source code
43 new(GeneratedTransferObject genType) {
45 if (isBaseEncodingImportRequired) {
46 addImport(BaseEncoding)
50 final private def boolean isBaseEncodingImportRequired() {
51 for (property : finalProperties) {
52 val propRet = property.returnType
53 if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
54 (propRet as GeneratedTransferObject).properties !== null &&
55 !(propRet as GeneratedTransferObject).properties.empty &&
56 ((propRet as GeneratedTransferObject).properties.size == 1) &&
57 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
58 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
64 override constructors() '''
65 «unionConstructorsParentProperties»
67 «IF !allProperties.empty»
70 «IF properties.empty && !parentProperties.empty»
75 private def unionConstructors() '''
76 «FOR property : finalProperties SEPARATOR "\n"»
77 «val propRet = property.returnType»
78 «IF "char[]".equals(propRet.name)»
80 * Constructor provided only for using in JMX. Don't use it for
81 * construction new object of this union type.
83 @«ConstructorProperties.importedName»("«property.name»")
84 public «type.name»(«propRet.importedName» «property.fieldName») {
85 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
86 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
87 «FOR other : finalProperties»
88 «IF other.name.equals("value")»
89 «IF other.returnType.importedName.contains("[]")»
90 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
92 this.«other.fieldName» = «other.fieldName»;
95 this.«other.fieldName» = defInst.«other.fieldName»;
100 «val propertyAndTopParentProperties = parentProperties + #[property]»
101 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
102 super(«parentProperties.asArguments»);
103 this.«property.fieldName» = «property.fieldName»;
104 «FOR other : finalProperties»
105 «IF property != other && !"value".equals(other.name)»
106 this.«other.fieldName» = null;
115 val outerCls = getOuterClassName(type);
116 if(outerCls !== null) {
117 return outerCls + type.name + "Builder"
119 return type.name + "Builder"
122 private def unionConstructorsParentProperties() '''
123 «FOR property : parentProperties SEPARATOR "\n"»
124 public «type.name»(«property.returnType.importedName» «property.fieldName») {
125 super(«property.fieldName»);
130 override protected getterMethod(GeneratedProperty field) {
131 if (!"value".equals(field.name)) {
132 return super.getterMethod(field)
135 Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
138 public char[] «field.getterMethodName»() {
139 if («field.fieldName» == null) {
140 «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
141 if («property.fieldName» != null) {
142 «val propRet = property.returnType»
143 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
145 «field.fieldName» = «property.fieldName».toCharArray();
146 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
147 ««« type instance-identifier
148 «field.fieldName» = «property.fieldName».toString().toCharArray();
149 «ELSEIF "byte[]".equals(propRet.name)»
151 «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
152 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
153 || propRet instanceof Enumeration
154 || propRet.fullyQualifiedName.startsWith("java.math")»
155 ««« type int*, uint, decimal64 or enumeration*
156 «field.fieldName» = «property.fieldName».toString().toCharArray();
157 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
159 «field.fieldName» = «property.fieldName».getValue();
160 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
161 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
162 && (propRet as GeneratedTransferObject).properties !== null
163 && !(propRet as GeneratedTransferObject).properties.empty
164 && ((propRet as GeneratedTransferObject).properties.size == 1)
165 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
166 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
167 ««« generated boolean typedef
168 «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
169 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
170 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
171 && (propRet as GeneratedTransferObject).properties !== null
172 && !(propRet as GeneratedTransferObject).properties.empty
173 && ((propRet as GeneratedTransferObject).properties.size == 1)
174 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
175 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
176 ««« generated byte[] typedef
177 «field.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
178 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
179 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
180 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
181 ««« generated bits typedef
182 «field.fieldName» = java.util.Arrays.toString(«property.fieldName».getValue()).toCharArray();
185 «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
190 return «field.fieldName» == null ? null : «field.fieldName».clone();
195 override def isReadOnly(GeneratedProperty field) {
196 return !"value".equals(field.name) && super.isReadOnly(field)
199 override protected copyConstructor() '''
201 * Creates a copy from Source Object.
203 * @param source Source object
205 public «type.name»(«type.name» source) {
206 «IF !parentProperties.empty»
209 «IF !properties.empty»
211 «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
212 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
214 this.«p.fieldName» = source.«p.fieldName»;