Merge "BUG-1275: teach NormalizedNode builders about size hints"
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / 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.yangtools.sal.java.api.generator
9
10 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
11 import java.beans.ConstructorProperties
12 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
13 import static org.opendaylight.yangtools.binding.generator.util.Types.*
14
15 /**
16  * Template for generating JAVA class. 
17  */
18 class UnionTemplate extends ClassTemplate {
19
20     /**
21      * Creates instance of this class with concrete <code>genType</code>.
22      * 
23      * @param genType generated transfer object which will be transformed to JAVA class source code
24      */
25     new(GeneratedTransferObject genType) {
26         super(genType)
27     }
28
29     override constructors() '''
30         «unionConstructorsParentProperties»
31         «unionConstructors»
32         «IF !allProperties.empty»
33             «copyConstructor»
34         «ENDIF»
35         «IF properties.empty && !parentProperties.empty»
36             «parentConstructor»
37         «ENDIF»
38     '''
39
40     private def unionConstructors() '''
41         «FOR property : finalProperties SEPARATOR "\n"»
42             «val propRet = property.returnType»
43             «val isCharArray = "char[]".equals(propRet.name)»
44             «IF isCharArray»
45                 /**
46                  * Constructor provided only for using in JMX. Don't use it for
47                  * construction new object of this union type. 
48                  */
49                 @«ConstructorProperties.importedName»("«property.name»")
50                 public «type.name»(«propRet.importedName» «property.fieldName») {
51                     «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
52                     «type.name» defInst = «type.name»Builder.getDefaultInstance(defVal);
53                     «FOR other : finalProperties»
54                         «IF other.name.equals("value")»
55                             this.«other.fieldName» = «other.fieldName»;
56                         «ELSE»
57                             this.«other.fieldName» = defInst.«other.fieldName»;
58                         «ENDIF»
59                     «ENDFOR»
60                 }
61             «ELSE»
62                 «val propertyAndTopParentProperties = parentProperties + #[property]»
63                 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
64                     super(«parentProperties.asArguments»);
65                     this.«property.fieldName» = «property.fieldName»;
66                     «FOR other : finalProperties»
67                         «IF property != other»
68                             «IF "value".equals(other.name)»
69                                 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
70                                     ««« type string
71                                     this.«other.fieldName» = «property.fieldName».toCharArray();
72                                 «ELSEIF "byte[]".equals(propRet.name)»
73                                     ««« type binary
74                                     this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
75                                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
76                                     ««« type int*, uint or enumeration*
77                                     this.«other.fieldName» = «property.fieldName».toString().toCharArray();
78                                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
79                                     ««« union type
80                                     this.«other.fieldName» = «property.fieldName».getValue();
81                                 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
82                                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
83                                         && (propRet as GeneratedTransferObject).properties != null 
84                                         && !(propRet as GeneratedTransferObject).properties.empty 
85                                         && ((propRet as GeneratedTransferObject).properties.size == 1) 
86                                         && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") 
87                                         && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
88                                     ««« generated boolean typedef
89                                     this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
90                                 «ELSE»
91                                     ««« generated type
92                                     this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
93                                 «ENDIF»
94                             «ELSE»
95                                 this.«other.fieldName» = null;
96                             «ENDIF»
97                         «ENDIF»
98                     «ENDFOR»
99                 }
100             «ENDIF»
101         «ENDFOR»
102     '''
103
104     private def unionConstructorsParentProperties() '''
105         «FOR property : parentProperties SEPARATOR "\n"»
106             public «type.name»(«property.returnType.importedName» «property.fieldName») {
107                 super(«property.fieldName»);
108             }
109         «ENDFOR»
110     '''
111
112     override protected copyConstructor() '''
113         /**
114          * Creates a copy from Source Object.
115          *
116          * @param source Source object
117          */
118         public «type.name»(«type.name» source) {
119             «IF !parentProperties.empty»
120                 super(source);
121             «ENDIF»
122             «IF !properties.empty»
123                 «FOR p : properties»
124                     this.«p.fieldName» = source.«p.fieldName»;
125                 «ENDFOR»
126             «ENDIF»
127         }
128     '''
129
130 }