Merge "Bug 2265: Use streaming for DeleteData message"
[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 com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import java.math.BigDecimal;
15 import java.math.BigInteger;
16 import java.util.Map;
17 import java.util.Set;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20
21 final class ValueTypes {
22     public static final byte SHORT_TYPE = 1;
23     public static final byte BYTE_TYPE = 2;
24     public static final byte INT_TYPE = 3;
25     public static final byte LONG_TYPE = 4;
26     public static final byte BOOL_TYPE = 5;
27     public static final byte QNAME_TYPE = 6;
28     public static final byte BITS_TYPE = 7;
29     public static final byte YANG_IDENTIFIER_TYPE = 8;
30     public static final byte STRING_TYPE = 9;
31     public static final byte BIG_INTEGER_TYPE = 10;
32     public static final byte BIG_DECIMAL_TYPE = 11;
33     public static final byte BINARY_TYPE = 12;
34
35     private static final Map<Class<?>, Byte> TYPES;
36
37     static {
38         final Builder<Class<?>, Byte> b = ImmutableMap.builder();
39
40         b.put(String.class, Byte.valueOf(STRING_TYPE));
41         b.put(Byte.class, Byte.valueOf(BYTE_TYPE));
42         b.put(Integer.class, Byte.valueOf(INT_TYPE));
43         b.put(Long.class, Byte.valueOf(LONG_TYPE));
44         b.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
45         b.put(QName.class, Byte.valueOf(QNAME_TYPE));
46         b.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE));
47         b.put(Short.class, Byte.valueOf(SHORT_TYPE));
48         b.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
49         b.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
50         b.put(byte[].class, Byte.valueOf(BINARY_TYPE));
51
52         TYPES = b.build();
53     }
54
55     private ValueTypes() {
56         throw new UnsupportedOperationException("Utility class");
57     }
58
59     public static final byte getSerializableType(Object node) {
60         Preconditions.checkNotNull(node, "node should not be null");
61
62         final Byte type = TYPES.get(node.getClass());
63         if (type != null) {
64             return type;
65         }
66         if (node instanceof Set) {
67             return BITS_TYPE;
68         }
69
70         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
71     }
72 }