Merge "Speed up ClusterWrapperImpl"
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / ValueTypes.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
11 import com.google.common.base.Preconditions;
12 import java.math.BigDecimal;
13 import java.math.BigInteger;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19
20 public class ValueTypes {
21     public static final byte SHORT_TYPE = 1;
22     public static final byte BYTE_TYPE = 2;
23     public static final byte INT_TYPE = 3;
24     public static final byte LONG_TYPE = 4;
25     public static final byte BOOL_TYPE = 5;
26     public static final byte QNAME_TYPE = 6;
27     public static final byte BITS_TYPE = 7;
28     public static final byte YANG_IDENTIFIER_TYPE = 8;
29     public static final byte STRING_TYPE = 9;
30     public static final byte BIG_INTEGER_TYPE = 10;
31     public static final byte BIG_DECIMAL_TYPE = 11;
32     public static final byte BINARY_TYPE = 12;
33
34     private static Map<Class<?>, Byte> types = new HashMap<>();
35
36     static {
37         types.put(String.class, Byte.valueOf(STRING_TYPE));
38         types.put(Byte.class, Byte.valueOf(BYTE_TYPE));
39         types.put(Integer.class, Byte.valueOf(INT_TYPE));
40         types.put(Long.class, Byte.valueOf(LONG_TYPE));
41         types.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
42         types.put(QName.class, Byte.valueOf(QNAME_TYPE));
43         types.put(Set.class, Byte.valueOf(BITS_TYPE));
44         types.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE));
45         types.put(Short.class, Byte.valueOf(SHORT_TYPE));
46         types.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
47         types.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
48         types.put(byte[].class, Byte.valueOf(BINARY_TYPE));
49     }
50
51     public static final byte getSerializableType(Object node){
52         Preconditions.checkNotNull(node, "node should not be null");
53
54         Byte type = types.get(node.getClass());
55         if(type != null) {
56             return type;
57         } else if(node instanceof Set){
58             return BITS_TYPE;
59         }
60
61         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
62     }
63 }