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.mdsal.binding.java.api.generator
10 import static extension org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets
11 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
13 import org.opendaylight.mdsal.binding.model.api.Enumeration
14 import org.opendaylight.mdsal.binding.model.api.GeneratedType
15 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
16 import org.opendaylight.yangtools.yang.binding.EnumTypeObject
19 * Template for generating JAVA enumeration type.
21 class EnumTemplate extends BaseTemplate {
22 static val ENUM_TYPE_OBJECT = JavaTypeName.create(EnumTypeObject)
25 * Enumeration which will be transformed to JAVA source code for enumeration
30 * Constructs instance of this class with concrete <code>enums</code>.
32 * @param enums enumeration which will be transformed to JAVA source code
34 new(AbstractJavaGeneratedType javaType, Enumeration enums) {
35 super(javaType, enums as GeneratedType)
40 * Constructs instance of this class with concrete <code>enums</code>.
42 * @param enums enumeration which will be transformed to JAVA source code
44 new(Enumeration enums) {
45 super(enums as GeneratedType)
50 * Generates only JAVA enumeration source code.
52 * @return string with JAVA enumeration source code
54 def generateAsInnerClass() {
58 def writeEnumItem(String name, String mappedName, int value, String description) '''
59 «IF description !== null»
60 «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
62 «mappedName»(«value», "«name»")
66 * Template method which generates enumeration body (declaration + enumeration items).
68 * @return string with the enumeration body
71 «enums.formatDataForJavaDoc.wrapToDocumentation»
73 public enum «enums.name» implements «ENUM_TYPE_OBJECT.importedName» {
74 «writeEnumeration(enums)»
76 private final «STRING.importedNonNull» name;
77 private final int value;
79 private «enums.name»(int value, «STRING.importedNonNull» name) {
84 @«OVERRIDE.importedName»
85 public «STRING.importedNonNull» getName() {
89 @«OVERRIDE.importedName»
90 public int getIntValue() {
95 * Return the enumeration member whose {@link #getName()} matches specified assigned name.
97 * @param name YANG assigned name
98 * @return corresponding «enums.name» item, or {@code null} if no such item exists
99 * @throws «NPE.importedName» if {@code name} is null
101 public static «enums.importedNullable» forName(«STRING.importedName» name) {
102 return switch (name) {
103 «FOR v : enums.values»
104 case "«v.name»" -> «v.mappedName»;
111 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
113 * @param intValue integer value
114 * @return corresponding «enums.name» item, or {@code null} if no such item exists
116 public static «enums.importedNullable» forValue(int intValue) {
117 return switch (intValue) {
118 «FOR v : enums.values»
119 case «v.value» -> «v.mappedName»;
126 * Return the enumeration member whose {@link #getName()} matches specified assigned name.
128 * @param name YANG assigned name
129 * @return corresponding «enums.name» item
130 * @throws «NPE.importedName» if {@code name} is null
131 * @throws «IAE.importedName» if {@code name} does not match any item
133 public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
134 return «CODEHELPERS.importedName».checkEnum(forName(name), name);
138 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
140 * @param intValue integer value
141 * @return corresponding «enums.name» item
142 * @throws «IAE.importedName» if {@code intValue} does not match any item
144 public static «enums.importedNonNull» ofValue(int intValue) {
145 return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
150 def writeEnumeration(Enumeration enumeration)
152 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
153 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«