Bump versions to 13.0.4-SNAPSHOT
[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 import org.opendaylight.mdsal.binding.model.api.JavaTypeName
16 import org.opendaylight.yangtools.yang.binding.EnumTypeObject
17
18 /**
19  * Template for generating JAVA enumeration type.
20  */
21 class EnumTemplate extends BaseTemplate {
22     static val ENUM_TYPE_OBJECT = JavaTypeName.create(EnumTypeObject)
23
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         «IF description !== null»
60             «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
61         «ENDIF»
62         «mappedName»(«value», "«name»")
63     '''
64
65     /**
66      * Template method which generates enumeration body (declaration + enumeration items).
67      *
68      * @return string with the enumeration body
69      */
70     override body() '''
71         «enums.formatDataForJavaDoc.wrapToDocumentation»
72         «generatedAnnotation»
73         public enum «enums.name» implements «ENUM_TYPE_OBJECT.importedName» {
74             «writeEnumeration(enums)»
75
76             private final «STRING.importedNonNull» name;
77             private final int value;
78
79             private «enums.name»(int value, «STRING.importedNonNull» name) {
80                 this.value = value;
81                 this.name = name;
82             }
83
84             @«OVERRIDE.importedName»
85             public «STRING.importedNonNull» getName() {
86                 return name;
87             }
88
89             @«OVERRIDE.importedName»
90             public int getIntValue() {
91                 return value;
92             }
93
94             /**
95              * Return the enumeration member whose {@link #getName()} matches specified assigned name.
96              *
97              * @param name YANG assigned name
98              * @return corresponding «enums.name» item, or {@code null} if no such item exists
99              * @throws «NPE.importedName» if {@code name} is null
100              */
101             public static «enums.importedNullable» forName(«STRING.importedName» name) {
102                 return switch (name) {
103                     «FOR v : enums.values»
104                     case "«v.name»" -> «v.mappedName»;
105                     «ENDFOR»
106                     default -> null;
107                 };
108             }
109
110             /**
111              * Return the enumeration member whose {@link #getIntValue()} matches specified value.
112              *
113              * @param intValue integer value
114              * @return corresponding «enums.name» item, or {@code null} if no such item exists
115              */
116             public static «enums.importedNullable» forValue(int intValue) {
117                 return switch (intValue) {
118                     «FOR v : enums.values»
119                     case «v.value» -> «v.mappedName»;
120                     «ENDFOR»
121                     default -> null;
122                 };
123             }
124
125             /**
126              * Return the enumeration member whose {@link #getName()} matches specified assigned name.
127              *
128              * @param name YANG assigned name
129              * @return corresponding «enums.name» item
130              * @throws «NPE.importedName» if {@code name} is null
131              * @throws «IAE.importedName» if {@code name} does not match any item
132              */
133             public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
134                 return «CODEHELPERS.importedName».checkEnum(forName(name), name);
135             }
136
137             /**
138              * Return the enumeration member whose {@link #getIntValue()} matches specified value.
139              *
140              * @param intValue integer value
141              * @return corresponding «enums.name» item
142              * @throws «IAE.importedName» if {@code intValue} does not match any item
143              */
144             public static «enums.importedNonNull» ofValue(int intValue) {
145                 return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
146             }
147         }
148     '''
149
150     def writeEnumeration(Enumeration enumeration)
151     '''
152     «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
153     «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«
154     ENDFOR»
155     '''
156 }