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(AbstractJavaGeneratedType javaType, Enumeration enums) {
30 super(javaType, enums as GeneratedType)
35 * Constructs instance of this class with concrete <code>enums</code>.
37 * @param enums enumeration which will be transformed to JAVA source code
39 new(Enumeration enums) {
40 super(enums as GeneratedType)
45 * Generates only JAVA enumeration source code.
47 * @return string with JAVA enumeration source code
49 def generateAsInnerClass() {
53 def writeEnumItem(String name, String mappedName, int value, String description) '''
54 «asJavadoc(encodeAngleBrackets(description))»
55 «mappedName»(«value», "«name»")
59 * Template method which generates enumeration body (declaration + enumeration items).
61 * @return string with the enumeration body
63 // FIXME: for some reason importedName does not work here :(
65 «wrapToDocumentation(formatDataForJavaDoc(enums))»
66 public enum «enums.name» implements org.opendaylight.yangtools.yang.binding.Enumeration {
67 «writeEnumeration(enums)»
69 private static final java.util.Map<java.lang.String, «enums.name»> NAME_MAP;
70 private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
73 final com.google.common.collect.ImmutableMap.Builder<java.lang.String, «enums.name»> nb =
74 com.google.common.collect.ImmutableMap.builder();
75 final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> vb =
76 com.google.common.collect.ImmutableMap.builder();
77 for («enums.name» enumItem : «enums.name».values()) {
78 vb.put(enumItem.value, enumItem);
79 nb.put(enumItem.name, enumItem);
82 NAME_MAP = nb.build();
83 VALUE_MAP = vb.build();
86 private final java.lang.String name;
87 private final int value;
89 private «enums.name»(int value, «String.importedName» name) {
95 public java.lang.String getName() {
100 public int getIntValue() {
105 * Return the enumeration member whose {@link #getName()} matches specified value.
107 * @param name YANG assigned name
108 * @return corresponding «enums.name» item, if present
109 * @throws NullPointerException if name is null
111 public static java.util.Optional<«enums.name»> forName(«String.importedName» name) {
112 return java.util.Optional.ofNullable(NAME_MAP.get(java.util.Objects.requireNonNull(name)));
116 * Return the enumeration member whose {@link #getIntValue()} matches specified value.
118 * @param intValue integer value
119 * @return corresponding «enums.name» item, or null if no such item exists
121 public static «enums.name» forValue(int intValue) {
122 return VALUE_MAP.get(intValue);
127 def writeEnumeration(Enumeration enumeration)
129 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
130 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«