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
14 import java.util.Objects
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(Enumeration enums) {
34 super(enums as GeneratedType)
39 * Generates only JAVA enumeration source code.
41 * @return string with JAVA enumeration source code
43 def generateAsInnerClass() {
47 def writeEnumItem(String name, String mappedName, int value, String description) '''
48 «asJavadoc(encodeAngleBrackets(description))»
49 «mappedName»(«value», "«name»")
53 * Template method which generates enumeration body (declaration + enumeration items).
55 * @return string with the enumeration body
57 // FIXME: for some reason importedName does not work here :(
59 «wrapToDocumentation(formatDataForJavaDoc(enums))»
60 public enum «enums.name» implements org.opendaylight.yangtools.yang.binding.Enumeration {
61 «writeEnumeration(enums)»
63 private static final java.util.Map<java.lang.String, «enums.name»> NAME_MAP;
64 private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
67 final com.google.common.collect.ImmutableMap.Builder<java.lang.String, «enums.name»> nb =
68 com.google.common.collect.ImmutableMap.builder();
69 final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> vb =
70 com.google.common.collect.ImmutableMap.builder();
71 for («enums.name» enumItem : «enums.name».values()) {
72 vb.put(enumItem.value, enumItem);
73 nb.put(enumItem.name, enumItem);
76 NAME_MAP = nb.build();
77 VALUE_MAP = vb.build();
80 private final java.lang.String name;
81 private final int value;
83 private «enums.name»(int value, «String.importedName» name) {
89 public java.lang.String getName() {
94 public int getIntValue() {
99 * Return the enumeration member whose {@link #getName()} matches specified value.
101 * @param name YANG assigned name
102 * @return corresponding «enums.name» item, if present
103 * @throws NullPointerException if name is null
105 public static java.util.Optional<«enums.name»> forName(«String.importedName» name) {
106 return java.util.Optional.ofNullable(NAME_MAP.get(java.util.Objects.requireNonNull(name)));
110 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
112 * @param intValue integer value
113 * @return corresponding «enums.name» item, or null if no such item exists
115 public static «enums.name» forValue(int intValue) {
116 return VALUE_MAP.get(intValue);
121 def writeEnumeration(Enumeration enumeration)
123 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
124 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«