Optimize ValueType class lookups 98/14498/4
authorRobert Varga <rovarga@cisco.com>
Mon, 26 Jan 2015 14:54:23 +0000 (15:54 +0100)
committerRobert Varga <rovarga@cisco.com>
Tue, 27 Jan 2015 08:46:20 +0000 (09:46 +0100)
Instead of using a mutable HashMap, use an ImmutableMap, which has much
better lookup performance.

Change-Id: Ia44ef5243abce021bdbd8afc930e5f5fc9529724
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/serialization/ValueType.java
opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java

index 2007544b7edd96d3498d303e3ad0ffd8469ada36..8169c9e9ae4c13bf5a08bc790f5c272e468faf35 100644 (file)
@@ -9,14 +9,14 @@
 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
 
 import com.google.common.base.Preconditions;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 public enum ValueType {
     SHORT_TYPE,
@@ -32,30 +32,34 @@ public enum ValueType {
     BIG_DECIMAL_TYPE,
     BINARY_TYPE;
 
-    private static Map<Class<?>, ValueType> types = new HashMap<>();
+    private static final Map<Class<?>, ValueType> TYPES;
 
     static {
-        types.put(String.class, STRING_TYPE);
-        types.put(Byte.class, BYTE_TYPE);
-        types.put(Integer.class, INT_TYPE);
-        types.put(Long.class, LONG_TYPE);
-        types.put(Boolean.class, BOOL_TYPE);
-        types.put(QName.class, QNAME_TYPE);
-        types.put(Set.class, BITS_TYPE);
-        types.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
-        types.put(Short.class,SHORT_TYPE);
-        types.put(BigInteger.class, BIG_INTEGER_TYPE);
-        types.put(BigDecimal.class, BIG_DECIMAL_TYPE);
-        types.put(byte[].class, BINARY_TYPE);
+        final Builder<Class<?>, ValueType> b = ImmutableMap.builder();
+
+        b.put(String.class, STRING_TYPE);
+        b.put(Byte.class, BYTE_TYPE);
+        b.put(Integer.class, INT_TYPE);
+        b.put(Long.class, LONG_TYPE);
+        b.put(Boolean.class, BOOL_TYPE);
+        b.put(QName.class, QNAME_TYPE);
+        b.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
+        b.put(Short.class,SHORT_TYPE);
+        b.put(BigInteger.class, BIG_INTEGER_TYPE);
+        b.put(BigDecimal.class, BIG_DECIMAL_TYPE);
+        b.put(byte[].class, BINARY_TYPE);
+
+        TYPES = b.build();
     }
 
-    public static final ValueType getSerializableType(Object node){
+    public static final ValueType getSerializableType(Object node) {
         Preconditions.checkNotNull(node, "node should not be null");
 
-        ValueType type = types.get(node.getClass());
-        if(type != null) {
+        final ValueType type = TYPES.get(node.getClass());
+        if (type != null) {
             return type;
-        } else if(node instanceof Set){
+        }
+        if (node instanceof Set) {
             return BITS_TYPE;
         }
 
index e75a454d394294795c633a1d57d83ccc5661ce98..3a2d2b49b3ac90c49a7855de4f64d3502d89c2e2 100644 (file)
@@ -9,15 +9,16 @@
 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
 import java.math.BigDecimal;
 import java.math.BigInteger;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
-public class ValueTypes {
+final class ValueTypes {
     public static final byte SHORT_TYPE = 1;
     public static final byte BYTE_TYPE = 2;
     public static final byte INT_TYPE = 3;
@@ -31,30 +32,38 @@ public class ValueTypes {
     public static final byte BIG_DECIMAL_TYPE = 11;
     public static final byte BINARY_TYPE = 12;
 
-    private static Map<Class<?>, Byte> types = new HashMap<>();
+    private static final Map<Class<?>, Byte> TYPES;
 
     static {
-        types.put(String.class, Byte.valueOf(STRING_TYPE));
-        types.put(Byte.class, Byte.valueOf(BYTE_TYPE));
-        types.put(Integer.class, Byte.valueOf(INT_TYPE));
-        types.put(Long.class, Byte.valueOf(LONG_TYPE));
-        types.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
-        types.put(QName.class, Byte.valueOf(QNAME_TYPE));
-        types.put(Set.class, Byte.valueOf(BITS_TYPE));
-        types.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE));
-        types.put(Short.class, Byte.valueOf(SHORT_TYPE));
-        types.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
-        types.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
-        types.put(byte[].class, Byte.valueOf(BINARY_TYPE));
+        final Builder<Class<?>, Byte> b = ImmutableMap.builder();
+
+        b.put(String.class, Byte.valueOf(STRING_TYPE));
+        b.put(Byte.class, Byte.valueOf(BYTE_TYPE));
+        b.put(Integer.class, Byte.valueOf(INT_TYPE));
+        b.put(Long.class, Byte.valueOf(LONG_TYPE));
+        b.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
+        b.put(QName.class, Byte.valueOf(QNAME_TYPE));
+        b.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE));
+        b.put(Short.class, Byte.valueOf(SHORT_TYPE));
+        b.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
+        b.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
+        b.put(byte[].class, Byte.valueOf(BINARY_TYPE));
+
+        TYPES = b.build();
     }
 
-    public static final byte getSerializableType(Object node){
+    private ValueTypes() {
+        throw new UnsupportedOperationException("Utility class");
+    }
+
+    public static final byte getSerializableType(Object node) {
         Preconditions.checkNotNull(node, "node should not be null");
 
-        Byte type = types.get(node.getClass());
-        if(type != null) {
+        final Byte type = TYPES.get(node.getClass());
+        if (type != null) {
             return type;
-        } else if(node instanceof Set){
+        }
+        if (node instanceof Set) {
             return BITS_TYPE;
         }