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.BUILDER_SUFFIX
18 import java.util.Base64;
19 import org.gaul.modernizer_maven_annotations.SuppressModernizer
20 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
21 import org.opendaylight.mdsal.binding.model.api.Enumeration
22 import org.opendaylight.mdsal.binding.model.api.Type
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
26 * Template for generating JAVA class.
29 class UnionTemplate extends ClassTemplate {
31 * Creates instance of this class with concrete <code>genType</code>.
33 * @param genType generated transfer object which will be transformed to JAVA class source code
35 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
36 super(javaType, genType)
40 * Creates instance of this class with concrete <code>genType</code>.
42 * @param genType generated transfer object which will be transformed to JAVA class source code
44 new(GeneratedTransferObject genType) {
48 override constructors() '''
49 «unionConstructorsParentProperties»
51 «IF !allProperties.empty»
54 «IF properties.empty && !parentProperties.empty»
61 private def unionConstructors() '''
62 «FOR property : finalProperties SEPARATOR "\n"»
63 «val actualType = property.returnType»
64 «val restrictions = restrictionsForSetter(actualType)»
65 «IF restrictions !== null»
66 «generateCheckers(property, restrictions, actualType)»
68 «val propertyAndTopParentProperties = parentProperties + #[property]»
69 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
70 super(«parentProperties.asArguments»);
71 «IF restrictions !== null»
72 «checkArgument(property, restrictions, actualType, property.fieldName)»
74 this.«property.fieldName» = «property.fieldName»;
75 «FOR other : finalProperties»
76 «IF property != other»
77 this.«other.fieldName» = null;
85 val outerCls = getOuterClassName(type);
86 if (outerCls !== null) {
87 return outerCls + type.name + BUILDER_SUFFIX
89 return type.name + BUILDER_SUFFIX
92 private def unionConstructorsParentProperties() '''
93 «FOR property : parentProperties SEPARATOR "\n"»
94 public «type.name»(«property.returnType.importedName» «property.fieldName») {
95 super(«property.fieldName»);
100 def generateStringValue()
103 * Return a String representing the value of this union.
105 * @return String representation of this union's value.
107 public «STRING.importedName» stringValue() {
108 «FOR property : finalProperties»
109 «val field = property.fieldName»
110 if («field» != null) {
111 «val propRet = property.returnType»
112 «IF STRING_TYPE.equals(propRet)»
115 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
116 ««« type instance-identifier
117 return «field».toString();
118 «ELSEIF BINARY_TYPE.equals(propRet)»
120 return new «STRING.importedName»(«field»);
121 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
122 || propRet.fullyQualifiedName.startsWith("java.math")»
123 ««« type int*, decimal64 or enumeration*
124 return «field».toString();
125 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
126 && propRet.name.startsWith("Uint")»
128 return «field».toCanonicalString();
129 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
131 return «field».stringValue();
132 «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
133 ««« generated boolean typedef
134 return «field».isValue().toString();
135 «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
136 ««« generated byte[] typedef
137 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
138 «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
139 ««« generated empty typedef
141 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
142 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
143 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
144 ««« generated bits typedef
145 return «JU_ARRAYS.importedName».toString(«field».getValue());
148 return «field».getValue().toString();
152 throw new IllegalStateException("No value assigned");
156 private static def Type typedefReturnType(Type type) {
157 if (!(type instanceof GeneratedTransferObject)) {
160 val gto = type as GeneratedTransferObject
161 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
164 val prop = gto.properties.get(0)
165 if (prop.name.equals("value")) {
166 return prop.returnType
171 override protected copyConstructor() '''
173 * Creates a copy from Source Object.
175 * @param source Source object
177 public «type.name»(«type.name» source) {
178 «IF !parentProperties.empty»
182 «val fieldName = p.fieldName»
183 «IF p.returnType.name.endsWith("[]")»
184 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
186 this.«fieldName» = source.«fieldName»;