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 java.util.Arrays
14 import java.util.Base64;
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(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
30 super(javaType, genType)
34 * Creates instance of this class with concrete <code>genType</code>.
36 * @param genType generated transfer object which will be transformed to JAVA class source code
38 new(GeneratedTransferObject genType) {
42 override constructors() '''
43 «unionConstructorsParentProperties»
45 «IF !allProperties.empty»
48 «IF properties.empty && !parentProperties.empty»
55 private def unionConstructors() '''
56 «FOR property : finalProperties SEPARATOR "\n"»
57 «val actualType = property.returnType»
58 «val restrictions = restrictionsForSetter(actualType)»
59 «IF restrictions !== null»
60 «generateCheckers(property, restrictions, actualType)»
62 «val propertyAndTopParentProperties = parentProperties + #[property]»
63 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
64 super(«parentProperties.asArguments»);
65 «IF restrictions !== null»
66 «checkArgument(property, restrictions, actualType, property.fieldName.toString)»
68 this.«property.fieldName» = «property.fieldName»;
69 «FOR other : finalProperties»
70 «IF property != other»
71 this.«other.fieldName» = null;
79 val outerCls = getOuterClassName(type);
80 if(outerCls !== null) {
81 return outerCls + type.name + "Builder"
83 return type.name + "Builder"
86 private def unionConstructorsParentProperties() '''
87 «FOR property : parentProperties SEPARATOR "\n"»
88 public «type.name»(«property.returnType.importedName» «property.fieldName») {
89 super(«property.fieldName»);
94 def generateStringValue()
97 * Return a String representing the value of this union.
99 * @return String representation of this union's value.
101 public «String.importedName» stringValue() {
102 «FOR property : finalProperties»
103 «val field = property.fieldName»
104 if («field» != null) {
105 «val propRet = property.returnType»
106 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
109 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
110 ««« type instance-identifier
111 return «field».toString();
112 «ELSEIF "byte[]".equals(propRet.name)»
114 return new «String.importedName»(«field»);
115 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
116 || propRet.fullyQualifiedName.startsWith("java.math")»
117 ««« type int*, uint, decimal64 or enumeration*
118 return «field».toString();
119 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
121 return «field».stringValue();
122 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
123 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
124 && (propRet as GeneratedTransferObject).properties !== null
125 && !(propRet as GeneratedTransferObject).properties.empty
126 && ((propRet as GeneratedTransferObject).properties.size == 1)
127 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
128 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType) // And the property value is of type boolean»
129 ««« generated boolean typedef
130 return «field».isValue().toString();
131 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
132 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
133 && (propRet as GeneratedTransferObject).properties !== null
134 && !(propRet as GeneratedTransferObject).properties.empty
135 && ((propRet as GeneratedTransferObject).properties.size == 1)
136 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
137 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
138 ««« generated byte[] typedef
139 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
140 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
141 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
142 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
143 ««« generated bits typedef
144 return «Arrays.importedName».toString(«field».getValue());
147 return «field».getValue().toString();
152 throw new IllegalStateException("No value assinged");
156 override protected copyConstructor() '''
158 * Creates a copy from Source Object.
160 * @param source Source object
162 public «type.name»(«type.name» source) {
163 «IF !parentProperties.empty»
167 «IF p.returnType.importedName.contains("[]")»
168 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
170 this.«p.fieldName» = source.«p.fieldName»;