Speed up Union stringValue() generation
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / UnionTemplate.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator
9
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;
14
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
21
22 /**
23  * Template for generating JAVA class.
24  */
25 class UnionTemplate extends ClassTemplate {
26
27     /**
28      * Creates instance of this class with concrete <code>genType</code>.
29      *
30      * @param genType generated transfer object which will be transformed to JAVA class source code
31      */
32     new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
33         super(javaType, genType)
34     }
35
36     /**
37      * Creates instance of this class with concrete <code>genType</code>.
38      *
39      * @param genType generated transfer object which will be transformed to JAVA class source code
40      */
41     new(GeneratedTransferObject genType) {
42         super(genType)
43     }
44
45     override constructors() '''
46         «unionConstructorsParentProperties»
47         «unionConstructors»
48         «IF !allProperties.empty»
49             «copyConstructor»
50         «ENDIF»
51         «IF properties.empty && !parentProperties.empty»
52             «parentConstructor»
53         «ENDIF»
54
55         «generateStringValue»
56     '''
57
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)»
64             «ENDIF»
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)»
70                 «ENDIF»
71                 this.«property.fieldName» = «property.fieldName»;
72                 «FOR other : finalProperties»
73                     «IF property != other»
74                          this.«other.fieldName» = null;
75                     «ENDIF»
76                 «ENDFOR»
77             }
78         «ENDFOR»
79     '''
80
81     def typeBuilder() {
82         val outerCls = getOuterClassName(type);
83         if(outerCls !== null) {
84             return outerCls + type.name + "Builder"
85         }
86         return type.name + "Builder"
87     }
88
89     private def unionConstructorsParentProperties() '''
90         «FOR property : parentProperties SEPARATOR "\n"»
91             public «type.name»(«property.returnType.importedName» «property.fieldName») {
92                 super(«property.fieldName»);
93             }
94         «ENDFOR»
95     '''
96
97     def generateStringValue()
98     '''
99         /**
100          * Return a String representing the value of this union.
101          *
102          * @return String representation of this union's value.
103          */
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)»
110                     ««« type string
111                 return «field»;
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)»
116                     ««« type binary
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»
123                     ««« union type
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());
136                 «ELSE»
137                     ««« generated type
138                 return «field».getValue().toString();
139                 «ENDIF»
140             }
141             «ENDFOR»
142
143             throw new IllegalStateException("No value assinged");
144         }
145     '''
146
147     private static def Type typedefReturnType(Type type) {
148         if (!(type instanceof GeneratedTransferObject)) {
149             return null
150         }
151         val gto = type as GeneratedTransferObject
152         if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
153             return null
154         }
155         val prop = gto.properties.get(0)
156         if (prop.name.equals("value")) {
157             return prop.returnType
158         }
159         return null
160     }
161
162     override protected copyConstructor() '''
163         /**
164          * Creates a copy from Source Object.
165          *
166          * @param source Source object
167          */
168         public «type.name»(«type.name» source) {
169             «IF !parentProperties.empty»
170                 super(source);
171             «ENDIF»
172             «FOR p : properties»
173                 «IF p.returnType.importedName.contains("[]")»
174                 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
175                 «ELSE»
176                 this.«p.fieldName» = source.«p.fieldName»;
177                 «ENDIF»
178             «ENDFOR»
179         }
180     '''
181
182 }