X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fmd-sal%2Fsal-clustering-commons%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fdatastore%2Fnode%2Futils%2Fstream%2FValueTypes.java;h=2fd8055c15b8556122fe8ef59f3330c976e31636;hb=b08cec09b7113965fbda5ef1757ca23d7588e5cc;hp=80fa527b4613e54d942aa14cc121c3cf234dd410;hpb=4206a55cd7724e15e3c5b7b5b7c12f78481384cf;p=controller.git diff --git a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java index 80fa527b46..2fd8055c15 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/main/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/ValueTypes.java @@ -5,20 +5,22 @@ * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ - package org.opendaylight.controller.cluster.datastore.node.utils.stream; -import com.google.common.base.Preconditions; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import static java.util.Objects.requireNonNull; +import com.google.common.collect.ImmutableMap; import java.math.BigDecimal; import java.math.BigInteger; -import java.util.HashMap; -import java.util.Map; import java.util.Set; +import org.opendaylight.yangtools.yang.common.Empty; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; + +final class ValueTypes { + // The String length threshold beyond which a String should be encoded as bytes + public static final int STRING_BYTES_LENGTH_THRESHOLD = Short.MAX_VALUE / 4; -public class ValueTypes { public static final byte SHORT_TYPE = 1; public static final byte BYTE_TYPE = 2; public static final byte INT_TYPE = 3; @@ -30,33 +32,49 @@ public class ValueTypes { public static final byte STRING_TYPE = 9; public static final byte BIG_INTEGER_TYPE = 10; public static final byte BIG_DECIMAL_TYPE = 11; + public static final byte BINARY_TYPE = 12; + // Leaf nodes no longer allow null values. The "empty" type is now represented as + // org.opendaylight.yangtools.yang.common.Empty. This is kept for backwards compatibility. + @Deprecated + public static final byte NULL_TYPE = 13; + public static final byte STRING_BYTES_TYPE = 14; + public static final byte EMPTY_TYPE = 15; - private static Map, Byte> types = new HashMap<>(); - - static { - types.put(String.class, Byte.valueOf(STRING_TYPE)); - types.put(Byte.class, Byte.valueOf(BYTE_TYPE)); - types.put(Integer.class, Byte.valueOf(INT_TYPE)); - types.put(Long.class, Byte.valueOf(LONG_TYPE)); - types.put(Boolean.class, Byte.valueOf(BOOL_TYPE)); - types.put(QName.class, Byte.valueOf(QNAME_TYPE)); - types.put(Set.class, Byte.valueOf(BITS_TYPE)); - types.put(YangInstanceIdentifier.class, Byte.valueOf(YANG_IDENTIFIER_TYPE)); - types.put(Short.class, Byte.valueOf(SHORT_TYPE)); - types.put(BigInteger.class, Byte.valueOf(BIG_INTEGER_TYPE)); - types.put(BigDecimal.class, Byte.valueOf(BIG_DECIMAL_TYPE)); - } + private static final ImmutableMap, Byte> TYPES = ImmutableMap., Byte>builder() + .put(String.class, STRING_TYPE) + .put(Byte.class, BYTE_TYPE) + .put(Integer.class, INT_TYPE) + .put(Long.class, LONG_TYPE) + .put(Boolean.class, BOOL_TYPE) + .put(QName.class, QNAME_TYPE) + .put(Short.class, SHORT_TYPE) + .put(BigInteger.class, BIG_INTEGER_TYPE) + .put(BigDecimal.class, BIG_DECIMAL_TYPE) + .put(byte[].class, BINARY_TYPE) + .put(Empty.class, EMPTY_TYPE) + .build(); - public static final byte getSerializableType(Object node){ - Preconditions.checkNotNull(node, "node should not be null"); + private ValueTypes() { + throw new UnsupportedOperationException("Utility class"); + } - Byte type = types.get(node.getClass()); - if(type != null) { + static byte getSerializableType(final Object node) { + final Byte type = TYPES.get(requireNonNull(node).getClass()); + if (type != null) { + if (type == STRING_TYPE && ((String) node).length() >= STRING_BYTES_LENGTH_THRESHOLD) { + return STRING_BYTES_TYPE; + } return type; - } else if(node instanceof Set){ + } + + if (node instanceof Set) { return BITS_TYPE; } + if (node instanceof YangInstanceIdentifier) { + return YANG_IDENTIFIER_TYPE; + } + throw new IllegalArgumentException("Unknown value type " + node.getClass().getSimpleName()); } }