Merge "Bug 626 - It should be possible to create java doc"
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / 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.yangtools.sal.java.api.generator
9
10 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
11 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
12
13 /**
14  * Template for generating JAVA enumeration type. 
15  */
16 class EnumTemplate extends BaseTemplate {
17
18     
19     /**
20      * Enumeration which will be transformed to JAVA source code for enumeration
21      */
22     val Enumeration enums
23     
24     /**
25      * Constructs instance of this class with concrete <code>enums</code>.
26      * 
27      * @param enums enumeration which will be transformed to JAVA source code 
28      */
29     new(Enumeration enums) {
30         super(enums as GeneratedType )
31         this.enums = enums
32     }
33     
34     
35     /**
36      * Generates only JAVA enumeration source code.
37      * 
38      * @return string with JAVA enumeration source code
39      */
40     def generateAsInnerClass() {
41         return body
42     }
43     
44     def writeEnumItem(String name, int value, String description) '''
45         «asJavadoc(formatToParagraph(description))»
46         «name»(«value»)
47     '''
48
49     /**
50      * Template method which generates enumeration body (declaration + enumeration items).
51      * 
52      * @return string with the enumeration body 
53      */
54     override body() '''
55         «wrapToDocumentation(formatDataForJavaDoc(enums))»
56         public enum «enums.name» {
57             «writeEnumeration(enums)»
58
59
60             int value;
61             static java.util.Map<java.lang.Integer, «enums.name»> valueMap;
62
63             static {
64                 valueMap = new java.util.HashMap<>();
65                 for («enums.name» enumItem : «enums.name».values())
66                 {
67                     valueMap.put(enumItem.value, enumItem);
68                 }
69             }
70         
71             private «enums.name»(int value) {
72                 this.value = value;
73             }
74             
75             /**
76              * @return integer value
77              */
78             public int getIntValue() {
79                 return value;
80             }
81
82             /**
83              * @param valueArg
84              * @return corresponding «enums.name» item
85              */
86             public static «enums.name» forValue(int valueArg) {
87                 return valueMap.get(valueArg);
88             }
89         }
90     '''
91
92     def writeEnumeration(Enumeration enumeration)
93     '''
94     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
95     «writeEnumItem(v.name, v.value, v.description)»«
96     ENDFOR»
97     '''
98 }