f9c27fe86529d2f8175dc2c1f45805b986f881b2
[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
13 /**
14  * Template for generating JAVA class. 
15  */
16 class UnionTemplate extends ClassTemplate {
17
18     /**
19      * Creates instance of this class with concrete <code>genType</code>.
20      * 
21      * @param genType generated transfer object which will be transformed to JAVA class source code
22      */
23     new(GeneratedTransferObject genType) {
24         super(genType)
25     }
26
27     override constructors() '''
28         «unionConstructorsParentProperties»
29         «unionConstructors»
30         «IF !allProperties.empty»
31             «copyConstructor»
32         «ENDIF»
33         «IF properties.empty && !parentProperties.empty»
34             «parentConstructor»
35         «ENDIF»
36     '''
37
38     private def unionConstructors() '''
39         «FOR property : finalProperties SEPARATOR "\n"»
40             «val isCharArray = "char[]".equals(property.returnType.name)»
41             «IF isCharArray»
42                 /**
43                  * Constructor provided only for using in JMX. Don't use it for
44                  * construction new object of this union type. 
45                  */
46                 @«ConstructorProperties.importedName»("«property.name»")
47                 public «type.name»(«property.returnType.importedName» «property.fieldName») {
48                     «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
49                     «type.name» defInst = «type.name»Builder.getDefaultInstance(defVal);
50                     «FOR other : finalProperties»
51                         «IF other.name.equals("value")»
52                             this.«other.fieldName» = «other.fieldName»;
53                         «ELSE»
54                             this.«other.fieldName» = defInst.«other.fieldName»;
55                         «ENDIF»
56                     «ENDFOR»
57                 }
58             «ELSE»
59                 «val propertyAndTopParentProperties = parentProperties + #[property]»
60                 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
61                     super(«parentProperties.asArguments»);
62                     this.«property.fieldName» = «property.fieldName»;
63                     «FOR other : finalProperties»
64                         «IF property != other»this.«other.fieldName» = null;«ENDIF»
65                     «ENDFOR»
66                 }
67             «ENDIF»
68         «ENDFOR»
69     '''
70
71     private def unionConstructorsParentProperties() '''
72         «FOR property : parentProperties SEPARATOR "\n"»
73             public «type.name»(«property.returnType.importedName» «property.fieldName») {
74                 super(«property.fieldName»);
75             }
76         «ENDFOR»
77     '''
78
79     override protected copyConstructor() '''
80         /**
81          * Creates a copy from Source Object.
82          *
83          * @param source Source object
84          */
85         public «type.name»(«type.name» source) {
86             «IF !parentProperties.empty»
87                 super(source);
88             «ENDIF»
89             «IF !properties.empty»
90                 «FOR p : properties»
91                     this.«p.fieldName» = source.«p.fieldName»;
92                 «ENDFOR»
93             «ENDIF»
94         }
95     '''
96
97 }