Simplify UnionTemplate stringValue() dispatch
[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.getOuterClassName;
13
14 import java.util.Arrays
15 import java.util.Base64;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
17 import org.opendaylight.mdsal.binding.model.api.Enumeration
18 import org.opendaylight.mdsal.binding.model.api.Type
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
20
21 /**
22  * Template for generating JAVA class.
23  */
24 class UnionTemplate extends ClassTemplate {
25
26     /**
27      * Creates instance of this class with concrete <code>genType</code>.
28      *
29      * @param genType generated transfer object which will be transformed to JAVA class source code
30      */
31     new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
32         super(javaType, genType)
33     }
34
35     /**
36      * Creates instance of this class with concrete <code>genType</code>.
37      *
38      * @param genType generated transfer object which will be transformed to JAVA class source code
39      */
40     new(GeneratedTransferObject genType) {
41         super(genType)
42     }
43
44     override constructors() '''
45         «unionConstructorsParentProperties»
46         «unionConstructors»
47         «IF !allProperties.empty»
48             «copyConstructor»
49         «ENDIF»
50         «IF properties.empty && !parentProperties.empty»
51             «parentConstructor»
52         «ENDIF»
53
54         «generateStringValue»
55     '''
56
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)»
63             «ENDIF»
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)»
69                 «ENDIF»
70                 this.«property.fieldName» = «property.fieldName»;
71                 «FOR other : finalProperties»
72                     «IF property != other»
73                          this.«other.fieldName» = null;
74                     «ENDIF»
75                 «ENDFOR»
76             }
77         «ENDFOR»
78     '''
79
80     def typeBuilder() {
81         val outerCls = getOuterClassName(type);
82         if(outerCls !== null) {
83             return outerCls + type.name + "Builder"
84         }
85         return type.name + "Builder"
86     }
87
88     private def unionConstructorsParentProperties() '''
89         «FOR property : parentProperties SEPARATOR "\n"»
90             public «type.name»(«property.returnType.importedName» «property.fieldName») {
91                 super(«property.fieldName»);
92             }
93         «ENDFOR»
94     '''
95
96     def generateStringValue()
97     '''
98         /**
99          * Return a String representing the value of this union.
100          *
101          * @return String representation of this union's value.
102          */
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 "java.lang.String".equals(propRet.fullyQualifiedName)»
109                     ««« type string
110                 return «field»;
111                 «ELSEIF "org.opendaylight.yangtools.yang.binding.InstanceIdentifier".equals(propRet.fullyQualifiedName)»
112                     ««« type instance-identifier
113                 return «field».toString();
114                 «ELSEIF "byte[]".equals(propRet.name)»
115                     ««« type binary
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»
122                     ««« union type
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 propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
131                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
132                         && (propRet as GeneratedTransferObject).baseType instanceof BitsTypeDefinition»
133                     ««« generated bits typedef
134                 return «Arrays.importedName».toString(«field».getValue());
135                 «ELSE»
136                     ««« generated type
137                 return «field».getValue().toString();
138                 «ENDIF»
139             }
140             «ENDFOR»
141
142             throw new IllegalStateException("No value assinged");
143         }
144     '''
145
146     private static def Type typedefReturnType(Type type) {
147         if (!(type instanceof GeneratedTransferObject)) {
148             return null
149         }
150         val gto = type as GeneratedTransferObject
151         if (!gto.typedef || gto.properties === null || gto.properties.size != 1) {
152             return null
153         }
154         val prop = gto.properties.get(0)
155         if (prop.name.equals("value")) {
156             return prop.returnType
157         }
158         return null
159     }
160
161     override protected copyConstructor() '''
162         /**
163          * Creates a copy from Source Object.
164          *
165          * @param source Source object
166          */
167         public «type.name»(«type.name» source) {
168             «IF !parentProperties.empty»
169                 super(source);
170             «ENDIF»
171             «FOR p : properties»
172                 «IF p.returnType.importedName.contains("[]")»
173                 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
174                 «ELSE»
175                 this.«p.fieldName» = source.«p.fieldName»;
176                 «ENDIF»
177             «ENDFOR»
178         }
179     '''
180
181 }