Cleanup ValueTypes lookup
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / stream / ValueTypes.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 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.math.BigDecimal;
14 import java.math.BigInteger;
15 import java.util.Set;
16 import org.opendaylight.yangtools.yang.common.Empty;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19
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;
23
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     // Leaf nodes no longer allow null values. The "empty" type is now represented as
37     // org.opendaylight.yangtools.yang.common.Empty. This is kept for backwards compatibility.
38     @Deprecated
39     public static final byte NULL_TYPE = 13;
40     public static final byte STRING_BYTES_TYPE = 14;
41     public static final byte EMPTY_TYPE = 15;
42
43     private static final ImmutableMap<Class<?>, Byte> TYPES = ImmutableMap.<Class<?>, Byte>builder()
44             .put(String.class, STRING_TYPE)
45             .put(Byte.class, BYTE_TYPE)
46             .put(Integer.class, INT_TYPE)
47             .put(Long.class, LONG_TYPE)
48             .put(Boolean.class, BOOL_TYPE)
49             .put(QName.class, QNAME_TYPE)
50             .put(Short.class, SHORT_TYPE)
51             .put(BigInteger.class, BIG_INTEGER_TYPE)
52             .put(BigDecimal.class, BIG_DECIMAL_TYPE)
53             .put(byte[].class, BINARY_TYPE)
54             .put(Empty.class, EMPTY_TYPE)
55             .build();
56
57     private ValueTypes() {
58         throw new UnsupportedOperationException("Utility class");
59     }
60
61     static byte getSerializableType(final Object node) {
62         final Byte type = TYPES.get(requireNonNull(node).getClass());
63         if (type != null) {
64             if (type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD) {
65                 return STRING_BYTES_TYPE;
66             }
67             return type;
68         }
69
70         if (node instanceof Set) {
71             return BITS_TYPE;
72         }
73
74         if (node instanceof YangInstanceIdentifier) {
75             return YANG_IDENTIFIER_TYPE;
76         }
77
78         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
79     }
80 }