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