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