89239186786e3216f6a80289d0ac33c7c0e6edd4
[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 extension org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets
11 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
12
13 import org.opendaylight.mdsal.binding.model.api.Enumeration
14 import org.opendaylight.mdsal.binding.model.api.GeneratedType
15
16 /**
17  * Template for generating JAVA enumeration type.
18  */
19 class EnumTemplate extends BaseTemplate {
20     /**
21      * Enumeration which will be transformed to JAVA source code for enumeration
22      */
23     val Enumeration enums
24
25     /**
26      * Constructs instance of this class with concrete <code>enums</code>.
27      *
28      * @param enums enumeration which will be transformed to JAVA source code
29      */
30     new(AbstractJavaGeneratedType javaType, Enumeration enums) {
31         super(javaType, enums as GeneratedType)
32         this.enums = enums
33     }
34
35     /**
36      * Constructs instance of this class with concrete <code>enums</code>.
37      *
38      * @param enums enumeration which will be transformed to JAVA source code
39      */
40     new(Enumeration enums) {
41         super(enums as GeneratedType)
42         this.enums = enums
43     }
44
45     /**
46      * Generates only JAVA enumeration source code.
47      *
48      * @return string with JAVA enumeration source code
49      */
50     def generateAsInnerClass() {
51         return body
52     }
53
54     def writeEnumItem(String name, String mappedName, int value, String description) '''
55         «IF description !== null»
56             «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
57         «ENDIF»
58         «mappedName»(«value», "«name»")
59     '''
60
61     /**
62      * Template method which generates enumeration body (declaration + enumeration items).
63      *
64      * @return string with the enumeration body
65      */
66     override body() '''
67         «enums.formatDataForJavaDoc.wrapToDocumentation»
68         «generatedAnnotation»
69         public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
70             «writeEnumeration(enums)»
71
72             private final «STRING.importedNonNull» name;
73             private final int value;
74
75             private «enums.name»(int value, «STRING.importedNonNull» name) {
76                 this.value = value;
77                 this.name = name;
78             }
79
80             @«OVERRIDE.importedName»
81             public «STRING.importedNonNull» getName() {
82                 return name;
83             }
84
85             @«OVERRIDE.importedName»
86             public int getIntValue() {
87                 return value;
88             }
89
90             /**
91              * Return the enumeration member whose {@link #getName()} matches specified assigned name.
92              *
93              * @param name YANG assigned name
94              * @return corresponding «enums.name» item, or {@code null} if no such item exists
95              * @throws NullPointerException if {@code name} is null
96              */
97             public static «enums.importedNullable» forName(«STRING.importedName» name) {
98                 return switch (name) {
99                     «FOR v : enums.values»
100                     case "«v.name»" -> «v.mappedName»;
101                     «ENDFOR»
102                     default -> null;
103                 };
104             }
105
106             /**
107              * Return the enumeration member whose {@link #getIntValue()} matches specified value.
108              *
109              * @param intValue integer value
110              * @return corresponding «enums.name» item, or {@code null} if no such item exists
111              */
112             public static «enums.importedNullable» forValue(int intValue) {
113                 return switch (intValue) {
114                     «FOR v : enums.values»
115                     case «v.value» -> «v.mappedName»;
116                     «ENDFOR»
117                     default -> null;
118                 };
119             }
120
121             /**
122              * Return the enumeration member whose {@link #getName()} matches specified assigned name.
123              *
124              * @param name YANG assigned name
125              * @return corresponding «enums.name» item
126              * @throws NullPointerException if {@code name} is null
127              * @throws IllegalArgumentException if {@code name} does not match any item
128              */
129             public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
130                 return «CODEHELPERS.importedName».checkEnum(forName(name), name);
131             }
132
133             /**
134              * Return the enumeration member whose {@link #getIntValue()} matches specified value.
135              *
136              * @param intValue integer value
137              * @return corresponding «enums.name» item
138              * @throws IllegalArgumentException if {@code intValue} does not match any item
139              */
140             public static «enums.importedNonNull» ofValue(int intValue) {
141                 return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
142             }
143         }
144     '''
145
146     def writeEnumeration(Enumeration enumeration)
147     '''
148     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
149     «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«
150     ENDFOR»
151     '''
152 }