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
15 * Template for generating JAVA class.
17 class UnionTemplate extends ClassTemplate {
20 * Creates instance of this class with concrete <code>genType</code>.
22 * @param genType generated transfer object which will be transformed to JAVA class source code
24 new(GeneratedTransferObject genType) {
28 override constructors() '''
29 «unionConstructorsParentProperties»
31 «IF !allProperties.empty»
34 «IF properties.empty && !parentProperties.empty»
39 private def unionConstructors() '''
40 «FOR property : finalProperties SEPARATOR "\n"»
41 «val propRet = property.returnType»
42 «val isCharArray = "char[]".equals(propRet.name)»
45 * Constructor provided only for using in JMX. Don't use it for
46 * construction new object of this union type.
48 @«ConstructorProperties.importedName»("«property.name»")
49 public «type.name»(«propRet.importedName» «property.fieldName») {
50 «String.importedName» defVal = new «String.importedName»(«property.fieldName»);
51 «type.name» defInst = «type.name»Builder.getDefaultInstance(defVal);
52 «FOR other : finalProperties»
53 «IF other.name.equals("value")»
54 this.«other.fieldName» = «other.fieldName»;
56 this.«other.fieldName» = defInst.«other.fieldName»;
61 «val propertyAndTopParentProperties = parentProperties + #[property]»
62 public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
63 super(«parentProperties.asArguments»);
64 this.«property.fieldName» = «property.fieldName»;
65 «FOR other : finalProperties»
66 «IF property != other»
67 «IF "value".equals(other.name)»
68 «IF "java.lang.String".equals(propRet.fullyQualifiedName)»
70 this.«other.fieldName» = «property.fieldName».toCharArray();
71 «ELSEIF "byte[]".equals(propRet.name)»
73 this.«other.fieldName» = new «String.importedName»(«property.fieldName»).toCharArray();
74 «ELSEIF propRet.fullyQualifiedName.startsWith("java.lang") || propRet instanceof Enumeration»
75 ««« type int*, uint or enumeration*
76 this.«other.fieldName» = «property.fieldName».toString().toCharArray();
77 «ELSEIF propRet instanceof GeneratedTransferObject && (propRet as GeneratedTransferObject).unionType»
79 this.«other.fieldName» = «property.fieldName».getValue();
82 this.«other.fieldName» = «property.fieldName».getValue().toString().toCharArray();
85 this.«other.fieldName» = null;
94 private def unionConstructorsParentProperties() '''
95 «FOR property : parentProperties SEPARATOR "\n"»
96 public «type.name»(«property.returnType.importedName» «property.fieldName») {
97 super(«property.fieldName»);
102 override protected copyConstructor() '''
104 * Creates a copy from Source Object.
106 * @param source Source object
108 public «type.name»(«type.name» source) {
109 «IF !parentProperties.empty»
112 «IF !properties.empty»
114 this.«p.fieldName» = source.«p.fieldName»;