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 org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
12 import com.google.common.collect.ImmutableMap
13 import com.google.common.collect.ImmutableMap.Builder
15 import java.util.Objects
16 import java.util.Optional
17 import org.opendaylight.mdsal.binding.model.api.Enumeration
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType
21 * Template for generating JAVA enumeration type.
23 class EnumTemplate extends BaseTemplate {
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 «asJavadoc(encodeAngleBrackets(description))»
60 «mappedName»(«value», "«name»")
64 * Template method which generates enumeration body (declaration + enumeration items).
66 * @return string with the enumeration body
69 «wrapToDocumentation(formatDataForJavaDoc(enums))»
70 public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
71 «writeEnumeration(enums)»
73 private static final «Map.importedName»<«String.importedName», «enums.name»> NAME_MAP;
74 private static final «Map.importedName»<«Integer.importedName», «enums.name»> VALUE_MAP;
77 final «Builder.importedName»<«String.importedName», «enums.name»> nb = «ImmutableMap.importedName».builder();
78 final «Builder.importedName»<«Integer.importedName», «enums.name»> vb = «ImmutableMap.importedName».builder();
79 for («enums.name» enumItem : «enums.name».values()) {
80 vb.put(enumItem.value, enumItem);
81 nb.put(enumItem.name, enumItem);
84 NAME_MAP = nb.build();
85 VALUE_MAP = vb.build();
88 private final «String.importedName» name;
89 private final int value;
91 private «enums.name»(int value, «String.importedName» name) {
96 @«Override.importedName»
97 public «String.importedName» getName() {
101 @«Override.importedName»
102 public int getIntValue() {
107 * Return the enumeration member whose {@link #getName()} matches specified value.
109 * @param name YANG assigned name
110 * @return corresponding «enums.name» item, if present
111 * @throws NullPointerException if name is null
113 public static «Optional.importedName»<«enums.name»> forName(«String.importedName» name) {
114 return «Optional.importedName».ofNullable(NAME_MAP.get(«Objects.importedName».requireNonNull(name)));
118 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
120 * @param intValue integer value
121 * @return corresponding «enums.name» item, or null if no such item exists
123 public static «enums.name» forValue(int intValue) {
124 return VALUE_MAP.get(intValue);
129 def writeEnumeration(Enumeration enumeration)
131 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
132 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«