InnerT
- The "inner" Java type; must be a driver supported Java type (that is, there must
exist a codec registered for it).OuterT
- The "outer", or target Java type; this codec will handle the mapping to and from
InnerT
and OuterT
.public abstract class MappingCodec<InnerT,OuterT> extends Object implements TypeCodec<OuterT>
TypeCodec
that maps instances of InnerT
, a driver supported Java type, to
instances of a target OuterT
Java type.
This codec can be used to provide support for Java types that are not natively handled by the driver, as long as there is a conversion path to and from another supported Java type.
Modifier and Type | Field and Description |
---|---|
protected TypeCodec<InnerT> |
innerCodec |
protected GenericType<OuterT> |
outerJavaType |
Modifier | Constructor and Description |
---|---|
protected |
MappingCodec(TypeCodec<InnerT> innerCodec,
GenericType<OuterT> outerJavaType)
Creates a new mapping codec providing support for
OuterT based on an existing codec for
InnerT . |
Modifier and Type | Method and Description |
---|---|
OuterT |
decode(ByteBuffer bytes,
ProtocolVersion protocolVersion)
Decodes a value from the binary format of the CQL type handled by this codec.
|
ByteBuffer |
encode(OuterT value,
ProtocolVersion protocolVersion)
Encodes the given value in the binary format of the CQL type handled by this codec.
|
String |
format(OuterT value)
Formats the given value as a valid CQL literal according to the CQL type handled by this codec.
|
DataType |
getCqlType() |
GenericType<InnerT> |
getInnerJavaType() |
GenericType<OuterT> |
getJavaType() |
protected abstract OuterT |
innerToOuter(InnerT value)
Converts from an instance of the inner Java type to an instance of the outer Java type.
|
protected abstract InnerT |
outerToInner(OuterT value)
Converts from an instance of the outer Java type to an instance of the inner Java type.
|
OuterT |
parse(String value)
Parse the given CQL literal into an instance of the Java type handled by this codec.
|
protected final GenericType<OuterT> outerJavaType
protected MappingCodec(@NonNull TypeCodec<InnerT> innerCodec, @NonNull GenericType<OuterT> outerJavaType)
OuterT
based on an existing codec for
InnerT
.innerCodec
- The inner codec to use to handle instances of InnerT; must not be null.outerJavaType
- The outer Java type; must not be null.@NonNull public GenericType<OuterT> getJavaType()
getJavaType
in interface TypeCodec<OuterT>
OuterT
.public GenericType<InnerT> getInnerJavaType()
InnerT
.@NonNull public DataType getCqlType()
getCqlType
in interface TypeCodec<OuterT>
public ByteBuffer encode(OuterT value, @NonNull ProtocolVersion protocolVersion)
TypeCodec
null
input as the equivalent of an
empty collection.
public OuterT decode(ByteBuffer bytes, @NonNull ProtocolVersion protocolVersion)
TypeCodec
null
or a default value for the corresponding Java type, if
applicable;
null
; they should return
empty collections instead (the driver's default collection codecs all comply with this
rule);
ByteBuffer
should never be consumed by read operations that modify
its current position; if necessary, ByteBuffer.duplicate()
duplicate} it before
consuming.
@NonNull public String format(OuterT value)
TypeCodec
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:
AggregateMetadata.describe(boolean)
;
toString()
representation of some driver objects (such as UdtValue
and TupleValue
), which is only used in driver logs;
QueryBuilder#literal(Object,
CodecRegistry)
and QueryBuilder#literal(Object, TypeCodec)
).
public OuterT parse(String value)
TypeCodec
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).
If you choose not to implement this method, don't throw an exception but instead return
null
.
@Nullable protected abstract OuterT innerToOuter(@Nullable InnerT value)
value
- The value to convert; may be null.@Nullable protected abstract InnerT outerToInner(@Nullable OuterT value)
value
- The value to convert; may be null.Copyright © 2017–2020. All rights reserved.