Use switch expression in forElementBody() 31/107231/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 3 Aug 2023 15:54:28 +0000 (17:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 3 Aug 2023 15:54:28 +0000 (17:54 +0200)
We have an utterly simple dispatch here. Eliminate the use of an
ImmutableMap and just make it a switch expression.

Change-Id: I3cfbfe2ed183b69cad35edc1abc129acb07c93a7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/ErrorSeverity.java

index 2c52019aa7292fda6d3c7bea9c71d4a855f399ec..9bcadd38818922a3a22995a93edd785e6b099be3 100644 (file)
@@ -9,9 +9,6 @@ package org.opendaylight.yangtools.yang.common;
 
 import static java.util.Objects.requireNonNull;
 
-import com.google.common.collect.Maps;
-import java.util.Arrays;
-import java.util.Map;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -30,13 +27,10 @@ public enum ErrorSeverity {
      */
     WARNING("warning");
 
-    private static final Map<String, ErrorSeverity> BY_ELEMENT_BODY =
-        Maps.uniqueIndex(Arrays.asList(values()), ErrorSeverity::elementBody);
-
     private final String elementBody;
 
-    ErrorSeverity(final String elementName) {
-        this.elementBody = requireNonNull(elementName);
+    ErrorSeverity(final String elementBody) {
+        this.elementBody = requireNonNull(elementBody);
     }
 
     /**
@@ -49,6 +43,10 @@ public enum ErrorSeverity {
     }
 
     public static @Nullable ErrorSeverity forElementBody(final String elementBody) {
-        return BY_ELEMENT_BODY.get(requireNonNull(elementBody));
+        return switch (elementBody) {
+            case "error" -> ERROR;
+            case "warning" -> WARNING;
+            default -> null;
+        };
     }
 }