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 org.opendaylight.mdsal.binding.model.api.Enumeration
11 import org.opendaylight.mdsal.binding.model.api.GeneratedType
13 import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
16 * Template for generating JAVA enumeration type.
18 class EnumTemplate extends BaseTemplate {
22 * Enumeration which will be transformed to JAVA source code for enumeration
27 * Constructs instance of this class with concrete <code>enums</code>.
29 * @param enums enumeration which will be transformed to JAVA source code
31 new(Enumeration enums) {
32 super(enums as GeneratedType )
38 * Generates only JAVA enumeration source code.
40 * @return string with JAVA enumeration source code
42 def generateAsInnerClass() {
46 def writeEnumItem(String name, String mappedName, int value, String description) '''
47 «asJavadoc(encodeAngleBrackets(description))»
48 «mappedName»(«value», "«name»")
52 * Template method which generates enumeration body (declaration + enumeration items).
54 * @return string with the enumeration body
57 «wrapToDocumentation(formatDataForJavaDoc(enums))»
58 public enum «enums.name» {
59 «writeEnumeration(enums)»
61 private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
64 final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> b = com.google.common.collect.ImmutableMap.builder();
65 for («enums.name» enumItem : «enums.name».values()) {
66 b.put(enumItem.value, enumItem);
69 VALUE_MAP = b.build();
72 private final «String.importedName» name;
73 private final int value;
75 private «enums.name»(int value, «String.importedName» name) {
81 * Returns the name of the enumeration item as it is specified in the input yang.
83 * @return the name of the enumeration item as it is specified in the input yang
85 public «String.importedName» getName() {
90 * @return integer value
92 public int getIntValue() {
97 * @param valueArg integer value
98 * @return corresponding «enums.name» item
100 public static «enums.name» forValue(int valueArg) {
101 return VALUE_MAP.get(valueArg);
106 def writeEnumeration(Enumeration enumeration)
108 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
109 «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«