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