Package com.datastax.driver.core.exceptions

Examples of com.datastax.driver.core.exceptions.InvalidTypeException


    @Override
    @SuppressWarnings("unchecked")
    public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass) {
        DataType type = getType(i);
        if (type.getName() != DataType.Name.MAP)
            throw new InvalidTypeException(String.format("Column %s is not of map type", getName(i)));

        Class<?> expectedKeysClass = type.getTypeArguments().get(0).getName().javaType;
        Class<?> expectedValuesClass = type.getTypeArguments().get(1).getName().javaType;
        if (!keysClass.isAssignableFrom(expectedKeysClass) || !valuesClass.isAssignableFrom(expectedValuesClass))
            throw new InvalidTypeException(String.format("Column %s is a map of %s->%s (CQL type %s), cannot be retrieve as a map of %s->%s", getName(i), expectedKeysClass, expectedValuesClass, type, keysClass, valuesClass));

        ByteBuffer value = getValue(i);
        if (value == null)
            return Collections.<K, V>emptyMap();
View Full Code Here


    @Override
    @SuppressWarnings("unchecked")
    public UDTValue getUDTValue(int i) {
        DataType type = getType(i);
        if (type.getName() != DataType.Name.UDT)
            throw new InvalidTypeException(String.format("Column %s is not a UDT", getName(i)));

        ByteBuffer value = getValue(i);
        if (value == null || value.remaining() == 0)
            return null;
View Full Code Here

    @Override
    @SuppressWarnings("unchecked")
    public TupleValue getTupleValue(int i) {
        DataType type = getType(i);
        if (type.getName() != DataType.Name.TUPLE)
            throw new InvalidTypeException(String.format("Column %s is not a tuple", getName(i)));

        ByteBuffer value = getValue(i);
        if (value == null || value.remaining() == 0)
            return null;
View Full Code Here

        }

        @Override
        public String parse(String value) {
            if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'')
                throw new InvalidTypeException("text values must enclosed by a single quotes");

            return value.substring(1, value.length() - 1).replace("''", "'");
        }
View Full Code Here

        @Override
        public Long parse(String value) {
            try {
                return Long.parseLong(value);
            } catch (NumberFormatException e) {
                throw new InvalidTypeException(String.format("Cannot parse 64-bits long value from \"%s\"", value));
            }
        }
View Full Code Here

            return deserializeNoBoxing(bytes);
        }

        public long deserializeNoBoxing(ByteBuffer bytes) {
            if (bytes.remaining() != 8)
                throw new InvalidTypeException("Invalid 64-bits long value, expecting 8 bytes but got " + bytes.remaining());

            return bytes.getLong(bytes.position());
        }
View Full Code Here

     */
    public ByteBuffer serialize(Object value, ProtocolVersion protocolVersion) {
        Class<?> providedClass = value.getClass();
        Class<?> expectedClass = asJavaClass();
        if (!expectedClass.isAssignableFrom(providedClass))
            throw new InvalidTypeException(String.format("Invalid value for CQL type %s, expecting %s but %s provided", toString(), expectedClass, providedClass));

        try {
            return codec(protocolVersion).serialize(value);
        } catch (ClassCastException e) {
            // With collections, the element type has not been checked, so it can throw
            throw new InvalidTypeException("Invalid type for collection element: " + e.getMessage());
        }
    }
View Full Code Here

            return customClassName;
        }

        @Override
        public Object parse(String value) {
            throw new InvalidTypeException("Cannot parse values of custom types");
        }
View Full Code Here

            throw new InvalidTypeException("Cannot parse values of custom types");
        }

        @Override
        public String format(Object value) {
            throw new InvalidTypeException("Cannot format values of custom types");
        }
View Full Code Here

    UDTValue parseValue(String value) {
        UDTValue v = newValue();

        int idx = ParseUtils.skipSpaces(value, 0);
        if (value.charAt(idx++) != '{')
            throw new InvalidTypeException(String.format("Cannot parse UDT value from \"%s\", at character %d expecting '{' but got '%c'", value, idx, value.charAt(idx)));

        idx = ParseUtils.skipSpaces(value, idx);

        if (value.charAt(idx) == '}')
            return v;

        while (idx < value.length()) {

            int n;
            try {
                n = ParseUtils.skipCQLId(value, idx);
            } catch (IllegalArgumentException e) {
                throw new InvalidTypeException(String.format("Cannot parse UDT value from \"%s\", cannot parse a CQL identifier at character %d", value, idx), e);
            }
            String name = value.substring(idx, n);
            idx = n;

            if (!contains(name))
                throw new InvalidTypeException(String.format("Unknown field %s in value \"%s\"", name, value));

            idx = ParseUtils.skipSpaces(value, idx);
            if (value.charAt(idx++) != ':')
                throw new InvalidTypeException(String.format("Cannot parse UDT value from \"%s\", at character %d expecting ':' but got '%c'", value, idx, value.charAt(idx)));
            idx = ParseUtils.skipSpaces(value, idx);

            try {
                n = ParseUtils.skipCQLValue(value, idx);
            } catch (IllegalArgumentException e) {
                throw new InvalidTypeException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at character %d", value, idx), e);
            }

            DataType dt = getFieldType(name);
            v.setBytesUnsafe(name, dt.serialize(dt.parse(value.substring(idx, n)), ProtocolVersion.V3));
            idx = n;

            idx = ParseUtils.skipSpaces(value, idx);
            if (value.charAt(idx) == '}')
                return v;
            if (value.charAt(idx) != ',')
                throw new InvalidTypeException(String.format("Cannot parse UDT value from \"%s\", at character %d expecting ',' but got '%c'", value, idx, value.charAt(idx)));
            ++idx; // skip ','

            idx = ParseUtils.skipSpaces(value, idx);
        }
        throw new InvalidTypeException(String.format("Malformed UDT value \"%s\", missing closing '}'", value));
    }
View Full Code Here

TOP

Related Classes of com.datastax.driver.core.exceptions.InvalidTypeException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.