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.BaseYangTypes.BINARY_TYPE;
11 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.BOOLEAN_TYPE;
12 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.EMPTY_TYPE;
13 import static org.opendaylight.mdsal.binding.model.util.BaseYangTypes.STRING_TYPE;
14 import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
15 import static org.opendaylight.mdsal.binding.model.util.Types.getOuterClassName;
17 import java.util.Base64;
18 import org.gaul.modernizer_maven_annotations.SuppressModernizer
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
20 import org.opendaylight.mdsal.binding.model.api.Enumeration
21 import org.opendaylight.mdsal.binding.model.api.Type
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
25 * Template for generating JAVA class.
28 class UnionTemplate extends ClassTemplate {
30 * Creates instance of this class with concrete <code>genType</code>.
32 * @param genType generated transfer object which will be transformed to JAVA class source code
34 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
35 super(javaType, genType)
39 * Creates instance of this class with concrete <code>genType</code>.
41 * @param genType generated transfer object which will be transformed to JAVA class source code
43 new(GeneratedTransferObject genType) {
47 override constructors() '''
48 «unionConstructorsParentProperties»
50 «IF !allProperties.empty»
53 «IF properties.empty && !parentProperties.empty»
60 private def unionConstructors() '''
61 «FOR property : finalProperties SEPARATOR "\n"»
62 «val actualType = property.returnType»
63 «val restrictions = restrictionsForSetter(actualType)»
64 «IF restrictions !== null»
65 «generateCheckers(property, restrictions, actualType)»
67 «val propertyAndTopParentProperties = parentProperties + #[property]»
68 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
69 super(«parentProperties.asArguments»);
70 «IF restrictions !== null»
71 «checkArgument(property, restrictions, actualType, property.fieldName)»
73 this.«property.fieldName» = «property.fieldName»;
74 «FOR other : finalProperties»
75 «IF property != other»
76 this.«other.fieldName» = null;
84 val outerCls = getOuterClassName(type);
85 if(outerCls !== null) {
86 return outerCls + type.name + "Builder"
88 return type.name + "Builder"
91 private def unionConstructorsParentProperties() '''
92 «FOR property : parentProperties SEPARATOR "\n"»
93 public «type.name»(«property.returnType.importedName» «property.fieldName») {
94 super(«property.fieldName»);
99 def generateStringValue()
102 * Return a String representing the value of this union.
104 * @return String representation of this union's value.
106 public «STRING.importedName» stringValue() {
107 «FOR property : finalProperties»
108 «val field = property.fieldName»
109 if («field» != null) {
110 «val propRet = property.returnType»
111 «IF STRING_TYPE.equals(propRet)»
114 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
115 ««« type instance-identifier
116 return «field».toString();
117 «ELSEIF BINARY_TYPE.equals(propRet)»
119 return new «STRING.importedName»(«field»);
120 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
121 || propRet.fullyQualifiedName.startsWith("java.math")»
122 ««« type int*, decimal64 or enumeration*
123 return «field».toString();
124 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
125 && propRet.name.startsWith("Uint")»
127 return «field».toCanonicalString();
128 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
130 return «field».stringValue();
131 «ELSEIF BOOLEAN_TYPE.equals(propRet.typedefReturnType)»
132 ««« generated boolean typedef
133 return «field».isValue().toString();
134 «ELSEIF BINARY_TYPE.equals(propRet.typedefReturnType)»
135 ««« generated byte[] typedef
136 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
137 «ELSEIF EMPTY_TYPE.equals(propRet) || EMPTY_TYPE.equals(propRet.typedefReturnType)»
138 ««« generated empty typedef
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 «JU_ARRAYS.importedName».toString(«field».getValue());
147 return «field».getValue().toString();
152 throw new IllegalStateException("No value assinged");
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»;