Eliminate Union char[] constructor
[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.getOuterClassName;
12
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
18
19 /**
20  * Template for generating JAVA class.
21  */
22 class UnionTemplate extends ClassTemplate {
23
24     /**
25      * Creates instance of this class with concrete <code>genType</code>.
26      *
27      * @param genType generated transfer object which will be transformed to JAVA class source code
28      */
29     new(NestedJavaGeneratedType javaType, GeneratedTransferObject genType) {
30         super(javaType, genType)
31     }
32
33     /**
34      * Creates instance of this class with concrete <code>genType</code>.
35      *
36      * @param genType generated transfer object which will be transformed to JAVA class source code
37      */
38     new(GeneratedTransferObject genType) {
39         super(genType)
40     }
41
42     override constructors() '''
43         «unionConstructorsParentProperties»
44         «unionConstructors»
45         «IF !allProperties.empty»
46             «copyConstructor»
47         «ENDIF»
48         «IF properties.empty && !parentProperties.empty»
49             «parentConstructor»
50         «ENDIF»
51
52         «generateStringValue»
53     '''
54
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)»
61             «ENDIF»
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)»
67                 «ENDIF»
68                 this.«property.fieldName» = «property.fieldName»;
69                 «FOR other : finalProperties»
70                     «IF property != other»
71                          this.«other.fieldName» = null;
72                     «ENDIF»
73                 «ENDFOR»
74             }
75         «ENDFOR»
76     '''
77
78     def typeBuilder() {
79         val outerCls = getOuterClassName(type);
80         if(outerCls !== null) {
81             return outerCls + type.name + "Builder"
82         }
83         return type.name + "Builder"
84     }
85
86     private def unionConstructorsParentProperties() '''
87         «FOR property : parentProperties SEPARATOR "\n"»
88             public «type.name»(«property.returnType.importedName» «property.fieldName») {
89                 super(«property.fieldName»);
90             }
91         «ENDFOR»
92     '''
93
94     def generateStringValue()
95     '''
96         /**
97          * Return a String representing the value of this union.
98          *
99          * @return String representation of this union's value.
100          */
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)»
107                     ««« type string
108                 return «field»;
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)»
113                     ««« type binary
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»
121                     ««« union type
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());
146                 «ELSE»
147                     ««« generated type
148                 return «field».getValue().toString();
149                 «ENDIF»
150             }
151             «ENDFOR»
152
153             throw new IllegalStateException("No value assinged");
154         }
155     '''
156
157     override protected copyConstructor() '''
158         /**
159          * Creates a copy from Source Object.
160          *
161          * @param source Source object
162          */
163         public «type.name»(«type.name» source) {
164             «IF !parentProperties.empty»
165                 super(source);
166             «ENDIF»
167             «FOR p : properties»
168                 «IF p.returnType.importedName.contains("[]")»
169                 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
170                 «ELSE»
171                 this.«p.fieldName» = source.«p.fieldName»;
172                 «ENDIF»
173             «ENDFOR»
174         }
175     '''
176
177 }