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.mdsal.binding.model.util.Types
21 import org.opendaylight.yangtools.yang.common.Empty
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
25 * 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*, uint, decimal64 or enumeration*
122 return «field».toString();
123 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
125 return «field».stringValue();
126 «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
127 ««« generated boolean typedef
128 return «field».isValue().toString();
129 «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
130 ««« generated byte[] typedef
131 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
132 «ELSEIF Constants.EMPTY.equals(propRet) || Constants.EMPTY.equals(propRet.typedefReturnType)»
133 ««« generated empty typedef
135 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
136 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
137 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
138 ««« generated bits typedef
139 return «Arrays.importedName».toString(«field».getValue());
142 return «field».getValue().toString();
147 throw new IllegalStateException("No value assinged");
151 private static def Type typedefReturnType(Type type) {
152 if (!(type instanceof GeneratedTransferObject)) {
155 val gto = type as GeneratedTransferObject
156 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
159 val prop = gto.properties.get(0)
160 if (prop.name.equals("value")) {
161 return prop.returnType
166 override protected copyConstructor() '''
168 * Creates a copy from Source Object.
170 * @param source Source object
172 public «type.name»(«type.name» source) {
173 «IF !parentProperties.empty»
177 «IF p.returnType.importedName.contains("[]")»
178 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
180 this.«p.fieldName» = source.«p.fieldName»;