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.yangtools.sal.java.api.generator
10 import static org.opendaylight.yangtools.binding.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
12 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
13 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
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)»
62 «String.importedName» name;
64 private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
67 final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> b = com.google.common.collect.ImmutableMap.builder();
68 for («enums.name» enumItem : «enums.name».values())
70 b.put(enumItem.value, enumItem);
73 VALUE_MAP = b.build();
76 private «enums.name»(int value, «String.importedName» name) {
82 * Returns the name of the enumeration item as it is specified in the input yang.
84 * @return the name of the enumeration item as it is specified in the input yang
86 public «String.importedName» getName() {
91 * @return integer value
93 public int getIntValue() {
99 * @return corresponding «enums.name» item
101 public static «enums.name» forValue(int valueArg) {
102 return VALUE_MAP.get(valueArg);
107 def writeEnumeration(Enumeration enumeration)
109 «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
110 «writeEnumItem(v.name, v.mappedName, v.value, v.description)»«