2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.java.api.generator
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.*
16 * Template for generating JAVA class.
18 class UnionTemplate extends ClassTemplate {
21 * Creates instance of this class with concrete <code>genType</code>.
23 * @param genType generated transfer object which will be transformed to JAVA class source code
25 new(GeneratedTransferObject genType) {
27 if(isBaseEncodingImportRequired) {
28 this.importMap.put("BaseEncoding","com.google.common.io")
32 final private def boolean isBaseEncodingImportRequired() {
33 for (property : finalProperties) {
34 val propRet = property.returnType
35 if (propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).typedef &&
36 (propRet as GeneratedTransferObject).properties != null &&
37 !(propRet as GeneratedTransferObject).properties.empty &&
38 ((propRet as GeneratedTransferObject).properties.size == 1) &&
39 (propRet as GeneratedTransferObject).properties.get(0).name.equals("value") &&
40 "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)) {
46 override constructors() '''
47 «unionConstructorsParentProperties»
49 «IF !allProperties.empty»
52 «IF properties.empty && !parentProperties.empty»
57 private def unionConstructors() '''
58 «FOR property : finalProperties SEPARATOR "\n"»
59 «val propRet = property.returnType»
60 «val isCharArray = "char[]".equals(propRet.name)»
63 * Constructor provided only for using in JMX. Don't use it for
64 * construction new object of this union type.
66 @«ConstructorProperties.importedName»("«property.name»")
67 public «type.name»(«propRet.importedName» «property.fieldName») {
68 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
69 «type.name» defInst = «typeBuilder()».getDefaultInstance(defVal);
70 «FOR other : finalProperties»
71 «IF other.name.equals("value")»
72 «IF other.returnType.importedName.contains("[]")»
73 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
75 this.«other.fieldName» = «other.fieldName»;
78 this.«other.fieldName» = defInst.«other.fieldName»;
83 «val propertyAndTopParentProperties = parentProperties + #[property]»
84 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
85 super(«parentProperties.asArguments»);
86 this.«property.fieldName» = «property.fieldName»;
87 «FOR other : finalProperties»
88 «IF property != other»
89 «IF "value".equals(other.name)»
90 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
92 this.«other.fieldName» = «property.fieldName».toCharArray();
93 «ELSEIF "byte[]".equals(propRet.name)»
95 this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
96 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
97 || propRet instanceof Enumeration
98 || propRet.fullyQualifiedName.startsWith("java.math")»
99 ««« type int*, uint, decimal64 or enumeration*
100 this.«other.fieldName» = «property.fieldName».toString().toCharArray();
101 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
103 this.«other.fieldName» = «property.fieldName».getValue();
104 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
105 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
106 && (propRet as GeneratedTransferObject).properties != null
107 && !(propRet as GeneratedTransferObject).properties.empty
108 && ((propRet as GeneratedTransferObject).properties.size == 1)
109 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
110 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
111 ««« generated boolean typedef
112 this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
113 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
114 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
115 && (propRet as GeneratedTransferObject).properties != null
116 && !(propRet as GeneratedTransferObject).properties.empty
117 && ((propRet as GeneratedTransferObject).properties.size == 1)
118 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
119 && "byte[]".equals((propRet as GeneratedTransferObject).properties.get(0).returnType.name)»
120 ««« generated byte[] typedef
121 this.«other.fieldName» = BaseEncoding.base64().encode(«property.fieldName».getValue()).toCharArray();
124 this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
127 this.«other.fieldName» = null;
137 val outerCls = getOuterClassName(type);
138 if(outerCls !== null) {
139 return outerCls + type.name + "Builder"
141 return type.name + "Builder"
144 private def unionConstructorsParentProperties() '''
145 «FOR property : parentProperties SEPARATOR "\n"»
146 public «type.name»(«property.returnType.importedName» «property.fieldName») {
147 super(«property.fieldName»);
152 override protected copyConstructor() '''
154 * Creates a copy from Source Object.
156 * @param source Source object
158 public «type.name»(«type.name» source) {
159 «IF !parentProperties.empty»
162 «IF !properties.empty»
164 «IF p.returnType.importedName.contains("[]")»
165 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
167 this.«p.fieldName» = source.«p.fieldName»;