b9e46a3a570fd159d1c8b9284d2efc2ec95c214b
[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.controller.cluster.datastore.node.utils.QNameFactory;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21
22 public enum ValueType {
23     SHORT_TYPE {
24         @Override
25         Object deserialize(final String str) {
26             return Short.valueOf(str);
27         }
28     },
29     BYTE_TYPE {
30         @Override
31         Object deserialize(final String str) {
32             return Byte.valueOf(str);
33         }
34     },
35     INT_TYPE {
36         @Override
37         Object deserialize(final String str) {
38             return Integer.valueOf(str);
39         }
40     },
41     LONG_TYPE {
42         @Override
43         Object deserialize(final String str) {
44             return Long.valueOf(str);
45         }
46     },
47     BOOL_TYPE {
48         @Override
49         Object deserialize(final String str) {
50             return Boolean.valueOf(str);
51         }
52     },
53     QNAME_TYPE {
54         @Override
55         Object deserialize(final String str) {
56             return QNameFactory.create(str);
57         }
58     },
59     BITS_TYPE {
60         @Override
61         Object deserialize(final String str) {
62             throw new UnsupportedOperationException("Should have been caught by caller");
63         }
64     },
65     YANG_IDENTIFIER_TYPE {
66         @Override
67         Object deserialize(final String str) {
68             throw new UnsupportedOperationException("Should have been caught by caller");
69         }
70     },
71     STRING_TYPE {
72         @Override
73         Object deserialize(final String str) {
74             return str;
75         }
76     },
77     BIG_INTEGER_TYPE {
78         @Override
79         Object deserialize(final String str) {
80             return new BigInteger(str);
81         }
82     },
83     BIG_DECIMAL_TYPE {
84         @Override
85         Object deserialize(final String str) {
86             return new BigDecimal(str);
87         }
88     },
89     BINARY_TYPE {
90         @Override
91         Object deserialize(final String str) {
92             throw new UnsupportedOperationException("Should have been caught by caller");
93         }
94     };
95
96     private static final Map<Class<?>, ValueType> TYPES;
97
98     static {
99         final Builder<Class<?>, ValueType> b = ImmutableMap.builder();
100
101         b.put(String.class, STRING_TYPE);
102         b.put(Byte.class, BYTE_TYPE);
103         b.put(Integer.class, INT_TYPE);
104         b.put(Long.class, LONG_TYPE);
105         b.put(Boolean.class, BOOL_TYPE);
106         b.put(QName.class, QNAME_TYPE);
107         b.put(YangInstanceIdentifier.class, YANG_IDENTIFIER_TYPE);
108         b.put(Short.class,SHORT_TYPE);
109         b.put(BigInteger.class, BIG_INTEGER_TYPE);
110         b.put(BigDecimal.class, BIG_DECIMAL_TYPE);
111         b.put(byte[].class, BINARY_TYPE);
112
113         TYPES = b.build();
114     }
115
116     abstract Object deserialize(String str);
117
118     public static final ValueType getSerializableType(Object node) {
119         Preconditions.checkNotNull(node, "node should not be null");
120
121         final ValueType type = TYPES.get(node.getClass());
122         if (type != null) {
123             return type;
124         }
125         if (node instanceof Set) {
126             return BITS_TYPE;
127         }
128
129         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
130     }
131 }