public class ExtraTypeCodecs extends Object
Modifier and Type | Field and Description |
---|---|
static TypeCodec<byte[]> |
BLOB_TO_ARRAY
A codec that maps CQL type
blob to Java's byte[] . |
static TypeCodec<boolean[]> |
BOOLEAN_LIST_TO_ARRAY
A codec that maps CQL type
list<boolean> to Java's boolean[] . |
static TypeCodec<byte[]> |
BYTE_LIST_TO_ARRAY
A codec that maps CQL type
list<tinyint> to Java's byte[] . |
static TypeCodec<double[]> |
DOUBLE_LIST_TO_ARRAY
A codec that maps CQL type
list<double> to Java's double[] . |
static TypeCodec<float[]> |
FLOAT_LIST_TO_ARRAY
A codec that maps CQL type
list<float> to Java's float[] . |
static TypeCodec<int[]> |
INT_LIST_TO_ARRAY
A codec that maps CQL type
list<int> to Java's int[] . |
static TypeCodec<LocalDateTime> |
LOCAL_TIMESTAMP_SYSTEM
A codec that maps CQL type
timestamp to Java's LocalDateTime , using the
system's default time zone. |
static TypeCodec<LocalDateTime> |
LOCAL_TIMESTAMP_UTC
A codec that maps CQL type
timestamp to Java's LocalDateTime , using the UTC
time zone. |
static TypeCodec<long[]> |
LONG_LIST_TO_ARRAY
A codec that maps CQL type
list<bigint> to Java's long[] . |
static TypeCodec<short[]> |
SHORT_LIST_TO_ARRAY
A codec that maps CQL type
list<smallint> to Java's short[] . |
static PrimitiveLongCodec |
TIMESTAMP_MILLIS_SYSTEM
A codec that maps CQL type
timestamp to Java's long , representing the number of
milliseconds since the Epoch, using the system's default time zone to parse and format CQL
literals. |
static PrimitiveLongCodec |
TIMESTAMP_MILLIS_UTC
A codec that maps CQL type
timestamp to Java's long , representing the number of
milliseconds since the Epoch, using the UTC time zone to parse and format CQL literals. |
static TypeCodec<Instant> |
TIMESTAMP_UTC
A codec that maps CQL type
timestamp to Java's Instant , using the UTC time zone
to parse and format CQL literals. |
static TypeCodec<ZonedDateTime> |
ZONED_TIMESTAMP_PERSISTED
A codec that maps CQL type
tuple<timestamp,text> to Java's ZonedDateTime ,
providing a pattern for maintaining timezone information in Cassandra. |
static TypeCodec<ZonedDateTime> |
ZONED_TIMESTAMP_SYSTEM
A codec that maps CQL type
timestamp to Java's ZonedDateTime , using the
system's default time zone. |
static TypeCodec<ZonedDateTime> |
ZONED_TIMESTAMP_UTC
A codec that maps CQL type
timestamp to Java's ZonedDateTime , using the UTC
time zone. |
Constructor and Description |
---|
ExtraTypeCodecs() |
Modifier and Type | Method and Description |
---|---|
static <EnumT extends Enum<EnumT>> |
enumNamesOf(Class<EnumT> enumClass)
Builds a new codec that maps CQL type
text to a Java Enum, according to its constants'
programmatic names. |
static <EnumT extends Enum<EnumT>> |
enumOrdinalsOf(Class<EnumT> enumClass)
Builds a new codec that maps CQL type
int to a Java Enum, according to its constants'
ordinals (STRONGLY discouraged, see explanations below). |
static <T> TypeCodec<T> |
json(Class<T> javaType)
Builds a new codec that maps CQL type
text to the given Java type, using JSON
serialization with a default Jackson mapper. |
static <T> TypeCodec<T> |
json(Class<T> javaType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Builds a new codec that maps CQL type
text to the given Java type, using JSON
serialization with the provided Jackson mapper. |
static <T> TypeCodec<T> |
json(GenericType<T> javaType)
Builds a new codec that maps CQL type
text to the given Java type, using JSON
serialization with a default Jackson mapper. |
static <T> TypeCodec<T> |
json(GenericType<T> javaType,
com.fasterxml.jackson.databind.ObjectMapper objectMapper)
Builds a new codec that maps CQL type
text to the given Java type, using JSON
serialization with the provided Jackson mapper. |
static <T> TypeCodec<T[]> |
listToArrayOf(TypeCodec<T> elementCodec)
Builds a new codec that maps a CQL list to a Java array.
|
static TypeCodec<LocalDateTime> |
localTimestampAt(ZoneId timeZone)
Builds a new codec that maps CQL type
timestamp to Java's LocalDateTime . |
static <T> TypeCodec<Optional<T>> |
optionalOf(TypeCodec<T> innerCodec)
Builds a new codec that wraps another codec's Java type into
Optional instances
(mapping CQL null to Optional.empty() ). |
static TypeCodec<Instant> |
timestampAt(ZoneId timeZone)
Builds a new codec that maps CQL type
timestamp to Java's Instant , using the
given time zone to parse and format CQL literals. |
static PrimitiveLongCodec |
timestampMillisAt(ZoneId timeZone)
Builds a new codec that maps CQL type
timestamp to Java's long , representing
the number of milliseconds since the Epoch, using the given time zone to parse and format CQL
literals. |
static TypeCodec<ZonedDateTime> |
zonedTimestampAt(ZoneId timeZone)
Builds a new codec that maps CQL type
timestamp to Java's ZonedDateTime . |
public static final TypeCodec<Instant> TIMESTAMP_UTC
timestamp
to Java's Instant
, using the UTC time zone
to parse and format CQL literals.
This codec uses ZoneOffset.UTC
as its source of time zone information when
formatting values as CQL literals, or parsing CQL literals that do not have any time zone
indication. Note that this only applies to the TypeCodec.format(Object)
and TypeCodec.parse(String)
methods; regular encoding and decoding, like setting a value on a
bound statement or reading a column from a row, are not affected by the time zone.
If you need a different time zone, consider other constants in this class, or call timestampAt(ZoneId)
instead.
TypeCodecs.TIMESTAMP
,
timestampAt(ZoneId)
public static final PrimitiveLongCodec TIMESTAMP_MILLIS_SYSTEM
timestamp
to Java's long
, representing the number of
milliseconds since the Epoch, using the system's default time zone to parse and format CQL
literals.
This codec uses the system's default time zone as its
source of time zone information when formatting values as CQL literals, or parsing CQL literals
that do not have any time zone indication. Note that this only applies to the TypeCodec.format(Object)
and TypeCodec.parse(String)
methods; regular encoding and
decoding, like setting a value on a bound statement or reading a column from a row, are not
affected by the time zone.
If you need a different time zone, consider other constants in this class, or call timestampMillisAt(ZoneId)
instead.
This codec can serve as a replacement for the driver's built-in timestamp codec, when application code prefers to deal with raw
milliseconds than with Instant
instances.
TIMESTAMP_MILLIS_UTC
,
timestampMillisAt(ZoneId)
public static final PrimitiveLongCodec TIMESTAMP_MILLIS_UTC
timestamp
to Java's long
, representing the number of
milliseconds since the Epoch, using the UTC time zone to parse and format CQL literals.
This codec uses ZoneOffset.UTC
as its source of time zone information when
formatting values as CQL literals, or parsing CQL literals that do not have any time zone
indication. Note that this only applies to the TypeCodec.format(Object)
and TypeCodec.parse(String)
methods; regular encoding and decoding, like setting a value on a
bound statement or reading a column from a row, are not affected by the time zone.
If you need a different time zone, consider other constants in this class, or call timestampMillisAt(ZoneId)
instead.
This codec can serve as a replacement for the driver's built-in timestamp codec, when application code prefers to deal with raw
milliseconds than with Instant
instances.
TIMESTAMP_MILLIS_SYSTEM
,
timestampMillisAt(ZoneId)
public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_SYSTEM
timestamp
to Java's ZonedDateTime
, using the
system's default time zone.
This codec uses the system's default time zone as its
source of time zone information when encoding or decoding. If you need a different time zone,
consider using other constants in this class, or call zonedTimestampAt(ZoneId)
instead.
Note that CQL type timestamp
type does not store any time zone; this codec is
provided merely as a convenience for users that need to deal with zoned timestamps in their
applications.
public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_UTC
timestamp
to Java's ZonedDateTime
, using the UTC
time zone.
This codec uses ZoneOffset.UTC
as its source of time zone information when encoding
or decoding. If you need a different time zone, consider using other constants in this class,
or call zonedTimestampAt(ZoneId)
instead.
Note that CQL type timestamp
type does not store any time zone; this codec is
provided merely as a convenience for users that need to deal with zoned timestamps in their
applications.
public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_PERSISTED
tuple<timestamp,text>
to Java's ZonedDateTime
,
providing a pattern for maintaining timezone information in Cassandra.
Since CQL type timestamp
does not store any time zone, it is persisted separately in
the text
field of the tuple, and so when the value is read back the original timezone
it was written with is preserved.
public static final TypeCodec<LocalDateTime> LOCAL_TIMESTAMP_SYSTEM
timestamp
to Java's LocalDateTime
, using the
system's default time zone.
This codec uses the system's default time zone as its
source of time zone information when encoding or decoding. If you need a different time zone,
consider using other constants in this class, or call localTimestampAt(ZoneId)
instead.
Note that CQL type timestamp
does not store any time zone; this codec is provided
merely as a convenience for users that need to deal with local date-times in their
applications.
LOCAL_TIMESTAMP_UTC
,
localTimestampAt(ZoneId)
public static final TypeCodec<LocalDateTime> LOCAL_TIMESTAMP_UTC
timestamp
to Java's LocalDateTime
, using the UTC
time zone.
This codec uses ZoneOffset.UTC
as its source of time zone information when encoding
or decoding. If you need a different time zone, consider using other constants in this class,
or call localTimestampAt(ZoneId)
instead.
Note that CQL type timestamp
does not store any time zone; this codec is provided
merely as a convenience for users that need to deal with local date-times in their
applications.
LOCAL_TIMESTAMP_SYSTEM
,
localTimestampAt(ZoneId)
public static final TypeCodec<byte[]> BLOB_TO_ARRAY
blob
to Java's byte[]
.
If you are looking for a codec mapping CQL type blob
to the Java type ByteBuffer
, you should use TypeCodecs.BLOB
instead.
If you are looking for a codec mapping CQL type list<tinyint
to the Java type byte[]
, you should use BYTE_LIST_TO_ARRAY
instead.
TypeCodecs.BLOB
,
BYTE_LIST_TO_ARRAY
public static final TypeCodec<boolean[]> BOOLEAN_LIST_TO_ARRAY
list<boolean>
to Java's boolean[]
.
Note that this codec is designed for performance and converts CQL lists directly to
boolean[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive boolean
values; it also instantiates arrays without the need for an intermediary Java List
object.
public static final TypeCodec<byte[]> BYTE_LIST_TO_ARRAY
list<tinyint>
to Java's byte[]
.
This codec is not suitable for reading CQL blobs as byte arrays. If you are looking for a
codec for the CQL type blob
, you should use TypeCodecs.BLOB
or BLOB_TO_ARRAY
instead.
Note that this codec is designed for performance and converts CQL lists directly to
byte[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive byte
values; it also instantiates arrays without the need for an intermediary Java List
object.
TypeCodecs.BLOB
,
BLOB_TO_ARRAY
public static final TypeCodec<short[]> SHORT_LIST_TO_ARRAY
list<smallint>
to Java's short[]
.
Note that this codec is designed for performance and converts CQL lists directly to
short[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive short
values; it also instantiates arrays without the need for an intermediary Java List
object.
public static final TypeCodec<int[]> INT_LIST_TO_ARRAY
list<int>
to Java's int[]
.
Note that this codec is designed for performance and converts CQL lists directly to
int[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive int
values; it also instantiates arrays without the need for an intermediary Java List
object.
public static final TypeCodec<long[]> LONG_LIST_TO_ARRAY
list<bigint>
to Java's long[]
.
Note that this codec is designed for performance and converts CQL lists directly to
long[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive long
values; it also instantiates arrays without the need for an intermediary Java List
object.
public static final TypeCodec<float[]> FLOAT_LIST_TO_ARRAY
list<float>
to Java's float[]
.
Note that this codec is designed for performance and converts CQL lists directly to
float[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive float
values; it also instantiates arrays without the need for an intermediary Java List
object.
public static final TypeCodec<double[]> DOUBLE_LIST_TO_ARRAY
list<double>
to Java's double[]
.
Note that this codec is designed for performance and converts CQL lists directly to
double[]
, thus avoiding any unnecessary boxing and unboxing of Java primitive double
values; it also instantiates arrays without the need for an intermediary Java List
object.
@NonNull public static TypeCodec<Instant> timestampAt(@NonNull ZoneId timeZone)
timestamp
to Java's Instant
, using the
given time zone to parse and format CQL literals.
This codec uses the supplied ZoneId
as its source of time zone information when
formatting values as CQL literals, or parsing CQL literals that do not have any time zone
indication. Note that this only applies to the TypeCodec.format(Object)
and TypeCodec.parse(String)
methods; regular encoding and decoding, like setting a value on a
bound statement or reading a column from a row, are not affected by the time zone.
TypeCodecs.TIMESTAMP
,
TIMESTAMP_UTC
@NonNull public static PrimitiveLongCodec timestampMillisAt(@NonNull ZoneId timeZone)
timestamp
to Java's long
, representing
the number of milliseconds since the Epoch, using the given time zone to parse and format CQL
literals.
This codec uses the supplied ZoneId
as its source of time zone information when
formatting values as CQL literals, or parsing CQL literals that do not have any time zone
indication. Note that this only applies to the TypeCodec.format(Object)
and TypeCodec.parse(String)
methods; regular encoding and decoding, like setting a value on a
bound statement or reading a column from a row, are not affected by the time zone.
This codec can serve as a replacement for the driver's built-in timestamp codec, when application code prefers to deal with raw
milliseconds than with Instant
instances.
TIMESTAMP_MILLIS_SYSTEM
,
TIMESTAMP_MILLIS_UTC
@NonNull public static TypeCodec<ZonedDateTime> zonedTimestampAt(@NonNull ZoneId timeZone)
timestamp
to Java's ZonedDateTime
.
This codec uses the supplied ZoneId
as its source of time zone information when
encoding or decoding.
Note that CQL type timestamp
does not store any time zone; the codecs created by
this method are provided merely as a convenience for users that need to deal with zoned
timestamps in their applications.
@NonNull public static TypeCodec<LocalDateTime> localTimestampAt(@NonNull ZoneId timeZone)
timestamp
to Java's LocalDateTime
.
This codec uses the supplied ZoneId
as its source of time zone information when
encoding or decoding.
Note that CQL type timestamp
does not store any time zone; the codecs created by
this method are provided merely as a convenience for users that need to deal with local
date-times in their applications.
LOCAL_TIMESTAMP_UTC
,
localTimestampAt(ZoneId)
@NonNull public static <T> TypeCodec<T[]> listToArrayOf(@NonNull TypeCodec<T> elementCodec)
This method is not suitable for Java primitive arrays. Use BOOLEAN_LIST_TO_ARRAY
, BYTE_LIST_TO_ARRAY
, SHORT_LIST_TO_ARRAY
, INT_LIST_TO_ARRAY
, LONG_LIST_TO_ARRAY
, FLOAT_LIST_TO_ARRAY
or DOUBLE_LIST_TO_ARRAY
instead.
@NonNull public static <EnumT extends Enum<EnumT>> TypeCodec<EnumT> enumOrdinalsOf(@NonNull Class<EnumT> enumClass)
int
to a Java Enum, according to its constants'
ordinals (STRONGLY discouraged, see explanations below).
This method is provided for compatibility with driver 3, but we strongly recommend against it. Relying on enum ordinals is a bad practice: any reordering of the enum constants, or insertion of a new constant before the end, will change the ordinals. The codec will keep working, but start inserting different codes and corrupting your data.
enumNamesOf(Class)
is a safer alternative, as it is not dependent on the constant
order. If you still want to use integer codes for storage efficiency, we recommend implementing
an explicit mapping (for example with a toCode()
method on your enum type). It is then
fairly straightforward to implement a codec with MappingCodec
, using TypeCodecs.INT
as the "inner" codec.
@NonNull public static <EnumT extends Enum<EnumT>> TypeCodec<EnumT> enumNamesOf(@NonNull Class<EnumT> enumClass)
text
to a Java Enum, according to its constants'
programmatic names.enumOrdinalsOf(Class)
@NonNull public static <T> TypeCodec<Optional<T>> optionalOf(@NonNull TypeCodec<T> innerCodec)
Optional
instances
(mapping CQL null to Optional.empty()
).@NonNull public static <T> TypeCodec<T> json(@NonNull GenericType<T> javaType)
text
to the given Java type, using JSON
serialization with a default Jackson mapper.@NonNull public static <T> TypeCodec<T> json(@NonNull Class<T> javaType)
text
to the given Java type, using JSON
serialization with a default Jackson mapper.@NonNull public static <T> TypeCodec<T> json(@NonNull GenericType<T> javaType, @NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper)
text
to the given Java type, using JSON
serialization with the provided Jackson mapper.@NonNull public static <T> TypeCodec<T> json(@NonNull Class<T> javaType, @NonNull com.fasterxml.jackson.databind.ObjectMapper objectMapper)
text
to the given Java type, using JSON
serialization with the provided Jackson mapper.Copyright © 2017–2024. All rights reserved.