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.BYTE_ARRAY;
12 import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
13 import static org.opendaylight.mdsal.binding.model.util.Types.getOuterClassName;
15 import java.util.Base64;
16 import org.gaul.modernizer_maven_annotations.SuppressModernizer
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
18 import org.opendaylight.mdsal.binding.model.api.Enumeration
19 import org.opendaylight.mdsal.binding.model.api.Type
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
23 * Template for generating JAVA class.
26 class UnionTemplate extends ClassTemplate {
28 * Creates instance of this class with concrete <code>genType</code>.
30 * @param genType generated transfer object which will be transformed to JAVA class source code
32 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
33 super(javaType, genType)
37 * Creates instance of this class with concrete <code>genType</code>.
39 * @param genType generated transfer object which will be transformed to JAVA class source code
41 new(GeneratedTransferObject genType) {
45 override constructors() '''
46 «unionConstructorsParentProperties»
48 «IF !allProperties.empty»
51 «IF properties.empty && !parentProperties.empty»
58 private def unionConstructors() '''
59 «FOR property : finalProperties SEPARATOR "\n"»
60 «val actualType = property.returnType»
61 «val restrictions = restrictionsForSetter(actualType)»
62 «IF restrictions !== null»
63 «generateCheckers(property, restrictions, actualType)»
65 «val propertyAndTopParentProperties = parentProperties + #[property]»
66 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
67 super(«parentProperties.asArguments»);
68 «IF restrictions !== null»
69 «checkArgument(property, restrictions, actualType, property.fieldName)»
71 this.«property.fieldName» = «property.fieldName»;
72 «FOR other : finalProperties»
73 «IF property != other»
74 this.«other.fieldName» = null;
82 val outerCls = getOuterClassName(type);
83 if(outerCls !== null) {
84 return outerCls + type.name + "Builder"
86 return type.name + "Builder"
89 private def unionConstructorsParentProperties() '''
90 «FOR property : parentProperties SEPARATOR "\n"»
91 public «type.name»(«property.returnType.importedName» «property.fieldName») {
92 super(«property.fieldName»);
97 def generateStringValue()
100 * Return a String representing the value of this union.
102 * @return String representation of this union's value.
104 public «STRING.importedName» stringValue() {
105 «FOR property : finalProperties»
106 «val field = property.fieldName»
107 if («field» != null) {
108 «val propRet = property.returnType»
109 «IF STRING.equals(propRet)»
112 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
113 ««« type instance-identifier
114 return «field».toString();
115 «ELSEIF BYTE_ARRAY.equals(propRet)»
117 return new «STRING.importedName»(«field»);
118 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
119 || propRet.fullyQualifiedName.startsWith("java.math")»
120 ««« type int*, decimal64 or enumeration*
121 return «field».toString();
122 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
123 && propRet.name.startsWith("Uint")»
125 return «field».toCanonicalString();
126 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
128 return «field».stringValue();
129 «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
130 ««« generated boolean typedef
131 return «field».isValue().toString();
132 «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
133 ««« generated byte[] typedef
134 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
135 «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.equals(propRet.typedefReturnType)»
136 ««« generated empty typedef
138 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
139 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
140 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
141 ««« generated bits typedef
142 return «JU_ARRAYS.importedName».toString(«field».getValue());
145 return «field».getValue().toString();
150 throw new IllegalStateException("No value assinged");
154 private static def Type typedefReturnType(Type type) {
155 if (!(type instanceof GeneratedTransferObject)) {
158 val gto = type as GeneratedTransferObject
159 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
162 val prop = gto.properties.get(0)
163 if (prop.name.equals("value")) {
164 return prop.returnType
169 override protected copyConstructor() '''
171 * Creates a copy from Source Object.
173 * @param source Source object
175 public «type.name»(«type.name» source) {
176 «IF !parentProperties.empty»
180 «val fieldName = p.fieldName»
181 «IF p.returnType.name.endsWith("[]")»
182 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
184 this.«fieldName» = source.«fieldName»;