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 = «type.name»Builder.getDefaultInstance(defVal);
53 «FOR other : finalProperties»
54 «IF other.name.equals("value")»
55 this.«other.fieldName» = «other.fieldName»;
57 this.«other.fieldName» = defInst.«other.fieldName»;
62 «val propertyAndTopParentProperties = parentProperties + #[property]»
63 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
64 super(«parentProperties.asArguments»);
65 this.«property.fieldName» = «property.fieldName»;
66 «FOR other : finalProperties»
67 «IF property != other»
68 «IF "value".equals(other.name)»
69 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
71 this.«other.fieldName» = «property.fieldName».toCharArray();
72 «ELSEIF "byte[]".equals(propRet.name)»
74 this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
75 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
76 ««« type int*, uint or enumeration*
77 this.«other.fieldName» = «property.fieldName».toString().toCharArray();
78 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
80 this.«other.fieldName» = «property.fieldName».getValue();
81 «ELSEIF propRet instanceof GeneratedTransferObject // Is it a GeneratedTransferObject
82 && (propRet as GeneratedTransferObject).typedef // Is it a typedef
83 && (propRet as GeneratedTransferObject).properties != null
84 && !(propRet as GeneratedTransferObject).properties.empty
85 && ((propRet as GeneratedTransferObject).properties.size == 1)
86 && (propRet as GeneratedTransferObject).properties.get(0).name.equals("value")
87 && BOOLEAN.equals((propRet as GeneratedTransferObject).properties.get(0).returnType)» // And the property value is of type boolean
88 ««« generated boolean typedef
89 this.«other.fieldName» = «property.fieldName».isValue().toString().toCharArray();
92 this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
95 this.«other.fieldName» = null;
104 private def unionConstructorsParentProperties() '''
105 «FOR property : parentProperties SEPARATOR "\n"»
106 public «type.name»(«property.returnType.importedName» «property.fieldName») {
107 super(«property.fieldName»);
112 override protected copyConstructor() '''
114 * Creates a copy from Source Object.
116 * @param source Source object
118 public «type.name»(«type.name» source) {
119 «IF !parentProperties.empty»
122 «IF !properties.empty»
124 this.«p.fieldName» = source.«p.fieldName»;