public interface CodecRegistry
Implementations MUST provide a default mapping for all CQL types (primitive types, and
all the collections, tuples or user-defined types that can recursively be built from them —
see DataTypes
).
They may also provide additional mappings to other Java types (for use with methods such as
GettableByIndex.get(int, Class)
, SettableByIndex.set(int, Object, Class)
, etc.)
The default implementation returned by the driver also implements MutableCodecRegistry
, and we strongly recommend that custom implementations do as well. The two
interfaces are only separate for backward compatibility, because mutability was introduced in
4.3.0.
Modifier and Type | Field and Description |
---|---|
static CodecRegistry |
DEFAULT
An immutable instance, that only handles built-in driver types (that is, primitive types, and
collections, tuples, and user defined types thereof).
|
Modifier and Type | Method and Description |
---|---|
default <JavaTypeT> |
codecFor(Class<JavaTypeT> javaType)
Shortcut for
codecFor(GenericType.of(javaType)) . |
<JavaTypeT> |
codecFor(DataType cqlType)
Returns a codec to convert the given CQL type to the Java type deemed most appropriate to
represent it.
|
default <JavaTypeT> |
codecFor(DataType cqlType,
Class<JavaTypeT> javaType)
Shortcut for
codecFor(cqlType,
GenericType.of(javaType)) . |
<JavaTypeT> |
codecFor(DataType cqlType,
GenericType<JavaTypeT> javaType)
Returns a codec to handle the conversion between the given types.
|
<JavaTypeT> |
codecFor(DataType cqlType,
JavaTypeT value)
Returns a codec to convert the given Java object to the given CQL type.
|
<JavaTypeT> |
codecFor(GenericType<JavaTypeT> javaType)
Returns a codec to convert the given Java type to the CQL type deemed most appropriate to
represent it.
|
<JavaTypeT> |
codecFor(JavaTypeT value)
Returns a codec to convert the given Java object to the CQL type deemed most appropriate to
represent it.
|
static final CodecRegistry DEFAULT
Note that, due to implementation details, this instance is a MutableCodecRegistry
,
but any attempt to register new codecs
will throw UnsupportedOperationException
.
@NonNull <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull DataType cqlType, @NonNull GenericType<JavaTypeT> javaType)
This is used internally by the driver, in cases where both types are known, for example
row.getString(0)
(Java type inferred from the method,
CQL type known from the row metadata).
The driver's default registry implementation is invariant with regard to the Java
type: for example, if B extends A
and an A<=>int
codec is registered, codecFor(DataTypes.INT, B.class)
will not find that codec. This is because this method
is used internally both for encoding and decoding, and covariance wouldn't work when decoding.
CodecNotFoundException
- if there is no such codec.@NonNull default <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull DataType cqlType, @NonNull Class<JavaTypeT> javaType)
codecFor(cqlType,
GenericType.of(javaType))
.
Implementations may decide to override this method for performance reasons, if they have a way to avoid the overhead of wrapping.
CodecNotFoundException
- if there is no such codec.@NonNull <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull DataType cqlType)
This is used internally by the driver, in cases where the Java type is not explicitly
provided, for example row.getObject(0)
(CQL type known
from the row metadata, Java type unspecified).
The definition of "most appropriate" is left to the appreciation of the registry implementor.
CodecNotFoundException
- if there is no such codec.@NonNull <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull GenericType<JavaTypeT> javaType)
The driver does not use this method. It is provided as a convenience for third-party usage, for example if you were to generate a schema based on a set of Java classes.
The driver's default registry implementation is invariant with regard to the Java
type: for example, if B extends A
and an A<=>int
codec is registered, codecFor(DataTypes.INT, B.class)
will not find that codec. This is because we don't
know whether this method will be used for encoding, decoding, or both.
CodecNotFoundException
- if there is no such codec.@NonNull default <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull Class<JavaTypeT> javaType)
codecFor(GenericType.of(javaType))
.
Implementations may decide to override this method for performance reasons, if they have a way to avoid the overhead of wrapping.
CodecNotFoundException
- if there is no such codec.@NonNull <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull DataType cqlType, @NonNull JavaTypeT value)
This is used internally by the driver when you bulk-set values in a bound statement
, UDT
or tuple
.
Unlike other methods, the driver's default registry implementation is covariant
with regard to the Java type: for example, if B extends A
and an A<=>int
codec
is registered, codecFor(DataTypes.INT, someB)
will find that codec. This is
because this method is always used in encoding scenarios; if a bound statement has a value with
a runtime type of ArrayList<String>
, it should be possible to encode it with a codec
that accepts a List<String>
.
CodecNotFoundException
- if there is no such codec.@NonNull <JavaTypeT> TypeCodec<JavaTypeT> codecFor(@NonNull JavaTypeT value)
This is used internally by the driver, in cases where the CQL type is unknown, for example for simple statement variables (simple statements don't have access to schema metadata).
Unlike other methods, the driver's default registry implementation is covariant
with regard to the Java type: for example, if B extends A
and an A<=>int
codec
is registered, codecFor(someB)
will find that codec. This is because this method
is always used in encoding scenarios; if a simple statement has a value with a runtime type of
ArrayList<String>
, it should be possible to encode it with a codec that accepts a
List<String>
.
Note that, if value
is an empty collection, this method may return a codec that
won't accept JavaTypeT
; but it will encode value
correctly.
CodecNotFoundException
- if there is no such codec.Copyright © 2017–2024. All rights reserved.