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