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) {
29 override constructors() '''
30 «unionConstructorsParentProperties»
32 «IF !allProperties.empty»
35 «IF properties.empty && !parentProperties.empty»
40 private def unionConstructors() '''
41 «FOR property : finalProperties SEPARATOR "\n"»
42 «val propRet = property.returnType»
43 «val isCharArray = "char[]".equals(propRet.name)»
46 * Constructor provided only for using in JMX. Don't use it for
47 * construction new object of this union type.
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 = «typeBuilder()».getDefaultInstance(defVal);
53 «FOR other : finalProperties»
54 «IF other.name.equals("value")»
55 «IF other.returnType.importedName.contains("[]")»
56 this.«other.fieldName» = «other.fieldName» == null ? null : «other.fieldName».clone();
58 this.«other.fieldName» = «other.fieldName»;
61 this.«other.fieldName» = defInst.«other.fieldName»;
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)»
75 this.«other.fieldName» = «property.fieldName».toCharArray();
76 «ELSEIF "byte[]".equals(propRet.name)»
78 this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
79 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang")
80 || propRet instanceof Enumeration
81 || propRet.fullyQualifiedName.startsWith("java.math")»
82 ««« type int*, uint, decimal64 or enumeration*
83 this.«other.fieldName» = «property.fieldName».toString().toCharArray();
84 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
86 this.«other.fieldName» = «property.fieldName».getValue();
87 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
88 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
89 && (propRet as GeneratedTransferObject).properties != null
90 && !(propRet as GeneratedTransferObject).properties.empty
91 && ((propRet as GeneratedTransferObject).properties.size == 1)
92 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
93 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
94 ««« generated boolean typedef
95 this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
98 this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
101 this.«other.fieldName» = null;
111 val outerCls = getOuterClassName(type);
112 if(outerCls !== null) {
113 return outerCls + type.name + "Builder"
115 return type.name + "Builder"
118 private def unionConstructorsParentProperties() '''
119 «FOR property : parentProperties SEPARATOR "\n"»
120 public «type.name»(«property.returnType.importedName» «property.fieldName») {
121 super(«property.fieldName»);
126 override protected copyConstructor() '''
128 * Creates a copy from Source Object.
130 * @param source Source object
132 public «type.name»(«type.name» source) {
133 «IF !parentProperties.empty»
136 «IF !properties.empty»
138 «IF p.returnType.importedName.contains("[]")»
139 this.«p.fieldName» = source.«p.fieldName» == null ? null : source.«p.fieldName».clone();
141 this.«p.fieldName» = source.«p.fieldName»;