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 org.opendaylight.mdsal.binding.model.api.Enumeration
13 import org.opendaylight.mdsal.binding.model.api.GeneratedType
16 * Template for generating JAVA enumeration type.
18 class EnumTemplate extends BaseTemplate {
20 * Enumeration which will be transformed to JAVA source code for enumeration
25 * Constructs instance of this class with concrete <code>enums</code>.
27 * @param enums enumeration which will be transformed to JAVA source code
29 new(Enumeration enums) {
30 super(enums as GeneratedType)
35 * Generates only JAVA enumeration source code.
37 * @return string with JAVA enumeration source code
39 def generateAsInnerClass() {
43 def writeEnumItem(String name, String mappedName, int value, String description) '''
44 «asJavadoc(encodeAngleBrackets(description))»
45 «mappedName»(«value», "«name»")
49 * Template method which generates enumeration body (declaration + enumeration items).
51 * @return string with the enumeration body
53 // FIXME: for some reason importedName does not work here :(
55 «wrapToDocumentation(formatDataForJavaDoc(enums))»
56 public enum «enums.name» implements org.opendaylight.yangtools.yang.binding.Enumeration {
57 «writeEnumeration(enums)»
59 private static final java.util.Map<java.lang.String, «enums.name»> NAME_MAP;
60 private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
63 final com.google.common.collect.ImmutableMap.Builder<java.lang.String, «enums.name»> nb =
64 com.google.common.collect.ImmutableMap.builder();
65 final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> vb =
66 com.google.common.collect.ImmutableMap.builder();
67 for («enums.name» enumItem : «enums.name».values()) {
68 vb.put(enumItem.value, enumItem);
69 nb.put(enumItem.name, enumItem);
72 NAME_MAP = nb.build();
73 VALUE_MAP = vb.build();
76 private final java.lang.String name;
77 private final int value;
79 private «enums.name»(int value, «String.importedName» name) {
85 public java.lang.String getName() {
90 public int getIntValue() {
95 * Return the enumeration member whose {@link #getName()} matches specified value.
97 * @param name YANG assigned name
98 * @return corresponding «enums.name» item, if present
99 * @throws NullPointerException if name is null
101 public static java.util.Optional<«enums.name»> forName(«String.importedName» name) {
102 return java.util.Optional.ofNullable(NAME_MAP.get(java.util.Objects.requireNonNull(name)));
106 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
108 * @param intValue integer value
109 * @return corresponding «enums.name» item, or null if no such item exists
111 public static «enums.name» forValue(int intValue) {
112 return VALUE_MAP.get(intValue);
117 def writeEnumeration(Enumeration enumeration)
119 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
120 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«