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