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.generator.util.Types.*
12 import com.google.common.base.Preconditions;
13 import java.beans.ConstructorProperties
14 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
15 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
16 import org.opendaylight.mdsal.binding.model.api.Enumeration
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
20 * Template for generating JAVA class.
22 class UnionTemplate extends ClassTemplate {
25 * Creates instance of this class with concrete <code>genType</code>.
27 * @param genType generated transfer object which will be transformed to JAVA class source code
29 new(GeneratedTransferObject genType) {
31 if(isBaseEncodingImportRequired) {
32 this.importMap.put("BaseEncoding","com.google.common.io")
36 final private def boolean isBaseEncodingImportRequired() {
37 for (property : finalProperties) {
38 val propRet = property.returnType
39 if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
40 (propRet as GeneratedTransferObject).properties !== null &&
41 !(propRet as GeneratedTransferObject).properties.empty &&
42 ((propRet as GeneratedTransferObject).properties.size == 1) &&
43 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
44 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
50 override constructors() '''
51 «unionConstructorsParentProperties»
53 «IF !allProperties.empty»
56 «IF properties.empty && !parentProperties.empty»
61 private def unionConstructors() '''
62 «FOR property : finalProperties SEPARATOR "\n"»
63 «val propRet = property.returnType»
64 «IF "char[]".equals(propRet.name)»
66 * Constructor provided only for using in JMX. Don't use it for
67 * construction new object of this union type.
69 @«ConstructorProperties.importedName»("«property.name»")
70 public «type.name»(«propRet.importedName» «property.fieldName») {
71 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
72 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
73 «FOR other : finalProperties»
74 «IF other.name.equals("value")»
75 «IF other.returnType.importedName.contains("[]")»
76 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
78 this.«other.fieldName» = «other.fieldName»;
81 this.«other.fieldName» = defInst.«other.fieldName»;
86 «val propertyAndTopParentProperties = parentProperties + #[property]»
87 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
88 super(«parentProperties.asArguments»);
89 this.«property.fieldName» = «property.fieldName»;
90 «FOR other : finalProperties»
91 «IF property != other && !"value".equals(other.name)»
92 this.«other.fieldName» = null;
101 val outerCls = getOuterClassName(type);
102 if(outerCls !== null) {
103 return outerCls + type.name + "Builder"
105 return type.name + "Builder"
108 private def unionConstructorsParentProperties() '''
109 «FOR property : parentProperties SEPARATOR "\n"»
110 public «type.name»(«property.returnType.importedName» «property.fieldName») {
111 super(«property.fieldName»);
116 override protected getterMethod(GeneratedProperty field) {
117 if (!"value".equals(field.name)) {
118 return super.getterMethod(field)
121 Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
124 public char[] «field.getterMethodName»() {
125 if («field.fieldName» == null) {
126 «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
127 if («property.fieldName» != null) {
128 «val propRet = property.returnType»
129 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
131 «field.fieldName» = «property.fieldName».toCharArray();
132 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
133 ««« type instance-identifier
134 «field.fieldName» = «property.fieldName».toString().toCharArray();
135 «ELSEIF "byte[]".equals(propRet.name)»
137 «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
138 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
139 || propRet instanceof Enumeration
140 || propRet.fullyQualifiedName.startsWith("java.math")»
141 ««« type int*, uint, decimal64 or enumeration*
142 «field.fieldName» = «property.fieldName».toString().toCharArray();
143 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
145 «field.fieldName» = «property.fieldName».getValue();
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 «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
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 «field.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
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 «field.fieldName» = java.util.Arrays.toString(«property.fieldName».getValue()).toCharArray();
171 «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
176 return «field.fieldName» == null ? null : «field.fieldName».clone();
181 override def isReadOnly(GeneratedProperty field) {
182 return !"value".equals(field.name) && super.isReadOnly(field)
185 override protected copyConstructor() '''
187 * Creates a copy from Source Object.
189 * @param source Source object
191 public «type.name»(«type.name» source) {
192 «IF !parentProperties.empty»
195 «IF !properties.empty»
197 «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
198 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
200 this.«p.fieldName» = source.«p.fieldName»;