Generate a switch expression in forName()/forIntValue()
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / EnumTemplate.xtend
index 0bca47918eebcc8cbe5c04cbaa64a0fa08ad5938..89239186786e3216f6a80289d0ac33c7c0e6edd4 100644 (file)
@@ -7,13 +7,9 @@
  */
 package org.opendaylight.mdsal.binding.java.api.generator
 
-import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
+import static extension org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
 
-import com.google.common.collect.ImmutableMap
-import com.google.common.collect.ImmutableMap.Builder
-import java.util.Map
-import java.util.Objects
-import java.util.Optional
 import org.opendaylight.mdsal.binding.model.api.Enumeration
 import org.opendaylight.mdsal.binding.model.api.GeneratedType
 
@@ -56,7 +52,9 @@ class EnumTemplate extends BaseTemplate {
     }
 
     def writeEnumItem(String name, String mappedName, int value, String description) '''
-        «asJavadoc(encodeAngleBrackets(description))»
+        «IF description !== null»
+            «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
+        «ENDIF»
         «mappedName»(«value», "«name»")
     '''
 
@@ -66,62 +64,81 @@ class EnumTemplate extends BaseTemplate {
      * @return string with the enumeration body
      */
     override body() '''
-        «wrapToDocumentation(formatDataForJavaDoc(enums))»
+        «enums.formatDataForJavaDoc.wrapToDocumentation»
+        «generatedAnnotation»
         public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
             «writeEnumeration(enums)»
 
-            private static final «Map.importedName»<«String.importedName», «enums.name»> NAME_MAP;
-            private static final «Map.importedName»<«Integer.importedName», «enums.name»> VALUE_MAP;
-
-            static {
-                final «Builder.importedName»<«String.importedName», «enums.name»> nb = «ImmutableMap.importedName».builder();
-                final «Builder.importedName»<«Integer.importedName», «enums.name»> vb = «ImmutableMap.importedName».builder();
-                for («enums.name» enumItem : «enums.name».values()) {
-                    vb.put(enumItem.value, enumItem);
-                    nb.put(enumItem.name, enumItem);
-                }
-
-                NAME_MAP = nb.build();
-                VALUE_MAP = vb.build();
-            }
-
-            private final «String.importedName» name;
+            private final «STRING.importedNonNull» name;
             private final int value;
 
-            private «enums.name»(int value, «String.importedName» name) {
+            private «enums.name»(int value, «STRING.importedNonNull» name) {
                 this.value = value;
                 this.name = name;
             }
 
-            @Override
-            public «String.importedName» getName() {
+            @«OVERRIDE.importedName»
+            public «STRING.importedNonNull» getName() {
                 return name;
             }
 
-            @Override
+            @«OVERRIDE.importedName»
             public int getIntValue() {
                 return value;
             }
 
             /**
-             * Return the enumeration member whose {@link #getName()} matches specified value.
+             * Return the enumeration member whose {@link #getName()} matches specified assigned name.
+             *
+             * @param name YANG assigned name
+             * @return corresponding «enums.name» item, or {@code null} if no such item exists
+             * @throws NullPointerException if {@code name} is null
+             */
+            public static «enums.importedNullable» forName(«STRING.importedName» name) {
+                return switch (name) {
+                    «FOR v : enums.values»
+                    case "«v.name»" -> «v.mappedName»;
+                    «ENDFOR»
+                    default -> null;
+                };
+            }
+
+            /**
+             * Return the enumeration member whose {@link #getIntValue()} matches specified value.
+             *
+             * @param intValue integer value
+             * @return corresponding «enums.name» item, or {@code null} if no such item exists
+             */
+            public static «enums.importedNullable» forValue(int intValue) {
+                return switch (intValue) {
+                    «FOR v : enums.values»
+                    case «v.value» -> «v.mappedName»;
+                    «ENDFOR»
+                    default -> null;
+                };
+            }
+
+            /**
+             * Return the enumeration member whose {@link #getName()} matches specified assigned name.
              *
              * @param name YANG assigned name
-             * @return corresponding «enums.name» item, if present
-             * @throws NullPointerException if name is null
+             * @return corresponding «enums.name» item
+             * @throws NullPointerException if {@code name} is null
+             * @throws IllegalArgumentException if {@code name} does not match any item
              */
-            public static «Optional.importedName»<«enums.name»> forName(«String.importedName» name) {
-                return «Optional.importedName».ofNullable(NAME_MAP.get(«Objects.importedName».requireNonNull(name)));
+            public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
+                return «CODEHELPERS.importedName».checkEnum(forName(name), name);
             }
 
             /**
              * Return the enumeration member whose {@link #getIntValue()} matches specified value.
              *
              * @param intValue integer value
-             * @return corresponding «enums.name» item, or null if no such item exists
+             * @return corresponding «enums.name» item
+             * @throws IllegalArgumentException if {@code intValue} does not match any item
              */
-            public static «enums.name» forValue(int intValue) {
-                return VALUE_MAP.get(intValue);
+            public static «enums.importedNonNull» ofValue(int intValue) {
+                return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
             }
         }
     '''