ca45d66ff4d51d398656b020c9d5bef19af23248
[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.model.util.BindingGeneratorUtil.encodeAngleBrackets
11
12 import com.google.common.collect.ImmutableMap
13 import com.google.common.collect.ImmutableMap.Builder
14 import java.util.Map
15 import java.util.Objects
16 import java.util.Optional
17 import org.opendaylight.mdsal.binding.model.api.Enumeration
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType
19
20 /**
21  * Template for generating JAVA enumeration type.
22  */
23 class EnumTemplate extends BaseTemplate {
24     /**
25      * Enumeration which will be transformed to JAVA source code for enumeration
26      */
27     val Enumeration enums
28
29     /**
30      * Constructs instance of this class with concrete <code>enums</code>.
31      *
32      * @param enums enumeration which will be transformed to JAVA source code
33      */
34     new(AbstractJavaGeneratedType javaType, Enumeration enums) {
35         super(javaType, enums as GeneratedType)
36         this.enums = enums
37     }
38
39     /**
40      * Constructs instance of this class with concrete <code>enums</code>.
41      *
42      * @param enums enumeration which will be transformed to JAVA source code
43      */
44     new(Enumeration enums) {
45         super(enums as GeneratedType)
46         this.enums = enums
47     }
48
49     /**
50      * Generates only JAVA enumeration source code.
51      *
52      * @return string with JAVA enumeration source code
53      */
54     def generateAsInnerClass() {
55         return body
56     }
57
58     def writeEnumItem(String name, String mappedName, int value, String description) '''
59         «asJavadoc(encodeAngleBrackets(description))»
60         «mappedName»(«value», "«name»")
61     '''
62
63     /**
64      * Template method which generates enumeration body (declaration + enumeration items).
65      *
66      * @return string with the enumeration body
67      */
68     override body() '''
69         «wrapToDocumentation(formatDataForJavaDoc(enums))»
70         public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
71             «writeEnumeration(enums)»
72
73             private static final «Map.importedName»<«String.importedName», «enums.name»> NAME_MAP;
74             private static final «Map.importedName»<«Integer.importedName», «enums.name»> VALUE_MAP;
75
76             static {
77                 final «Builder.importedName»<«String.importedName», «enums.name»> nb = «ImmutableMap.importedName».builder();
78                 final «Builder.importedName»<«Integer.importedName», «enums.name»> vb = «ImmutableMap.importedName».builder();
79                 for («enums.name» enumItem : «enums.name».values()) {
80                     vb.put(enumItem.value, enumItem);
81                     nb.put(enumItem.name, enumItem);
82                 }
83
84                 NAME_MAP = nb.build();
85                 VALUE_MAP = vb.build();
86             }
87
88             private final «String.importedName» name;
89             private final int value;
90
91             private «enums.name»(int value, «String.importedName» name) {
92                 this.value = value;
93                 this.name = name;
94             }
95
96             @«OVERRIDE.importedName»
97             public «String.importedName» getName() {
98                 return name;
99             }
100
101             @«OVERRIDE.importedName»
102             public int getIntValue() {
103                 return value;
104             }
105
106             /**
107              * Return the enumeration member whose {@link #getName()} matches specified value.
108              *
109              * @param name YANG assigned name
110              * @return corresponding «enums.name» item, if present
111              * @throws NullPointerException if name is null
112              */
113             public static «Optional.importedName»<«enums.name»> forName(«String.importedName» name) {
114                 return «Optional.importedName».ofNullable(NAME_MAP.get(«Objects.importedName».requireNonNull(name)));
115             }
116
117             /**
118              * Return the enumeration member whose {@link #getIntValue()} matches specified value.
119              *
120              * @param intValue integer value
121              * @return corresponding «enums.name» item, or null if no such item exists
122              */
123             public static «enums.name» forValue(int intValue) {
124                 return VALUE_MAP.get(intValue);
125             }
126         }
127     '''
128
129     def writeEnumeration(Enumeration enumeration)
130     '''
131     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
132     «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«
133     ENDFOR»
134     '''
135 }