Bug 2480: Union objects are generated incorrectly when using bits type
[mdsal.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / EnumTemplate.xtend
index 9fe871c399b93ce371219b7d24a811569a53380d..417451bc181eee82bc51d14c51909aeb56430d8c 100644 (file)
-package org.opendaylight.yangtools.sal.java.api.generator\r
-\r
-import org.opendaylight.yangtools.sal.binding.model.api.Enumeration\r
-import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType\r
-/**\r
- * Template for generating JAVA enumeration type. \r
- */\r
-class EnumTemplate extends BaseTemplate {\r
-\r
-    \r
-    /**\r
-     * Enumeration which will be transformed to JAVA source code for enumeration\r
-     */\r
-    val Enumeration enums\r
-    \r
-    /**\r
-     * Constructs instance of this class with concrete <code>enums</code>.\r
-     * \r
-     * @param enumeration which will be transformed to JAVA source code \r
-     */\r
-    new(Enumeration enums) {\r
-        super(enums as GeneratedType )\r
-        this.enums = enums\r
-    }\r
-    \r
-    \r
-    /**\r
-     * Generates only JAVA enumeration source code.\r
-     * \r
-     * @return string with JAVA enumeration source code\r
-     */\r
-    def generateAsInnerClass() {\r
-        return body\r
-    }\r
-    \r
-    /**\r
-     * Template method which generates enumeration body (declaration + enumeration items).\r
-     * \r
-     * @return string with the enumeration body \r
-     */\r
-    override body() '''\r
-        public enum «enums.name» {\r
-        «FOR v : enums.values SEPARATOR ",\n"»\r
-            «"    "»«v.name»(«v.value»)«\r
-        ENDFOR»;\r
-        \r
-            int value;\r
-        \r
-            private «enums.name»(int value) {\r
-                this.value = value;\r
-            }\r
-        }\r
-    '''\r
-}
\ No newline at end of file
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.sal.java.api.generator
+
+import org.opendaylight.yangtools.sal.binding.model.api.Enumeration
+import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType
+
+/**
+ * Template for generating JAVA enumeration type. 
+ */
+class EnumTemplate extends BaseTemplate {
+
+    
+    /**
+     * Enumeration which will be transformed to JAVA source code for enumeration
+     */
+    val Enumeration enums
+    
+    /**
+     * Constructs instance of this class with concrete <code>enums</code>.
+     * 
+     * @param enums enumeration which will be transformed to JAVA source code 
+     */
+    new(Enumeration enums) {
+        super(enums as GeneratedType )
+        this.enums = enums
+    }
+    
+    
+    /**
+     * Generates only JAVA enumeration source code.
+     * 
+     * @return string with JAVA enumeration source code
+     */
+    def generateAsInnerClass() {
+        return body
+    }
+    
+    def writeEnumItem(String name, int value, String description) '''
+        «asJavadoc(formatToParagraph(description))»
+        «name»(«value»)
+    '''
+
+    /**
+     * Template method which generates enumeration body (declaration + enumeration items).
+     * 
+     * @return string with the enumeration body 
+     */
+    override body() '''
+        «wrapToDocumentation(formatDataForJavaDoc(enums))»
+        public enum «enums.name» {
+            «writeEnumeration(enums)»
+
+
+            int value;
+            private static final java.util.Map<java.lang.Integer, «enums.name»> VALUE_MAP;
+
+            static {
+                final com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> b = com.google.common.collect.ImmutableMap.builder();
+                for («enums.name» enumItem : «enums.name».values())
+                {
+                    b.put(enumItem.value, enumItem);
+                }
+
+                VALUE_MAP = b.build();
+            }
+        
+            private «enums.name»(int value) {
+                this.value = value;
+            }
+            
+            /**
+             * @return integer value
+             */
+            public int getIntValue() {
+                return value;
+            }
+
+            /**
+             * @param valueArg
+             * @return corresponding «enums.name» item
+             */
+            public static «enums.name» forValue(int valueArg) {
+                return VALUE_MAP.get(valueArg);
+            }
+        }
+    '''
+
+    def writeEnumeration(Enumeration enumeration)
+    '''
+    «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
+    «writeEnumItem(v.name, v.value, v.description)»«
+    ENDFOR»
+    '''
+}