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