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.ri.BaseYangTypes.BINARY_TYPE
11 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.BOOLEAN_TYPE
12 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.EMPTY_TYPE
13 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.STRING_TYPE
14 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING
15 import static org.opendaylight.mdsal.binding.model.ri.Types.getOuterClassName
16 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME
17 import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.BUILDER_SUFFIX
18 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
19 import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isIdentityType
21 import java.util.Base64;
22 import org.gaul.modernizer_maven_annotations.SuppressModernizer
23 import org.opendaylight.mdsal.binding.model.api.Enumeration
24 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
25 import org.opendaylight.mdsal.binding.model.api.Type
28 * Template for generating JAVA class.
31 class UnionTemplate extends ClassTemplate {
33 * Creates instance of this class with concrete <code>genType</code>.
35 * @param genType generated transfer object which will be transformed to JAVA class source code
37 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
38 super(javaType, genType)
42 * Creates instance of this class with concrete <code>genType</code>.
44 * @param genType generated transfer object which will be transformed to JAVA class source code
46 new(GeneratedTransferObject genType) {
50 override constructors() '''
51 «unionConstructorsParentProperties»
53 «IF !allProperties.empty»
56 «IF properties.empty && !parentProperties.empty»
63 private def unionConstructors() '''
64 «FOR property : finalProperties SEPARATOR "\n"»
65 «val actualType = property.returnType»
66 «val restrictions = restrictionsForSetter(actualType)»
67 «IF restrictions !== null»
68 «generateCheckers(property, restrictions, actualType)»
70 «val propertyAndTopParentProperties = parentProperties + #[property]»
71 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
72 super(«parentProperties.asArguments»);
73 «IF restrictions !== null»
74 «checkArgument(property, restrictions, actualType, property.fieldName)»
76 this.«property.fieldName» = «property.fieldName»;
77 «FOR other : finalProperties»
78 «IF property != other»
79 this.«other.fieldName» = null;
87 val outerCls = getOuterClassName(type);
88 if (outerCls !== null) {
89 return outerCls + type.name + BUILDER_SUFFIX
91 return type.name + BUILDER_SUFFIX
94 private def unionConstructorsParentProperties() '''
95 «FOR property : parentProperties SEPARATOR "\n"»
96 public «type.name»(«property.returnType.importedName» «property.fieldName») {
97 super(«property.fieldName»);
102 def generateStringValue()
105 * Return a String representing the value of this union.
107 * @return String representation of this union's value.
109 public «STRING.importedName» stringValue() {
110 «FOR property : finalProperties»
111 «val field = property.fieldName»
112 if («field» != null) {
113 «val propRet = property.returnType»
114 «IF STRING_TYPE.equals(propRet)»
117 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
118 ««« type instance-identifier
119 return «field».toString();
120 «ELSEIF BINARY_TYPE.equals(propRet)»
122 return new «STRING.importedName»(«field»);
123 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
124 ««« type int* or enumeration*
125 return «field».toString();
126 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
127 && (propRet.name.startsWith("Uint") || "Decimal64".equals(propRet.name))»
128 ««« type uint*, decimal64
129 return «field».toCanonicalString();
130 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
132 return «field».stringValue();
133 «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
134 ««« generated boolean typedef
135 return «field».isValue().toString();
136 «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
137 ««« generated byte[] typedef
138 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
139 «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
140 ««« generated empty typedef
142 «ELSEIF propRet.isBitsType»
143 ««« generated bits typedef
144 return «JU_ARRAYS.importedName».toString(«field».getValue());
145 «ELSEIF propRet.isIdentityType»
146 ««« generated identity
147 return «field».«BINDING_CONTRACT_IMPLEMENTED_INTERFACE_NAME»().toString();
150 return «field».getValue().toString();
154 throw new IllegalStateException("No value assigned");
158 private static def Type typedefReturnType(Type type) {
159 if (!(type instanceof GeneratedTransferObject)) {
162 val gto = type as GeneratedTransferObject
163 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
166 val prop = gto.properties.get(0)
167 if (prop.name.equals("value")) {
168 return prop.returnType
173 override protected copyConstructor() '''
175 * Creates a copy from Source Object.
177 * @param source Source object
179 public «type.name»(«type.name» source) {
180 «IF !parentProperties.empty»
184 «val fieldName = p.fieldName»
185 «IF p.returnType.name.endsWith("[]")»
186 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
188 this.«fieldName» = source.«fieldName»;