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.getOuterClassName;
13 import com.google.common.io.BaseEncoding
14 import java.util.Arrays
15 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
16 import org.opendaylight.mdsal.binding.model.api.Enumeration
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
20 * Template for generating JAVA class.
22 class UnionTemplate extends ClassTemplate {
25 * Creates instance of this class with concrete <code>genType</code>.
27 * @param genType generated transfer object which will be transformed to JAVA class source code
29 new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
30 super(javaType, genType)
34 * Creates instance of this class with concrete <code>genType</code>.
36 * @param genType generated transfer object which will be transformed to JAVA class source code
38 new(GeneratedTransferObject genType) {
42 override constructors() '''
43 «unionConstructorsParentProperties»
45 «IF !allProperties.empty»
48 «IF properties.empty && !parentProperties.empty»
55 private def unionConstructors() '''
56 «FOR property : finalProperties SEPARATOR "\n"»
57 «val actualType = property.returnType»
58 «val restrictions = restrictionsForSetter(actualType)»
59 «IF restrictions !== null»
60 «generateCheckers(property, restrictions, actualType)»
62 «val propertyAndTopParentProperties = parentProperties + #[property]»
63 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
64 super(«parentProperties.asArguments»);
65 «IF restrictions !== null»
66 «checkArgument(property, restrictions, actualType, property.fieldName.toString)»
68 this.«property.fieldName» = «property.fieldName»;
69 «FOR other : finalProperties»
70 «IF property != other»
71 this.«other.fieldName» = null;
79 val outerCls = getOuterClassName(type);
80 if(outerCls !== null) {
81 return outerCls + type.name + "Builder"
83 return type.name + "Builder"
86 private def unionConstructorsParentProperties() '''
87 «FOR property : parentProperties SEPARATOR "\n"»
88 public «type.name»(«property.returnType.importedName» «property.fieldName») {
89 super(«property.fieldName»);
94 def generateStringValue()
97 * Return a String representing the value of this union.
99 * @return String representation of this union's value.
101 public «String.importedName» stringValue() {
102 «FOR property : finalProperties»
103 «val field = property.fieldName»
104 if («field» != null) {
105 «val propRet = property.returnType»
106 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
109 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
110 ««« type instance-identifier
111 return «field».toString();
112 «ELSEIF "byte[]".equals(propRet.name)»
114 return new «String.importedName»(«field»);
115 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration
116 || propRet.fullyQualifiedName.startsWith("java.math")»
117 ««« type int*, uint, decimal64 or enumeration*
118 return «field».toString();
119 «ELSEIF propRet instanceof GeneratedTransferObject
120 && (propRet as GeneratedTransferObject).unionType»
122 return «field».stringValue();
123 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
124 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
125 && (propRet as GeneratedTransferObject).properties !== null
126 && !(propRet as GeneratedTransferObject).properties.empty
127 && ((propRet as GeneratedTransferObject).properties.size == 1)
128 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
129 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType) // And the property value is of type boolean»
130 ««« generated boolean typedef
131 return «field».isValue().toString();
132 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
133 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
134 && (propRet as GeneratedTransferObject).properties !== null
135 && !(propRet as GeneratedTransferObject).properties.empty
136 && ((propRet as GeneratedTransferObject).properties.size == 1)
137 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
138 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
139 ««« generated byte[] typedef
140 return «BaseEncoding.importedName».base64().encode(«field».getValue());
141 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
142 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
143 && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
144 ««« generated bits typedef
145 return «Arrays.importedName».toString(«field».getValue());
148 return «field».getValue().toString();
153 throw new IllegalStateException("No value assinged");
157 override protected copyConstructor() '''
159 * Creates a copy from Source Object.
161 * @param source Source object
163 public «type.name»(«type.name» source) {
164 «IF !parentProperties.empty»
168 «IF p.returnType.importedName.contains("[]")»
169 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
171 this.«p.fieldName» = source.«p.fieldName»;