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.stream;
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.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 final class ValueTypes {
21 // The String length threshold beyond which a String should be encoded as bytes
22 public static final int STRING_BYTES_LENGTH_THRESHOLD = Short.MAX_VALUE / 4;
24 public static final byte SHORT_TYPE = 1;
25 public static final byte BYTE_TYPE = 2;
26 public static final byte INT_TYPE = 3;
27 public static final byte LONG_TYPE = 4;
28 public static final byte BOOL_TYPE = 5;
29 public static final byte QNAME_TYPE = 6;
30 public static final byte BITS_TYPE = 7;
31 public static final byte YANG_IDENTIFIER_TYPE = 8;
32 public static final byte STRING_TYPE = 9;
33 public static final byte BIG_INTEGER_TYPE = 10;
34 public static final byte BIG_DECIMAL_TYPE = 11;
35 public static final byte BINARY_TYPE = 12;
36 public static final byte NULL_TYPE = 13;
37 public static final byte STRING_BYTES_TYPE = 14;
39 private static final Map<Class<?>, Byte> TYPES;
42 final Builder<Class<?>, Byte> b = ImmutableMap.builder();
44 b.put(String.class, Byte.valueOf(STRING_TYPE));
45 b.put(Byte.class, Byte.valueOf(BYTE_TYPE));
46 b.put(Integer.class, Byte.valueOf(INT_TYPE));
47 b.put(Long.class, Byte.valueOf(LONG_TYPE));
48 b.put(Boolean.class, Byte.valueOf(BOOL_TYPE));
49 b.put(QName.class, Byte.valueOf(QNAME_TYPE));
50 b.put(Short.class, Byte.valueOf(SHORT_TYPE));
51 b.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE));
52 b.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE));
53 b.put(byte[].class, Byte.valueOf(BINARY_TYPE));
58 private ValueTypes() {
59 throw new UnsupportedOperationException("Utility class");
62 public static final byte getSerializableType(Object node) {
67 final Byte type = TYPES.get(node.getClass());
69 if(type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD ){
70 return STRING_BYTES_TYPE;
75 if (node instanceof Set) {
79 if (node instanceof YangInstanceIdentifier) {
80 return YANG_IDENTIFIER_TYPE;
83 throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());