Bug 6859 #4 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / EnumTemplate.xtend
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator
9
10 import static org.opendaylight.mdsal.binding.generator.util.BindingGeneratorUtil.encodeAngleBrackets;
11
12 import org.opendaylight.mdsal.binding.model.api.Enumeration
13 import org.opendaylight.mdsal.binding.model.api.GeneratedType
14
15 /**
16  * Template for generating JAVA enumeration type.
17  */
18 class EnumTemplate extends BaseTemplate {
19
20
21     /**
22      * Enumeration which will be transformed to JAVA source code for enumeration
23      */
24     val Enumeration enums
25
26     /**
27      * Constructs instance of this class with concrete <code>enums</code>.
28      *
29      * @param enums enumeration which will be transformed to JAVA source code
30      */
31     new(Enumeration enums) {
32         super(enums as GeneratedType )
33         this.enums = enums
34     }
35
36
37     /**
38      * Generates only JAVA enumeration source code.
39      *
40      * @return string with JAVA enumeration source code
41      */
42     def generateAsInnerClass() {
43         return body
44     }
45
46     def writeEnumItem(String name, String mappedName, int value, String description) '''
47         «asJavadoc(encodeAngleBrackets(description))»
48         «mappedName»(«value», "«name»")
49     '''
50
51     /**
52      * Template method which generates enumeration body (declaration + enumeration items).
53      *
54      * @return string with the enumeration body
55      */
56     override body() '''
57         «wrapToDocumentation(formatDataForJavaDoc(enums))»
58         public enum «enums.name» {
59             «writeEnumeration(enums)»
60
61
62             «String.importedName» name;
63             int value;
64             private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
65
66             static {
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())
69                 {
70                     b.put(enumItem.value, enumItem);
71                 }
72
73                 VALUE_MAP = b.build();
74             }
75
76             private «enums.name»(int value, «String.importedName» name) {
77                 this.value = value;
78                 this.name = name;
79             }
80
81             /**
82              * Returns the name of the enumeration item as it is specified in the input yang.
83              *
84              * @return the name of the enumeration item as it is specified in the input yang
85              */
86             public «String.importedName» getName() {
87                 return name;
88             }
89
90             /**
91              * @return integer value
92              */
93             public int getIntValue() {
94                 return value;
95             }
96
97             /**
98              * @param valueArg
99              * @return corresponding «enums.name» item
100              */
101             public static «enums.name» forValue(int valueArg) {
102                 return VALUE_MAP.get(valueArg);
103             }
104         }
105     '''
106
107     def writeEnumeration(Enumeration enumeration)
108     '''
109     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
110     «writeEnumItem(v.name, v.mappedName, v.value, v.description)»«
111     ENDFOR»
112     '''
113 }