8169c9e9ae4c13bf5a08bc790f5c272e468faf35
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / serialization / ValueType.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.serialization;
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 public enum ValueType {
22     SHORT_TYPE,
23     BYTE_TYPE,
24     INT_TYPE,
25     LONG_TYPE,
26     BOOL_TYPE,
27     QNAME_TYPE,
28     BITS_TYPE,
29     YANG_IDENTIFIER_TYPE,
30     STRING_TYPE,
31     BIG_INTEGER_TYPE,
32     BIG_DECIMAL_TYPE,
33     BINARY_TYPE;
34
35     private static final Map<Class<?>, ValueType> TYPES;
36
37     static {
38         final Builder<Class<?>, ValueType> b = ImmutableMap.builder();
39
40         b.put(String.class, STRING_TYPE);
41         b.put(Byte.class, BYTE_TYPE);
42         b.put(Integer.class, INT_TYPE);
43         b.put(Long.class, LONG_TYPE);
44         b.put(Boolean.class, BOOL_TYPE);
45         b.put(QName.class, QNAME_TYPE);
46         b.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
47         b.put(Short.class,SHORT_TYPE);
48         b.put(BigInteger.class, BIG_INTEGER_TYPE);
49         b.put(BigDecimal.class, BIG_DECIMAL_TYPE);
50         b.put(byte[].class, BINARY_TYPE);
51
52         TYPES = b.build();
53     }
54
55     public static final ValueType getSerializableType(Object node) {
56         Preconditions.checkNotNull(node, "node should not be null");
57
58         final ValueType type = TYPES.get(node.getClass());
59         if (type != null) {
60             return type;
61         }
62         if (node instanceof Set) {
63             return BITS_TYPE;
64         }
65
66         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
67     }
68 }