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
17 * Template for generating JAVA enumeration type.
19 class EnumTemplate extends BaseTemplate {
21 * Enumeration which will be transformed to JAVA source code for enumeration
26 * Constructs instance of this class with concrete <code>enums</code>.
28 * @param enums enumeration which will be transformed to JAVA source code
30 new(AbstractJavaGeneratedType javaType, Enumeration enums) {
31 super(javaType, enums as GeneratedType)
36 * Constructs instance of this class with concrete <code>enums</code>.
38 * @param enums enumeration which will be transformed to JAVA source code
40 new(Enumeration enums) {
41 super(enums as GeneratedType)
46 * Generates only JAVA enumeration source code.
48 * @return string with JAVA enumeration source code
50 def generateAsInnerClass() {
54 def writeEnumItem(String name, String mappedName, int value, String description) '''
55 «IF description !== null»
56 «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
58 «mappedName»(«value», "«name»")
62 * Template method which generates enumeration body (declaration + enumeration items).
64 * @return string with the enumeration body
67 «enums.formatDataForJavaDoc.wrapToDocumentation»
69 public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
70 «writeEnumeration(enums)»
72 private final «STRING.importedNonNull» name;
73 private final int value;
75 private «enums.name»(int value, «STRING.importedNonNull» name) {
80 @«OVERRIDE.importedName»
81 public «STRING.importedNonNull» getName() {
85 @«OVERRIDE.importedName»
86 public int getIntValue() {
91 * Return the enumeration member whose {@link #getName()} matches specified assigned name.
93 * @param name YANG assigned name
94 * @return corresponding «enums.name» item, or {@code null} if no such item exists
95 * @throws NullPointerException if {@code name} is null
97 public static «enums.importedNullable» forName(«STRING.importedName» name) {
98 return switch (name) {
99 «FOR v : enums.values»
100 case "«v.name»" -> «v.mappedName»;
107 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
109 * @param intValue integer value
110 * @return corresponding «enums.name» item, or {@code null} if no such item exists
112 public static «enums.importedNullable» forValue(int intValue) {
113 return switch (intValue) {
114 «FOR v : enums.values»
115 case «v.value» -> «v.mappedName»;
122 * Return the enumeration member whose {@link #getName()} matches specified assigned name.
124 * @param name YANG assigned name
125 * @return corresponding «enums.name» item
126 * @throws NullPointerException if {@code name} is null
127 * @throws IllegalArgumentException if {@code name} does not match any item
129 public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
130 return «CODEHELPERS.importedName».checkEnum(forName(name), name);
134 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
136 * @param intValue integer value
137 * @return corresponding «enums.name» item
138 * @throws IllegalArgumentException if {@code intValue} does not match any item
140 public static «enums.importedNonNull» ofValue(int intValue) {
141 return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
146 def writeEnumeration(Enumeration enumeration)
148 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
149 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«