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.Arrays
16 import java.util.Base64;
17 import org.gaul.modernizer_maven_annotations.SuppressModernizer
18 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
19 import org.opendaylight.mdsal.binding.model.api.Enumeration
20 import org.opendaylight.mdsal.binding.model.api.Type
21 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
24 * Template for generating JAVA class.
27 class UnionTemplate extends ClassTemplate {
29 * Creates instance of this class with concrete <code>genType</code>.
31 * @param genType generated transfer object which will be transformed to JAVA class source code
33 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
34 super(javaType, genType)
38 * Creates instance of this class with concrete <code>genType</code>.
40 * @param genType generated transfer object which will be transformed to JAVA class source code
42 new(GeneratedTransferObject genType) {
46 override constructors() '''
47 «unionConstructorsParentProperties»
49 «IF !allProperties.empty»
52 «IF properties.empty && !parentProperties.empty»
59 private def unionConstructors() '''
60 «FOR property : finalProperties SEPARATOR "\n"»
61 «val actualType = property.returnType»
62 «val restrictions = restrictionsForSetter(actualType)»
63 «IF restrictions !== null»
64 «generateCheckers(property, restrictions, actualType)»
66 «val propertyAndTopParentProperties = parentProperties + #[property]»
67 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
68 super(«parentProperties.asArguments»);
69 «IF restrictions !== null»
70 «checkArgument(property, restrictions, actualType, property.fieldName.toString)»
72 this.«property.fieldName» = «property.fieldName»;
73 «FOR other : finalProperties»
74 «IF property != other»
75 this.«other.fieldName» = null;
83 val outerCls = getOuterClassName(type);
84 if(outerCls !== null) {
85 return outerCls + type.name + "Builder"
87 return type.name + "Builder"
90 private def unionConstructorsParentProperties() '''
91 «FOR property : parentProperties SEPARATOR "\n"»
92 public «type.name»(«property.returnType.importedName» «property.fieldName») {
93 super(«property.fieldName»);
98 def generateStringValue()
101 * Return a String representing the value of this union.
103 * @return String representation of this union's value.
105 public «String.importedName» stringValue() {
106 «FOR property : finalProperties»
107 «val field = property.fieldName»
108 if («field» != null) {
109 «val propRet = property.returnType»
110 «IF STRING.equals(propRet)»
113 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
114 ««« type instance-identifier
115 return «field».toString();
116 «ELSEIF BYTE_ARRAY.equals(propRet)»
118 return new «String.importedName»(«field»);
119 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
120 || propRet.fullyQualifiedName.startsWith("java.math")»
121 ««« type int*, decimal64 or enumeration*
122 return «field».toString();
123 «ELSEIF "org.opendaylight.yangtools.yang.common".equals(propRet.packageName)
124 && propRet.name.startsWith("Uint")»
126 return «field».toCanonicalString();
127 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
129 return «field».stringValue();
130 «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
131 ««« generated boolean typedef
132 return «field».isValue().toString();
133 «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
134 ««« generated byte[] typedef
135 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
136 «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.equals(propRet.typedefReturnType)»
137 ««« generated empty typedef
139 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
140 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
141 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
142 ««« generated bits typedef
143 return «Arrays.importedName».toString(«field».getValue());
146 return «field».getValue().toString();
151 throw new IllegalStateException("No value assinged");
155 private static def Type typedefReturnType(Type type) {
156 if (!(type instanceof GeneratedTransferObject)) {
159 val gto = type as GeneratedTransferObject
160 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
163 val prop = gto.properties.get(0)
164 if (prop.name.equals("value")) {
165 return prop.returnType
170 override protected copyConstructor() '''
172 * Creates a copy from Source Object.
174 * @param source Source object
176 public «type.name»(«type.name» source) {
177 «IF !parentProperties.empty»
181 «val fieldName = p.fieldName»
182 «IF p.returnType.importedName.contains("[]")»
183 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
185 this.«fieldName» = source.«fieldName»;