com.datastax.driver.core

## Class TypeCodec<T>

• ### Nested Class Summary

Nested Classes
Modifier and Type Class and Description
static class  TypeCodec.AbstractCollectionCodec<E,C extends Collection<E>>
Base class for codecs mapping CQL lists and sets to Java collections.
static class  TypeCodec.AbstractMapCodec<K,V>
Base class for codecs mapping CQL maps to a Java Map.
static class  TypeCodec.AbstractTupleCodec<T>
Base class for codecs mapping CQL tuples to Java objects.
static class  TypeCodec.AbstractUDTCodec<T>
Base class for codecs mapping CQL user-defined types (UDTs) to Java objects.
static class  TypeCodec.PrimitiveBooleanCodec
A codec that is capable of handling primitive booleans, thus avoiding the overhead of boxing and unboxing such primitives.
static class  TypeCodec.PrimitiveByteCodec
A codec that is capable of handling primitive bytes, thus avoiding the overhead of boxing and unboxing such primitives.
static class  TypeCodec.PrimitiveDoubleCodec
A codec that is capable of handling primitive doubles, thus avoiding the overhead of boxing and unboxing such primitives.
static class  TypeCodec.PrimitiveFloatCodec
A codec that is capable of handling primitive floats, thus avoiding the overhead of boxing and unboxing such primitives.
static class  TypeCodec.PrimitiveIntCodec
A codec that is capable of handling primitive ints, thus avoiding the overhead of boxing and unboxing such primitives.
static class  TypeCodec.PrimitiveLongCodec
A codec that is capable of handling primitive longs, thus avoiding the overhead of boxing and unboxing such primitives.
static class  TypeCodec.PrimitiveShortCodec
A codec that is capable of handling primitive shorts, thus avoiding the overhead of boxing and unboxing such primitives.
• ### Field Summary

Fields
Modifier and Type Field and Description
protected DataType cqlType
protected com.google.common.reflect.TypeToken<T> javaType
• ### Constructor Summary

Constructors
Modifier Constructor and Description
protected  TypeCodec(DataType cqlType, Class<T> javaClass)
This constructor can only be used for non parameterized types.
protected  TypeCodec(DataType cqlType, com.google.common.reflect.TypeToken<T> javaType)
• ### Method Summary

All Methods
Modifier and Type Method and Description
boolean accepts(Class<?> javaType)
Return true if this codec is capable of serializing the given javaType.
boolean accepts(DataType cqlType)
Return true if this codec is capable of deserializing the given cqlType.
boolean accepts(Object value)
Return true if this codec is capable of serializing the given object.
boolean accepts(com.google.common.reflect.TypeToken javaType)
Return true if this codec is capable of serializing the given javaType.
static TypeCodec<String> ascii()
Return the default codec for the CQL type ascii.
static TypeCodec.PrimitiveLongCodec bigint()
Return the default codec for the CQL type bigint.
static TypeCodec<ByteBuffer> blob()
Return the default codec for the CQL type blob.
static TypeCodec.PrimitiveBooleanCodec cboolean()
Return the default codec for the CQL type boolean.
static TypeCodec.PrimitiveDoubleCodec cdouble()
Return the default codec for the CQL type double.
static TypeCodec.PrimitiveFloatCodec cfloat()
Return the default codec for the CQL type float.
static TypeCodec.PrimitiveIntCodec cint()
Return the default codec for the CQL type int.
static TypeCodec.PrimitiveLongCodec counter()
Return the default codec for the CQL type counter.
static TypeCodec<ByteBuffer> custom(DataType.CustomType type)
Return a newly-created codec for the given CQL custom type.
static TypeCodec<LocalDate> date()
Return the default codec for the CQL type date.
static TypeCodec<BigDecimal> decimal()
Return the default codec for the CQL type decimal.
abstract T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion)
Deserialize the given ByteBuffer instance according to the CQL type handled by this codec.
abstract String format(T value)
Format the given value as a valid CQL literal according to the CQL type handled by this codec.
DataType getCqlType()
Return the CQL type that this codec deserializes from and serializes to.
com.google.common.reflect.TypeToken<T> getJavaType()
Return the Java type that this codec deserializes to and serializes from.
static TypeCodec<InetAddress> inet()
Return the default codec for the CQL type inet.
static <T> TypeCodec<List<T>> list(TypeCodec<T> elementCodec)
Return a newly-created codec for the CQL type list whose element type is determined by the given element codec.
static <K,V> TypeCodec<Map<K,V>> map(TypeCodec<K> keyCodec, TypeCodec<V> valueCodec)
Return a newly-created codec for the CQL type map whose key type and value type are determined by the given codecs.
abstract T parse(String value)
Parse the given CQL literal into an instance of the Java type handled by this codec.
abstract ByteBuffer serialize(T value, ProtocolVersion protocolVersion)
Serialize the given value according to the CQL type handled by this codec.
static <T> TypeCodec<Set<T>> set(TypeCodec<T> elementCodec)
Return a newly-created codec for the CQL type set whose element type is determined by the given element codec.
static TypeCodec.PrimitiveShortCodec smallInt()
Return the default codec for the CQL type smallint.
static TypeCodec.PrimitiveLongCodec time()
Return the default codec for the CQL type time.
static TypeCodec<Date> timestamp()
Return the default codec for the CQL type timestamp.
static TypeCodec<UUID> timeUUID()
Return the default codec for the CQL type timeuuid.
static TypeCodec.PrimitiveByteCodec tinyInt()
Return the default codec for the CQL type tinyint.
String toString()
static TypeCodec<TupleValue> tuple(TupleType type)
Return a newly-created codec for the given CQL tuple type.
static TypeCodec<UDTValue> userType(UserType type)
Return a newly-created codec for the given user-defined CQL type.
static TypeCodec<UUID> uuid()
Return the default codec for the CQL type uuid.
static TypeCodec<String> varchar()
Return the default codec for the CQL type varchar.
static TypeCodec<BigInteger> varint()
Return the default codec for the CQL type varint.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
• ### Field Detail

• #### javaType

protected final com.google.common.reflect.TypeToken<T> javaType
• #### cqlType

protected final DataType cqlType
• ### Constructor Detail

• #### TypeCodec

protected TypeCodec(DataType cqlType,
Class<T> javaClass)
This constructor can only be used for non parameterized types. For parameterized ones, please use TypeCodec(DataType, TypeToken) instead.
Parameters:
javaClass - The Java class this codec serializes from and deserializes to.
• #### TypeCodec

protected TypeCodec(DataType cqlType,
com.google.common.reflect.TypeToken<T> javaType)
• ### Method Detail

• #### cboolean

public static TypeCodec.PrimitiveBooleanCodec cboolean()
Return the default codec for the CQL type boolean. The returned codec maps the CQL type boolean into the Java type Boolean. The returned instance is a singleton.
Returns:
the default codec for CQL type boolean.
• #### tinyInt

public static TypeCodec.PrimitiveByteCodec tinyInt()
Return the default codec for the CQL type tinyint. The returned codec maps the CQL type tinyint into the Java type Byte. The returned instance is a singleton.
Returns:
the default codec for CQL type tinyint.
• #### smallInt

public static TypeCodec.PrimitiveShortCodec smallInt()
Return the default codec for the CQL type smallint. The returned codec maps the CQL type smallint into the Java type Short. The returned instance is a singleton.
Returns:
the default codec for CQL type smallint.
• #### cint

public static TypeCodec.PrimitiveIntCodec cint()
Return the default codec for the CQL type int. The returned codec maps the CQL type int into the Java type Integer. The returned instance is a singleton.
Returns:
the default codec for CQL type int.
• #### bigint

public static TypeCodec.PrimitiveLongCodec bigint()
Return the default codec for the CQL type bigint. The returned codec maps the CQL type bigint into the Java type Long. The returned instance is a singleton.
Returns:
the default codec for CQL type bigint.
• #### counter

public static TypeCodec.PrimitiveLongCodec counter()
Return the default codec for the CQL type counter. The returned codec maps the CQL type counter into the Java type Long. The returned instance is a singleton.
Returns:
the default codec for CQL type counter.
• #### cfloat

public static TypeCodec.PrimitiveFloatCodec cfloat()
Return the default codec for the CQL type float. The returned codec maps the CQL type float into the Java type Float. The returned instance is a singleton.
Returns:
the default codec for CQL type float.
• #### cdouble

public static TypeCodec.PrimitiveDoubleCodec cdouble()
Return the default codec for the CQL type double. The returned codec maps the CQL type double into the Java type Double. The returned instance is a singleton.
Returns:
the default codec for CQL type double.
• #### varint

public static TypeCodec<BigInteger> varint()
Return the default codec for the CQL type varint. The returned codec maps the CQL type varint into the Java type BigInteger. The returned instance is a singleton.
Returns:
the default codec for CQL type varint.
• #### decimal

public static TypeCodec<BigDecimal> decimal()
Return the default codec for the CQL type decimal. The returned codec maps the CQL type decimal into the Java type BigDecimal. The returned instance is a singleton.
Returns:
the default codec for CQL type decimal.
• #### ascii

public static TypeCodec<String> ascii()
Return the default codec for the CQL type ascii. The returned codec maps the CQL type ascii into the Java type String. The returned instance is a singleton.
Returns:
the default codec for CQL type ascii.
• #### varchar

public static TypeCodec<String> varchar()
Return the default codec for the CQL type varchar. The returned codec maps the CQL type varchar into the Java type String. The returned instance is a singleton.
Returns:
the default codec for CQL type varchar.
• #### blob

public static TypeCodec<ByteBuffer> blob()
Return the default codec for the CQL type blob. The returned codec maps the CQL type blob into the Java type ByteBuffer. The returned instance is a singleton.
Returns:
the default codec for CQL type blob.
• #### date

public static TypeCodec<LocalDate> date()
Return the default codec for the CQL type date. The returned codec maps the CQL type date into the Java type LocalDate. The returned instance is a singleton.
Returns:
the default codec for CQL type date.
• #### time

public static TypeCodec.PrimitiveLongCodec time()
Return the default codec for the CQL type time. The returned codec maps the CQL type time into the Java type Long. The returned instance is a singleton.
Returns:
the default codec for CQL type time.
• #### timestamp

public static TypeCodec<Date> timestamp()
Return the default codec for the CQL type timestamp. The returned codec maps the CQL type timestamp into the Java type Date. The returned instance is a singleton.
Returns:
the default codec for CQL type timestamp.
• #### uuid

public static TypeCodec<UUID> uuid()
Return the default codec for the CQL type uuid. The returned codec maps the CQL type uuid into the Java type UUID. The returned instance is a singleton.
Returns:
the default codec for CQL type uuid.
• #### timeUUID

public static TypeCodec<UUID> timeUUID()
Return the default codec for the CQL type timeuuid. The returned codec maps the CQL type timeuuid into the Java type UUID. The returned instance is a singleton.
Returns:
the default codec for CQL type timeuuid.
• #### inet

public static TypeCodec<InetAddress> inet()
Return the default codec for the CQL type inet. The returned codec maps the CQL type inet into the Java type InetAddress. The returned instance is a singleton.
Returns:
the default codec for CQL type inet.
• #### list

public static <T> TypeCodec<List<T>> list(TypeCodec<T> elementCodec)
Return a newly-created codec for the CQL type list whose element type is determined by the given element codec. The returned codec maps the CQL type list into the Java type List. This method does not cache returned instances and returns a newly-allocated object at each invocation.
Parameters:
elementCodec - the codec that will handle elements of this list.
Returns:
A newly-created codec for CQL type list.
• #### set

public static <T> TypeCodec<Set<T>> set(TypeCodec<T> elementCodec)
Return a newly-created codec for the CQL type set whose element type is determined by the given element codec. The returned codec maps the CQL type set into the Java type Set. This method does not cache returned instances and returns a newly-allocated object at each invocation.
Parameters:
elementCodec - the codec that will handle elements of this set.
Returns:
A newly-created codec for CQL type set.
• #### map

public static <K,V> TypeCodec<Map<K,V>> map(TypeCodec<K> keyCodec,
TypeCodec<V> valueCodec)
Return a newly-created codec for the CQL type map whose key type and value type are determined by the given codecs. The returned codec maps the CQL type map into the Java type Map. This method does not cache returned instances and returns a newly-allocated object at each invocation.
Parameters:
keyCodec - the codec that will handle keys of this map.
valueCodec - the codec that will handle values of this map.
Returns:
A newly-created codec for CQL type map.
• #### userType

public static TypeCodec<UDTValue> userType(UserType type)
Return a newly-created codec for the given user-defined CQL type. The returned codec maps the user-defined type into the Java type UDTValue. This method does not cache returned instances and returns a newly-allocated object at each invocation.
Parameters:
type - the user-defined type this codec should handle.
Returns:
A newly-created codec for the given user-defined CQL type.
• #### tuple

public static TypeCodec<TupleValue> tuple(TupleType type)
Return a newly-created codec for the given CQL tuple type. The returned codec maps the tuple type into the Java type TupleValue. This method does not cache returned instances and returns a newly-allocated object at each invocation.
Parameters:
type - the tuple type this codec should handle.
Returns:
A newly-created codec for the given CQL tuple type.
• #### custom

public static TypeCodec<ByteBuffer> custom(DataType.CustomType type)
Return a newly-created codec for the given CQL custom type.

The returned codec maps the custom type into the Java type ByteBuffer, thus providing a (very lightweight) support for Cassandra types that do not have a CQL equivalent.

Note that the returned codec assumes that CQL literals for the given custom type are expressed in binary form as well, e.g. 0xcafebabe. If this is not the case, the returned codec might be unable to parse and format literals for this type. This is notoriously true for types inheriting from org.apache.cassandra.db.marshal.AbstractCompositeType, whose CQL literals are actually expressed as quoted strings.

This method does not cache returned instances and returns a newly-allocated object at each invocation.

Parameters:
type - the custom type this codec should handle.
Returns:
A newly-created codec for the given CQL custom type.
• #### getJavaType

public com.google.common.reflect.TypeToken<T> getJavaType()
Return the Java type that this codec deserializes to and serializes from.
Returns:
The Java type this codec deserializes to and serializes from.
• #### getCqlType

public DataType getCqlType()
Return the CQL type that this codec deserializes from and serializes to.
Returns:
The Java type this codec deserializes from and serializes to.
• #### serialize

public abstract ByteBuffer serialize(T value,
ProtocolVersion protocolVersion)
throws InvalidTypeException
Serialize the given value according to the CQL type handled by this codec.

Implementation notes:

1. Null values should be gracefully handled and no exception should be raised; these should be considered as the equivalent of a NULL CQL value;
2. Codecs for CQL collection types should not permit null elements;
3. Codecs for CQL collection types should treat a null input as the equivalent of an empty collection.
Parameters:
value - An instance of T; may be null.
protocolVersion - the protocol version to use when serializing bytes. In most cases, the proper value to provide for this argument is the value returned by ProtocolOptions.getProtocolVersion() (which is the protocol version in use by the driver).
Returns:
A ByteBuffer instance containing the serialized form of T
Throws:
InvalidTypeException - if the given value does not have the expected type
• #### deserialize

public abstract T deserialize(ByteBuffer bytes,
ProtocolVersion protocolVersion)
throws InvalidTypeException
Deserialize the given ByteBuffer instance according to the CQL type handled by this codec.

Implementation notes:

1. Null or empty buffers should be gracefully handled and no exception should be raised; these should be considered as the equivalent of a NULL CQL value and, in most cases, should map to null or a default value for the corresponding Java type, if applicable;
2. Codecs for CQL collection types should clearly document whether they return immutable collections or not (note that the driver's default collection codecs return mutable collections);
3. Codecs for CQL collection types should avoid returning null; they should return empty collections instead (the driver's default collection codecs all comply with this rule).
4. The provided ByteBuffer should never be consumed by read operations that modify its current position; if necessary, ByteBuffer.duplicate() duplicate} it before consuming.
Parameters:
bytes - A ByteBuffer instance containing the serialized form of T; may be null or empty.
protocolVersion - the protocol version to use when serializing bytes. In most cases, the proper value to provide for this argument is the value returned by ProtocolOptions.getProtocolVersion() (which is the protocol version in use by the driver).
Returns:
An instance of T
Throws:
InvalidTypeException - if the given ByteBuffer instance cannot be deserialized
• #### parse

public abstract T parse(String value)
throws InvalidTypeException
Parse the given CQL literal into an instance of the Java type handled by this codec.

Implementors should take care of unquoting and unescaping the given CQL string where applicable. Null values and empty Strings should be accepted, as well as the string "NULL"; in most cases, implementations should interpret these inputs has equivalent to a null reference.

Implementing this method is not strictly mandatory: internally, the driver only uses it to parse the INITCOND when building the metadata of an aggregate function (and in most cases it will use a built-in codec, unless the INITCOND has a custom type).

Parameters:
value - The CQL string to parse, may be null or empty.
Returns:
An instance of T; may be null on a null input.
Throws:
InvalidTypeException - if the given value cannot be parsed into the expected type
• #### format

public abstract String format(T value)
throws InvalidTypeException
Format the given value as a valid CQL literal according to the CQL type handled by this codec.

Implementors should take care of quoting and escaping the resulting CQL literal where applicable. Null values should be accepted; in most cases, implementations should return the CQL keyword "NULL" for null inputs.

Implementing this method is not strictly mandatory. It is used:

1. in the query builder, when values are inlined in the query string (see BuiltStatement for a detailed explanation of when this happens);
2. in the QueryLogger, if parameter logging is enabled;
3. to format the INITCOND in AggregateMetadata.asCQLQuery(boolean);
4. in the toString() implementation of some objects (UDTValue, TupleValue, and the internal representation of a ROWS response), which may appear in driver logs.
If you choose not to implement this method, you should not throw an exception but instead return a constant string (for example "XxxCodec.format not implemented").
Parameters:
value - An instance of T; may be null.
Returns:
CQL string
Throws:
InvalidTypeException - if the given value does not have the expected type
• #### accepts

public boolean accepts(com.google.common.reflect.TypeToken javaType)
Return true if this codec is capable of serializing the given javaType.

The implementation is invariant with respect to the passed argument (through the usage of TypeToken.equals(Object) and it's strongly recommended not to modify this behavior. This means that a codec will only ever return true for the exact Java type that it has been created for.

If the argument represents a Java primitive type, its wrapper type is considered instead.

Parameters:
javaType - The Java type this codec should serialize from and deserialize to; cannot be null.
Returns:
true if the codec is capable of serializing the given javaType, and false otherwise.
Throws:
NullPointerException - if javaType is null.
• #### accepts

public boolean accepts(Class<?> javaType)
Return true if this codec is capable of serializing the given javaType.

This implementation simply calls accepts(TypeToken).

Parameters:
javaType - The Java type this codec should serialize from and deserialize to; cannot be null.
Returns:
true if the codec is capable of serializing the given javaType, and false otherwise.
Throws:
NullPointerException - if javaType is null.
• #### accepts

public boolean accepts(DataType cqlType)
Return true if this codec is capable of deserializing the given cqlType.
Parameters:
cqlType - The CQL type this codec should deserialize from and serialize to; cannot be null.
Returns:
true if the codec is capable of deserializing the given cqlType, and false otherwise.
Throws:
NullPointerException - if cqlType is null.
• #### accepts

public boolean accepts(Object value)
Return true if this codec is capable of serializing the given object. Note that the object's Java type is inferred from the object' runtime (raw) type, contrary to accepts(TypeToken) which is capable of handling generic types.

This method is intended mostly to be used by the QueryBuilder when no type information is available when the codec is used.

Implementation notes:

1. The default implementation is covariant with respect to the passed argument (through the usage of TypeToken.isAssignableFrom(TypeToken) and it's strongly recommended not to modify this behavior. This means that, by default, a codec will accept any subtype of the Java type that it has been created for.
2. The base implementation provided here can only handle non-parameterized types; codecs handling parameterized types, such as collection types, must override this method and perform some sort of "manual" inspection of the actual type parameters.
3. Similarly, codecs that only accept a partial subset of all possible values must override this method and manually inspect the object to check if it complies or not with the codec's limitations.
Parameters:
value - The Java type this codec should serialize from and deserialize to; cannot be null.
Returns:
true if the codec is capable of serializing the given javaType, and false otherwise.
Throws:
NullPointerException - if value is null.
• #### toString

public String toString()
Overrides:
toString in class Object