T
- The codec's Java typepublic abstract class TypeCodec<T> extends Object
CQL type
and a given Java Type
.
serialize(Object,ProtocolVersion)
: used to serialize from the codec's Java type to a
ByteBuffer
instance corresponding to the codec's CQL type;deserialize(ByteBuffer,ProtocolVersion)
: used to deserialize a ByteBuffer
instance
corresponding to the codec's CQL type to the codec's Java type.format(Object)
: formats the Java type handled by the codec as a CQL string;parse(String)
; parses a CQL string into the Java type handled by the codec.accepts(DataType)
: returns true if the codec can deserialize the given CQL type;accepts(TypeToken)
: returns true if the codec can serialize the given Java type;accepts(Object)
; returns true if the codec can serialize the given object.null
values and empty ByteBuffers
(i.e. Buffer.remaining()
== 0
) in a reasonable way;
usually, NULL
CQL values should map to null
references, but exceptions exist;
e.g. for varchar types, a NULL
CQL value maps to a null
reference,
whereas an empty buffer maps to an empty String. For collection types, it is also admitted that
NULL
CQL values map to empty Java collections instead of null
references.
In any case, the codec's behavior in respect to null
values and empty ByteBuffers
should be clearly documented.TypeCodec.BooleanCodec
for an example).Modifier and Type | Class and Description |
---|---|
static class |
TypeCodec.AbstractUUIDCodec
Base class for codecs handling CQL UUID types such as
DataType.uuid() and DataType.timeuuid() . |
static class |
TypeCodec.AsciiCodec
This codec maps a CQL
DataType.ascii() to a Java String . |
static class |
TypeCodec.BigintCodec
This codec maps a CQL
DataType.bigint() to a Java Long . |
static class |
TypeCodec.BlobCodec
This codec maps a CQL
DataType.blob() to a Java ByteBuffer . |
static class |
TypeCodec.BooleanCodec
This codec maps a CQL
DataType.cboolean() to a Java Boolean . |
static class |
TypeCodec.CollectionCodec<E,C extends Collection<E>>
Base class for codecs handling CQL collection types such as
DataType.list(DataType)
or DataType.set(DataType) . |
static class |
TypeCodec.CounterCodec
This codec maps a CQL
DataType.counter() to a Java Long . |
static class |
TypeCodec.CustomCodec
This codec maps a CQL
DataType.custom(String) to a Java ByteBuffer . |
static class |
TypeCodec.DateCodec
This codec maps a CQL
DataType.date() to the custom LocalDate class. |
static class |
TypeCodec.DecimalCodec
This codec maps a CQL
DataType.decimal() to a Java BigDecimal . |
static class |
TypeCodec.DoubleCodec
This codec maps a CQL
DataType.cdouble() to a Java Double . |
static class |
TypeCodec.EnumIntCodec<E extends Enum<E>>
A codec that serializes
Enum instances as CQL int s
representing their ordinal values as returned by Enum.ordinal() . |
static class |
TypeCodec.EnumStringCodec<E extends Enum<E>>
A codec that serializes
Enum instances as CQL varchar s
representing their programmatic names as returned by Enum.name() . |
static class |
TypeCodec.FloatCodec
This codec maps a CQL
DataType.cfloat() to a Java Float . |
static class |
TypeCodec.InetCodec
This codec maps a CQL
DataType.inet() to a Java InetAddress . |
static class |
TypeCodec.IntCodec
This codec maps a CQL
DataType.cint() to a Java Integer . |
static class |
TypeCodec.ListCodec<T>
|
static class |
TypeCodec.LongCodec
Base class for codecs handling CQL 8-byte integer types such as
DataType.bigint() ,
DataType.counter() or DataType.time() . |
static class |
TypeCodec.MapCodec<K,V>
|
static class |
TypeCodec.MappingCodec<T,U>
An abstract TypeCodec that maps a Java Pojo to another Java object
that can in turn be serialized into a CQL type.
|
static interface |
TypeCodec.PrimitiveBooleanCodec
An interface for codecs that are capable of handling primitive booleans,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static interface |
TypeCodec.PrimitiveByteCodec
An interface for codecs that are capable of handling primitive bytes,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static interface |
TypeCodec.PrimitiveDoubleCodec
An interface for codecs that are capable of handling primitive doubles,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static interface |
TypeCodec.PrimitiveFloatCodec
An interface for codecs that are capable of handling primitive floats,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static interface |
TypeCodec.PrimitiveIntCodec
An interface for codecs that are capable of handling primitive ints,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static interface |
TypeCodec.PrimitiveLongCodec
An interface for codecs that are capable of handling primitive longs,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static interface |
TypeCodec.PrimitiveShortCodec
An interface for codecs that are capable of handling primitive shorts,
thus avoiding the overhead of boxing and unboxing such primitives.
|
static class |
TypeCodec.SetCodec<T>
|
static class |
TypeCodec.SmallIntCodec
This codec maps a CQL
DataType.smallint() to a Java Short . |
static class |
TypeCodec.StringCodec
Base class for codecs handling CQL string types such as
DataType.varchar() ,
DataType.text() or DataType.ascii() . |
static class |
TypeCodec.StringParsingCodec<T>
An abstract TypeCodec that actually stores objects as serialized strings.
|
static class |
TypeCodec.TimeCodec
This codec maps a CQL
DataType.time() to a Java Long . |
static class |
TypeCodec.TimestampCodec
This codec maps a CQL
DataType.timestamp() to a Java Date . |
static class |
TypeCodec.TimeUUIDCodec
This codec maps a CQL
DataType.timeuuid() to a Java UUID . |
static class |
TypeCodec.TinyIntCodec
This codec maps a CQL
DataType.tinyint() to a Java Byte . |
static class |
TypeCodec.TupleCodec
This codec maps a CQL
TupleType to a TupleValue . |
static class |
TypeCodec.UDTCodec
|
static class |
TypeCodec.UUIDCodec
This codec maps a CQL
DataType.uuid() to a Java UUID . |
static class |
TypeCodec.VarcharCodec
This codec maps a CQL
DataType.varchar() to a Java String . |
static class |
TypeCodec.VarintCodec
This codec maps a CQL
DataType.varint() to a Java BigInteger . |
Modifier and Type | Field and Description |
---|---|
protected DataType |
cqlType |
protected com.google.common.reflect.TypeToken<T> |
javaType |
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) |
Modifier and Type | Method and Description |
---|---|
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 . |
abstract T |
deserialize(ByteBuffer bytes,
ProtocolVersion protocolVersion)
Deserialize the given
ByteBuffer instance according to the CQL type
handled by this codec. |
boolean |
equals(Object o) |
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.
|
int |
hashCode() |
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.
|
String |
toString() |
protected final com.google.common.reflect.TypeToken<T> javaType
protected final DataType cqlType
protected TypeCodec(DataType cqlType, Class<T> javaClass)
TypeCodec(DataType, TypeToken)
instead.javaClass
- The Java class this codec serializes from and deserializes to.public com.google.common.reflect.TypeToken<T> getJavaType()
public DataType getCqlType()
public abstract ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException
Implementation notes:
null
input as
the equivalent of an empty collection.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).ByteBuffer
instance containing the serialized form of TInvalidTypeException
- if the given value does not have the expected typepublic abstract T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException
ByteBuffer
instance according to the CQL type
handled by this codec.
Implementation notes:
null
or a default value for the corresponding Java type, if applicable;null
;
they should return empty collections instead.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).InvalidTypeException
- if the given ByteBuffer
instance cannot be deserializedpublic abstract T parse(String value) throws InvalidTypeException
"NULL"
;
in most cases, implementations should interpret these inputs has equivalent to a null
reference.value
- The CQL string to parse, may be null
or empty.null
on a null input
.InvalidTypeException
- if the given value cannot be parsed into the expected typepublic abstract String format(T value) throws InvalidTypeException
"NULL"
for null
inputs.value
- An instance of T; may be null
.InvalidTypeException
- if the given value does not have the expected typepublic boolean accepts(com.google.common.reflect.TypeToken javaType)
true
if this codec is capable of serializing
the given javaType
.javaType
- The Java type this codec should serialize from and deserialize to; cannot be null
.true
if the codec is capable of serializing
the given javaType
, and false
otherwise.NullPointerException
- if javaType
is null
.public boolean accepts(DataType cqlType)
true
if this codec is capable of deserializing
the given cqlType
.cqlType
- The CQL type this codec should deserialize from and serialize to; cannot be null
.true
if the codec is capable of deserializing
the given cqlType
, and false
otherwise.NullPointerException
- if cqlType
is null
.public boolean accepts(Object value)
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:
TypeCodec.TimeUUIDCodec
or TypeCodec.AsciiCodec
, must override
this method and manually inspect the object to check if it
complies or not with the codec's limitations.value
- The Java type this codec should serialize from and deserialize to; cannot be null
.true
if the codec is capable of serializing
the given javaType
, and false
otherwise.NullPointerException
- if value
is null
.Copyright © 2012–2015. All rights reserved.