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.collect.ImmutableMap;
12 import com.google.common.collect.ImmutableMap.Builder;
13 import java.math.BigDecimal;
14 import java.math.BigInteger;
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;
21 public enum ValueType {
24 Object deserialize(final String str) {
25 return Short.valueOf(str);
30 Object deserialize(final String str) {
31 return Byte.valueOf(str);
36 Object deserialize(final String str) {
37 return Integer.valueOf(str);
42 Object deserialize(final String str) {
43 return Long.valueOf(str);
48 Object deserialize(final String str) {
49 return Boolean.valueOf(str);
54 Object deserialize(final String str) {
55 return QNameFactory.create(str);
60 Object deserialize(final String str) {
61 throw new UnsupportedOperationException("Should have been caught by caller");
64 YANG_IDENTIFIER_TYPE {
66 Object deserialize(final String str) {
67 throw new UnsupportedOperationException("Should have been caught by caller");
72 Object deserialize(final String str) {
78 Object deserialize(final String str) {
79 return new BigInteger(str);
84 Object deserialize(final String str) {
85 return new BigDecimal(str);
90 Object deserialize(final String str) {
91 throw new UnsupportedOperationException("Should have been caught by caller");
96 Object deserialize(final String str) {
101 private static final Map<Class<?>, ValueType> TYPES;
104 final Builder<Class<?>, ValueType> b = ImmutableMap.builder();
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);
120 abstract Object deserialize(String str);
122 public static final ValueType getSerializableType(Object node) {
127 final ValueType type = TYPES.get(node.getClass());
131 if (node instanceof Set) {
135 if (node instanceof YangInstanceIdentifier) {
136 return YANG_IDENTIFIER_TYPE;
139 throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());