Bug 1434 - Make arrays non-mutable
[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                             «IF other.returnType.importedName.contains("[]")»
56                             this.«other.fieldName» = Arrays.copyOf(«other.fieldName», «other.fieldName».length);
57                             «ELSE»
58                             this.«other.fieldName» = «other.fieldName»;
59                             «ENDIF»
60                         «ELSE»
61                             this.«other.fieldName» = defInst.«other.fieldName»;
62                         «ENDIF»
63                     «ENDFOR»
64                 }
65             «ELSE»
66                 «val propertyAndTopParentProperties = parentProperties + #[property]»
67                 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
68                     super(«parentProperties.asArguments»);
69                     this.«property.fieldName» = «property.fieldName»;
70                     «FOR other : finalProperties»
71                         «IF property != other»
72                             «IF "value".equals(other.name)»
73                                 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
74                                     ««« type string
75                                     this.«other.fieldName» = «property.fieldName».toCharArray();
76                                 «ELSEIF "byte[]".equals(propRet.name)»
77                                     ««« type binary
78                                     this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
79                                 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
80                                     ««« type int*, uint or enumeration*
81                                     this.«other.fieldName» = «property.fieldName».toString().toCharArray();
82                                 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
83                                     ««« union type
84                                     this.«other.fieldName» = «property.fieldName».getValue();
85                                 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
86                                         && (propRet as GeneratedTransferObject).typedef  // Is it a typedef
87                                         && (propRet as GeneratedTransferObject).properties != null 
88                                         && !(propRet as GeneratedTransferObject).properties.empty 
89                                         && ((propRet as GeneratedTransferObject).properties.size == 1) 
90                                         && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") 
91                                         && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
92                                     ««« generated boolean typedef
93                                     this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
94                                 «ELSE»
95                                     ««« generated type
96                                     this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
97                                 «ENDIF»
98                             «ELSE»
99                                 this.«other.fieldName» = null;
100                             «ENDIF»
101                         «ENDIF»
102                     «ENDFOR»
103                 }
104             «ENDIF»
105         «ENDFOR»
106     '''
107
108     private def unionConstructorsParentProperties() '''
109         «FOR property : parentProperties SEPARATOR "\n"»
110             public «type.name»(«property.returnType.importedName» «property.fieldName») {
111                 super(«property.fieldName»);
112             }
113         «ENDFOR»
114     '''
115
116     override protected copyConstructor() '''
117         /**
118          * Creates a copy from Source Object.
119          *
120          * @param source Source object
121          */
122         public «type.name»(«type.name» source) {
123             «IF !parentProperties.empty»
124                 super(source);
125             «ENDIF»
126             «IF !properties.empty»
127                 «FOR p : properties»
128                     «IF p.returnType.importedName.contains("[]")»
129                     this.«p.fieldName» = Arrays.copyOf(source.«p.fieldName», source.«p.fieldName».length);
130                     «ELSE»
131                     this.«p.fieldName» = source.«p.fieldName»;
132                     «ENDIF»
133                 «ENDFOR»
134             «ENDIF»
135         }
136     '''
137
138 }