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 java.beans.ConstructorProperties
15 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
16 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
17 import org.opendaylight.mdsal.binding.model.api.Enumeration
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
21 * Template for generating JAVA class.
23 class UnionTemplate extends ClassTemplate {
26 * Creates instance of this class with concrete <code>genType</code>.
28 * @param genType generated transfer object which will be transformed to JAVA class source code
30 new(GeneratedTransferObject genType) {
32 if (isBaseEncodingImportRequired) {
33 addImport("BaseEncoding","com.google.common.io")
37 final private def boolean isBaseEncodingImportRequired() {
38 for (property : finalProperties) {
39 val propRet = property.returnType
40 if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
41 (propRet as GeneratedTransferObject).properties !== null &&
42 !(propRet as GeneratedTransferObject).properties.empty &&
43 ((propRet as GeneratedTransferObject).properties.size == 1) &&
44 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
45 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
51 override constructors() '''
52 «unionConstructorsParentProperties»
54 «IF !allProperties.empty»
57 «IF properties.empty && !parentProperties.empty»
62 private def unionConstructors() '''
63 «FOR property : finalProperties SEPARATOR "\n"»
64 «val propRet = property.returnType»
65 «IF "char[]".equals(propRet.name)»
67 * Constructor provided only for using in JMX. Don't use it for
68 * construction new object of this union type.
70 @«ConstructorProperties.importedName»("«property.name»")
71 public «type.name»(«propRet.importedName» «property.fieldName») {
72 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
73 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
74 «FOR other : finalProperties»
75 «IF other.name.equals("value")»
76 «IF other.returnType.importedName.contains("[]")»
77 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
79 this.«other.fieldName» = «other.fieldName»;
82 this.«other.fieldName» = defInst.«other.fieldName»;
87 «val propertyAndTopParentProperties = parentProperties + #[property]»
88 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
89 super(«parentProperties.asArguments»);
90 this.«property.fieldName» = «property.fieldName»;
91 «FOR other : finalProperties»
92 «IF property != other && !"value".equals(other.name)»
93 this.«other.fieldName» = null;
102 val outerCls = getOuterClassName(type);
103 if(outerCls !== null) {
104 return outerCls + type.name + "Builder"
106 return type.name + "Builder"
109 private def unionConstructorsParentProperties() '''
110 «FOR property : parentProperties SEPARATOR "\n"»
111 public «type.name»(«property.returnType.importedName» «property.fieldName») {
112 super(«property.fieldName»);
117 override protected getterMethod(GeneratedProperty field) {
118 if (!"value".equals(field.name)) {
119 return super.getterMethod(field)
122 Preconditions.checkArgument("char[]".equals(field.returnType.importedName))
125 public char[] «field.getterMethodName»() {
126 if («field.fieldName» == null) {
127 «FOR property : finalProperties.filter([ p | !"value".equals(p.name)]) SEPARATOR " else"»
128 if («property.fieldName» != null) {
129 «val propRet = property.returnType»
130 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
132 «field.fieldName» = «property.fieldName».toCharArray();
133 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
134 ««« type instance-identifier
135 «field.fieldName» = «property.fieldName».toString().toCharArray();
136 «ELSEIF "byte[]".equals(propRet.name)»
138 «field.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
139 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
140 || propRet instanceof Enumeration
141 || propRet.fullyQualifiedName.startsWith("java.math")»
142 ««« type int*, uint, decimal64 or enumeration*
143 «field.fieldName» = «property.fieldName».toString().toCharArray();
144 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
146 «field.fieldName» = «property.fieldName».getValue();
147 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
148 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
149 && (propRet as GeneratedTransferObject).properties !== null
150 && !(propRet as GeneratedTransferObject).properties.empty
151 && ((propRet as GeneratedTransferObject).properties.size == 1)
152 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
153 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
154 ««« generated boolean typedef
155 «field.fieldName» = «property.fieldName».isValue().toString().toCharArray();
156 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
157 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
158 && (propRet as GeneratedTransferObject).properties !== null
159 && !(propRet as GeneratedTransferObject).properties.empty
160 && ((propRet as GeneratedTransferObject).properties.size == 1)
161 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
162 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
163 ««« generated byte[] typedef
164 «field.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
165 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
166 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
167 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
168 ««« generated bits typedef
169 «field.fieldName» = java.util.Arrays.toString(«property.fieldName».getValue()).toCharArray();
172 «field.fieldName» = «property.fieldName».getValue().toString().toCharArray();
177 return «field.fieldName» == null ? null : «field.fieldName».clone();
182 override def isReadOnly(GeneratedProperty field) {
183 return !"value".equals(field.name) && super.isReadOnly(field)
186 override protected copyConstructor() '''
188 * Creates a copy from Source Object.
190 * @param source Source object
192 public «type.name»(«type.name» source) {
193 «IF !parentProperties.empty»
196 «IF !properties.empty»
198 «IF !"value".equals(p.name) && p.returnType.importedName.contains("[]")»
199 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
201 this.«p.fieldName» = source.«p.fieldName»;