Search in sources :

Example 1 with PrimitiveIntCodec

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

the class SettableByIndex method setInt.

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

Example 2 with PrimitiveIntCodec

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

the class GettableByIndex method getInt.

/**
 * Returns the {@code i}th value as a Java primitive integer.
 *
 * <p>By default, this works with CQL type {@code int}.
 *
 * <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, Integer.class)} instead.
 *
 * @throws IndexOutOfBoundsException if the index is invalid.
 */
default int getInt(int i) {
    DataType cqlType = getType(i);
    TypeCodec<Integer> codec = codecRegistry().codecFor(cqlType, Integer.class);
    if (codec instanceof PrimitiveIntCodec) {
        return ((PrimitiveIntCodec) codec).decodePrimitive(getBytesUnsafe(i), protocolVersion());
    } else {
        Integer value = get(i, codec);
        return value == null ? 0 : value;
    }
}
Also used : BigInteger(java.math.BigInteger) DataType(com.datastax.oss.driver.api.core.type.DataType) PrimitiveIntCodec(com.datastax.oss.driver.api.core.type.codec.PrimitiveIntCodec)

Aggregations

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