Search in sources :

Example 1 with PrimitiveBooleanCodec

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

the class SettableByIndex method setBoolean.

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

Example 2 with PrimitiveBooleanCodec

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

the class GettableByIndex method getBoolean.

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

Aggregations

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