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 {
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.toString)»
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*, uint, decimal64 or enumeration*
121 return «field».toString();
122 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
124 return «field».stringValue();
125 «ELSEIF BOOLEAN.equals(propRet.typedefReturnType)»
126 ««« generated boolean typedef
127 return «field».isValue().toString();
128 «ELSEIF BYTE_ARRAY.equals(propRet.typedefReturnType)»
129 ««« generated byte[] typedef
130 return «Base64.importedName».getEncoder().encodeToString(«field».getValue());
131 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
132 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
133 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
134 ««« generated bits typedef
135 return «Arrays.importedName».toString(«field».getValue());
138 return «field».getValue().toString();
143 throw new IllegalStateException("No value assinged");
147 private static def Type typedefReturnType(Type type) {
148 if (!(type instanceof GeneratedTransferObject)) {
151 val gto = type as GeneratedTransferObject
152 if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
155 val prop = gto.properties.get(0)
156 if (prop.name.equals("value")) {
157 return prop.returnType
162 override protected copyConstructor() '''
164 * Creates a copy from Source Object.
166 * @param source Source object
168 public «type.name»(«type.name» source) {
169 «IF !parentProperties.empty»
173 «IF p.returnType.importedName.contains("[]")»
174 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
176 this.«p.fieldName» = source.«p.fieldName»;