Search in sources :

Example 1 with PrimitiveShortCodec

use of com.datastax.oss.driver.api.core.type.codec.PrimitiveShortCodec in project java-driver by datastax.

the class GettableByIndex method getShort.

/**
 * Returns the {@code i}th value as a Java primitive short.
 *
 * <p>By default, this works with CQL type {@code smallint}.
 *
 * <p>Note that, due to its signature, this method cannot return {@code null}. If the CQL value is
 * {@code NULL}, it will return {@code 0}. If this doesn't work for you, either call {@link
 * #isNull(int)} before calling this method, or use {@code get(i, Short.class)} instead.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
default short getShort(int i) {
    DataType cqlType = getType(i);
    TypeCodec<Short> codec = codecRegistry().codecFor(cqlType, Short.class);
    if (codec instanceof PrimitiveShortCodec) {
        return ((PrimitiveShortCodec) codec).decodePrimitive(getBytesUnsafe(i), protocolVersion());
    } else {
        Short value = get(i, codec);
        return value == null ? 0 : value;
    }
}
Also used : PrimitiveShortCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveShortCodec) DataType(com.datastax.oss.driver.api.core.type.DataType)

Example 2 with PrimitiveShortCodec

use of com.datastax.oss.driver.api.core.type.codec.PrimitiveShortCodec in project java-driver by datastax.

the class SettableByIndex method setShort.

/**
 * Sets the {@code i}th value to the provided Java primitive short.
 *
 * <p>By default, this works with CQL type {@code smallint}.
 *
 * <p>To set the value to CQL {@code NULL}, use {@link #setToNull(int)}, or {@code set(i, v,
 * Short.class)}.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
@NonNull
@CheckReturnValue
default SelfT setShort(int i, short v) {
    DataType cqlType = getType(i);
    TypeCodec<Short> codec = codecRegistry().codecFor(cqlType, Short.class);
    return (codec instanceof PrimitiveShortCodec) ? setBytesUnsafe(i, ((PrimitiveShortCodec) codec).encodePrimitive(v, protocolVersion())) : set(i, v, codec);
}
Also used : PrimitiveShortCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveShortCodec) DataType(com.datastax.oss.driver.api.core.type.DataType) CheckReturnValue(edu.umd.cs.findbugs.annotations.CheckReturnValue) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)2 PrimitiveShortCodec (com.datastax.oss.driver.api.core.type.codec.PrimitiveShortCodec)2 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1