2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore.node.utils.serialization;
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;
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;
22 public enum ValueType {
25 Object deserialize(final String str) {
26 return Short.valueOf(str);
31 Object deserialize(final String str) {
32 return Byte.valueOf(str);
37 Object deserialize(final String str) {
38 return Integer.valueOf(str);
43 Object deserialize(final String str) {
44 return Long.valueOf(str);
49 Object deserialize(final String str) {
50 return Boolean.valueOf(str);
55 Object deserialize(final String str) {
56 return QNameFactory.create(str);
61 Object deserialize(final String str) {
62 throw new UnsupportedOperationException("Should have been caught by caller");
65 YANG_IDENTIFIER_TYPE {
67 Object deserialize(final String str) {
68 throw new UnsupportedOperationException("Should have been caught by caller");
73 Object deserialize(final String str) {
79 Object deserialize(final String str) {
80 return new BigInteger(str);
85 Object deserialize(final String str) {
86 return new BigDecimal(str);
91 Object deserialize(final String str) {
92 throw new UnsupportedOperationException("Should have been caught by caller");
96 private static final Map<Class<?>, ValueType> TYPES;
99 final Builder<Class<?>, ValueType> b = ImmutableMap.builder();
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);
116 abstract Object deserialize(String str);
118 public static final ValueType getSerializableType(Object node) {
119 Preconditions.checkNotNull(node, "node should not be null");
121 final ValueType type = TYPES.get(node.getClass());
125 if (node instanceof Set) {
129 throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());