Use ImmutableMap for BGPErrors 50/80850/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 14 Mar 2019 10:57:44 +0000 (11:57 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 14 Mar 2019 11:01:09 +0000 (12:01 +0100)
This prevents accidental overlaps and improves performance a bit.

Change-Id: Icc6d1227a66c5f287bc95b4a66b7faebfbcc647c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/parser-api/src/main/java/org/opendaylight/protocol/bgp/parser/BGPError.java

index d306262d4be90638fb37a3ba4b805fdb17af8d35..7aeb71dba184477dd84402a496fcda51b08c6a43 100644 (file)
@@ -7,11 +7,12 @@
  */
 package org.opendaylight.protocol.bgp.parser;
 
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Maps;
 import java.io.Serializable;
-import java.util.Map;
-
+import java.util.Arrays;
 
 /**
  * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode
@@ -155,14 +156,8 @@ public enum BGPError {
 
     public static final String MANDATORY_ATTR_MISSING_MSG = "Well known mandatory attribute missing: ";
 
-    private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
-
-    static {
-        VALUE_MAP = Maps.newHashMap();
-        for (final BGPError enumItem : BGPError.values()) {
-            VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
-        }
-    }
+    private static final ImmutableMap<BGPErrorIdentifier, BGPError> VALUE_MAP = Maps.uniqueIndex(
+        Arrays.asList(values()), BGPError::getErrorIdentifier);
 
     private final BGPErrorIdentifier errorId;
 
@@ -172,8 +167,7 @@ public enum BGPError {
 
     public static BGPError forValue(final int code, final int subcode) {
         final BGPError e = VALUE_MAP.get(new BGPErrorIdentifier((short) code, (short) subcode));
-        Preconditions.checkArgument(e != null, "BGP Error code %s and subcode %s not recognized.",
-                code, subcode);
+        checkArgument(e != null, "BGP Error code %s and subcode %s not recognized.", code, subcode);
         return e;
     }