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 com.google.common.collect.ImmutableMap
14 import com.google.common.collect.ImmutableMap.Builder
15 import java.util.Optional
16 import org.opendaylight.mdsal.binding.model.api.Enumeration
17 import org.opendaylight.mdsal.binding.model.api.GeneratedType
20 * Template for generating JAVA enumeration type.
22 class EnumTemplate extends BaseTemplate {
24 * Enumeration which will be transformed to JAVA source code for enumeration
29 * Constructs instance of this class with concrete <code>enums</code>.
31 * @param enums enumeration which will be transformed to JAVA source code
33 new(AbstractJavaGeneratedType javaType, Enumeration enums) {
34 super(javaType, enums as GeneratedType)
39 * Constructs instance of this class with concrete <code>enums</code>.
41 * @param enums enumeration which will be transformed to JAVA source code
43 new(Enumeration enums) {
44 super(enums as GeneratedType)
49 * Generates only JAVA enumeration source code.
51 * @return string with JAVA enumeration source code
53 def generateAsInnerClass() {
57 def writeEnumItem(String name, String mappedName, int value, String description) '''
58 «IF description !== null»
59 «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
61 «mappedName»(«value», "«name»")
65 * Template method which generates enumeration body (declaration + enumeration items).
67 * @return string with the enumeration body
70 «enums.formatDataForJavaDoc.wrapToDocumentation»
72 public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
73 «writeEnumeration(enums)»
75 private static final «JU_MAP.importedName»<«STRING.importedName», «enums.name»> NAME_MAP;
76 private static final «JU_MAP.importedName»<«Integer.importedName», «enums.name»> VALUE_MAP;
79 final «Builder.importedName»<«STRING.importedName», «enums.name»> nb = «ImmutableMap.importedName».builder();
80 final «Builder.importedName»<«Integer.importedName», «enums.name»> vb = «ImmutableMap.importedName».builder();
81 for («enums.name» enumItem : «enums.name».values()) {
82 vb.put(enumItem.value, enumItem);
83 nb.put(enumItem.name, enumItem);
86 NAME_MAP = nb.build();
87 VALUE_MAP = vb.build();
90 private final «STRING.importedName» name;
91 private final int value;
93 private «enums.name»(int value, «STRING.importedName» name) {
98 @«OVERRIDE.importedName»
99 public «STRING.importedName» getName() {
103 @«OVERRIDE.importedName»
104 public int getIntValue() {
109 * Return the enumeration member whose {@link #getName()} matches specified value.
111 * @param name YANG assigned name
112 * @return corresponding «enums.name» item, if present
113 * @throws NullPointerException if name is null
115 public static «Optional.importedName»<«enums.name»> forName(«STRING.importedName» name) {
116 return «Optional.importedName».ofNullable(NAME_MAP.get(«JU_OBJECTS.importedName».requireNonNull(name)));
120 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
122 * @param intValue integer value
123 * @return corresponding «enums.name» item, or null if no such item exists
125 public static «enums.name» forValue(int intValue) {
126 return VALUE_MAP.get(intValue);
131 def writeEnumeration(Enumeration enumeration)
133 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
134 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«