fc6e21238e1ea70b794b9e01e0182b9911480d01
[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 org.opendaylight.mdsal.binding.model.api.Enumeration
11 import org.opendaylight.mdsal.binding.model.api.GeneratedType
12
13 import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
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             private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
62
63             static {
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);
67                 }
68
69                 VALUE_MAP = b.build();
70             }
71
72             private final «String.importedName» name;
73             private final int value;
74
75             private «enums.name»(int value, «String.importedName» name) {
76                 this.value = value;
77                 this.name = name;
78             }
79
80             /**
81              * Returns the name of the enumeration item as it is specified in the input yang.
82              *
83              * @return the name of the enumeration item as it is specified in the input yang
84              */
85             public «String.importedName» getName() {
86                 return name;
87             }
88
89             /**
90              * @return integer value
91              */
92             public int getIntValue() {
93                 return value;
94             }
95
96             /**
97              * @param valueArg integer value
98              * @return corresponding «enums.name» item
99              */
100             public static «enums.name» forValue(int valueArg) {
101                 return VALUE_MAP.get(valueArg);
102             }
103         }
104     '''
105
106     def writeEnumeration(Enumeration enumeration)
107     '''
108     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
109     «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«
110     ENDFOR»
111     '''
112 }