7316fa055536ca2d043c6f6d9081ec13629e31df
[mdsal.git] / binding / mdsal-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(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             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                 {
67                     b.put(enumItem.value, enumItem);
68                 }
69
70                 VALUE_MAP = b.build();
71             }
72
73             private «enums.name»(int value) {
74                 this.value = value;
75             }
76
77             /**
78              * @return integer value
79              */
80             public int getIntValue() {
81                 return value;
82             }
83
84             /**
85              * @param valueArg
86              * @return corresponding «enums.name» item
87              */
88             public static «enums.name» forValue(int valueArg) {
89                 return VALUE_MAP.get(valueArg);
90             }
91         }
92     '''
93
94     def writeEnumeration(Enumeration enumeration)
95     '''
96     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
97     «writeEnumItem(v.name, v.value, v.description)»«
98     ENDFOR»
99     '''
100 }