51ff8d35006f5fba38c6b047038965105a6408e8
[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
9 package org.opendaylight.controller.cluster.datastore.node.utils.stream;
10
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;
15 import java.util.Map;
16 import java.util.Objects;
17 import java.util.Set;
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;
21
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;
25
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.
40     @Deprecated
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;
44
45     private static final Map<Class<?>, Byte> TYPES;
46
47     static {
48         final Builder<Class<?>, Byte> b = ImmutableMap.builder();
49
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);
61
62         TYPES = b.build();
63     }
64
65     private ValueTypes() {
66         throw new UnsupportedOperationException("Utility class");
67     }
68
69     public static byte getSerializableType(Object node) {
70         Objects.requireNonNull(node);
71
72         final Byte type = TYPES.get(node.getClass());
73         if (type != null) {
74             if (type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD) {
75                 return STRING_BYTES_TYPE;
76             }
77             return type;
78         }
79
80         if (node instanceof Set) {
81             return BITS_TYPE;
82         }
83
84         if (node instanceof YangInstanceIdentifier) {
85             return YANG_IDENTIFIER_TYPE;
86         }
87
88         throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName());
89     }
90 }