Bug 2003: CDS serialization improvements
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / serialization / PathArgumentType.java
index 20009d8347564c59d6f98ff537324938f6168a87..58a09ae88599204a4414f9844db1bb57e032be32 100644 (file)
@@ -8,8 +8,9 @@
 
 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
 
-import com.google.common.base.Preconditions;
+import java.util.Map;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import com.google.common.collect.ImmutableMap;
 
 public enum PathArgumentType {
     AUGMENTATION_IDENTIFIER,
@@ -17,19 +18,21 @@ public enum PathArgumentType {
     NODE_IDENTIFIER_WITH_VALUE,
     NODE_IDENTIFIER_WITH_PREDICATES;
 
+    private static Map<Class<?>, PathArgumentType> CLASS_TO_ENUM_MAP =
+            ImmutableMap.<Class<?>, PathArgumentType>builder().
+                put(YangInstanceIdentifier.AugmentationIdentifier.class, AUGMENTATION_IDENTIFIER).
+                put(YangInstanceIdentifier.NodeIdentifier.class, NODE_IDENTIFIER).
+                put(YangInstanceIdentifier.NodeIdentifierWithPredicates.class, NODE_IDENTIFIER_WITH_PREDICATES).
+                put(YangInstanceIdentifier.NodeWithValue.class, NODE_IDENTIFIER_WITH_VALUE).build();
+
     public static int getSerializablePathArgumentType(YangInstanceIdentifier.PathArgument pathArgument){
-        Preconditions.checkNotNull(pathArgument, "pathArgument should not be null");
-
-        if(pathArgument instanceof YangInstanceIdentifier.AugmentationIdentifier){
-            return AUGMENTATION_IDENTIFIER.ordinal();
-        } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifier){
-            return NODE_IDENTIFIER.ordinal();
-        } else if(pathArgument instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates){
-            return NODE_IDENTIFIER_WITH_PREDICATES.ordinal();
-        } else if(pathArgument instanceof YangInstanceIdentifier.NodeWithValue){
-            return NODE_IDENTIFIER_WITH_VALUE.ordinal();
+
+        PathArgumentType type = CLASS_TO_ENUM_MAP.get(pathArgument.getClass());
+        if(type == null) {
+            throw new IllegalArgumentException("Unknown type of PathArgument = " + pathArgument);
         }
-        throw new IllegalArgumentException("Unknown type of PathArgument = " + pathArgument.toString());
+
+        return type.ordinal();
     }
 
 }