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