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