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.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.
25 class UnionTemplate extends ClassTemplate {
27 * Creates instance of this class with concrete <code>genType</code>.
29 * @param genType generated transfer object which will be transformed to JAVA class source code
31 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
32 super(javaType, genType)
36 * Creates instance of this class with concrete <code>genType</code>.
38 * @param genType generated transfer object which will be transformed to JAVA class source code
40 new(GeneratedTransferObject genType) {
44 override constructors() '''
45 «unionConstructorsParentProperties»
47 «IF !allProperties.empty»
50 «IF properties.empty && !parentProperties.empty»
57 private def unionConstructors() '''
58 «FOR property : finalProperties SEPARATOR "\n"»
59 «val actualType = property.returnType»
60 «val restrictions = restrictionsForSetter(actualType)»
61 «IF restrictions !== null»
62 «generateCheckers(property, restrictions, actualType)»
64 «val propertyAndTopParentProperties = parentProperties + #[property]»
65 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
66 super(«parentProperties.asArguments»);
67 «IF restrictions !== null»
68 «checkArgument(property, restrictions, actualType, property.fieldName.toString)»
70 this.«property.fieldName» = «property.fieldName»;
71 «FOR other : finalProperties»
72 «IF property != other»
73 this.«other.fieldName» = null;
81 val outerCls = getOuterClassName(type);
82 if(outerCls !== null) {
83 return outerCls + type.name + "Builder"
85 return type.name + "Builder"
88 private def unionConstructorsParentProperties() '''
89 «FOR property : parentProperties SEPARATOR "\n"»
90 public «type.name»(«property.returnType.importedName» «property.fieldName») {
91 super(«property.fieldName»);
96 def generateStringValue()
99 * Return a String representing the value of this union.
101 * @return String representation of this union's value.
103 public «String.importedName» stringValue() {
104 «FOR property : finalProperties»
105 «val field = property.fieldName»
106 if («field» != null) {
107 «val propRet = property.returnType»
108 «IF STRING.equals(propRet)»
111 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
112 ««« type instance-identifier
113 return «field».toString();
114 «ELSEIF BYTE_ARRAY.equals(propRet)»
116 return new «String.importedName»(«field»);
117 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
118 || propRet.fullyQualifiedName.startsWith("java.math")»
119 ««« type int*, uint, decimal64 or enumeration*
120 return «field».toString();
121 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
123 return «field».stringValue();
124 «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
125 ««« generated boolean typedef
126 return «field».isValue().toString();
127 «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
128 ««« generated byte[] typedef
129 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
130 «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.equals(propRet.typedefReturnType)»
131 ««« generated empty typedef
133 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
134 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
135 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
136 ««« generated bits typedef
137 return «Arrays.importedName».toString(«field».getValue());
140 return «field».getValue().toString();
145 throw new IllegalStateException("No value assinged");
149 private static def Type typedefReturnType(Type type) {
150 if (!(type instanceof GeneratedTransferObject)) {
153 val gto = type as GeneratedTransferObject
154 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
157 val prop = gto.properties.get(0)
158 if (prop.name.equals("value")) {
159 return prop.returnType
164 override protected copyConstructor() '''
166 * Creates a copy from Source Object.
168 * @param source Source object
170 public «type.name»(«type.name» source) {
171 «IF !parentProperties.empty»
175 «val fieldName = p.fieldName»
176 «IF p.returnType.name.endsWith("[]")»
177 this.«fieldName» = source.«fieldName» == null ? null : source.«fieldName».clone();
179 this.«fieldName» = source.«fieldName»;