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;
16 import java.util.Objects;
18 import org.opendaylight.yangtools.yang.common.Empty;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 final class ValueTypes {
23 // The String length threshold beyond which a String should be encoded as bytes
24 public static final int STRING_BYTES_LENGTH_THRESHOLD = Short.MAX_VALUE / 4;
26 public static final byte SHORT_TYPE = 1;
27 public static final byte BYTE_TYPE = 2;
28 public static final byte INT_TYPE = 3;
29 public static final byte LONG_TYPE = 4;
30 public static final byte BOOL_TYPE = 5;
31 public static final byte QNAME_TYPE = 6;
32 public static final byte BITS_TYPE = 7;
33 public static final byte YANG_IDENTIFIER_TYPE = 8;
34 public static final byte STRING_TYPE = 9;
35 public static final byte BIG_INTEGER_TYPE = 10;
36 public static final byte BIG_DECIMAL_TYPE = 11;
37 public static final byte BINARY_TYPE = 12;
38 // Leaf nodes no longer allow null values. The "empty" type is now represented as
39 // org.opendaylight.yangtools.yang.common.Empty. This is kept for backwards compatibility.
41 public static final byte NULL_TYPE = 13;
42 public static final byte STRING_BYTES_TYPE = 14;
43 public static final byte EMPTY_TYPE = 15;
45 private static final Map<Class<?>, Byte> TYPES;
48 final Builder<Class<?>, Byte> b = ImmutableMap.builder();
50 b.put(String.class, STRING_TYPE);
51 b.put(Byte.class, BYTE_TYPE);
52 b.put(Integer.class, INT_TYPE);
53 b.put(Long.class, LONG_TYPE);
54 b.put(Boolean.class, BOOL_TYPE);
55 b.put(QName.class, QNAME_TYPE);
56 b.put(Short.class, SHORT_TYPE);
57 b.put(BigInteger.class, BIG_INTEGER_TYPE);
58 b.put(BigDecimal.class, BIG_DECIMAL_TYPE);
59 b.put(byte[].class, BINARY_TYPE);
60 b.put(Empty.class, EMPTY_TYPE);
65 private ValueTypes() {
66 throw new UnsupportedOperationException("Utility class");
69 public static byte getSerializableType(Object node) {
70 Objects.requireNonNull(node);
72 final Byte type = TYPES.get(node.getClass());
74 if (type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD) {
75 return STRING_BYTES_TYPE;
80 if (node instanceof Set) {
84 if (node instanceof YangInstanceIdentifier) {
85 return YANG_IDENTIFIER_TYPE;
88 throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());