Class TypeCodec.AbstractTupleCodec<T>

  • Type Parameters:
    T - The Java type that this codec handles.
    Enclosing class:
    TypeCodec<T>

    public abstract static class TypeCodec.AbstractTupleCodec<T>
    extends TypeCodec<T>
    Base class for codecs mapping CQL tuples to Java objects. It can serve as a base class for codecs dealing with direct tuple-to-Pojo mappings.
    • Field Detail

      • definition

        protected final TupleType definition
    • Method Detail

      • accepts

        public boolean accepts​(DataType cqlType)
        Description copied from class: TypeCodec
        Return true if this codec is capable of deserializing the given cqlType.
        Overrides:
        accepts in class TypeCodec<T>
        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.
      • serialize

        public java.nio.ByteBuffer serialize​(T value,
                                             ProtocolVersion protocolVersion)
        Description copied from class: TypeCodec
        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.
        Specified by:
        serialize in class TypeCodec<T>
        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
      • deserialize

        public T deserialize​(java.nio.ByteBuffer bytes,
                             ProtocolVersion protocolVersion)
        Description copied from class: TypeCodec
        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.
        Specified by:
        deserialize in class TypeCodec<T>
        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
      • format

        public java.lang.String format​(T value)
        Description copied from class: TypeCodec
        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 querybuilder.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").

        Specified by:
        format in class TypeCodec<T>
        Parameters:
        value - An instance of T; may be null.
        Returns:
        CQL string
      • parse

        public T parse​(java.lang.String value)
        Description copied from class: TypeCodec
        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).

        Specified by:
        parse in class TypeCodec<T>
        Parameters:
        value - The CQL string to parse, may be null or empty.
        Returns:
        An instance of T; may be null on a null input.
      • newInstance

        protected abstract T newInstance()
        Return a new instance of T.
        Returns:
        A new instance of T.
      • serializeField

        protected abstract java.nio.ByteBuffer serializeField​(T source,
                                                              int index,
                                                              ProtocolVersion protocolVersion)
        Serialize an individual field in an object, as part of serializing the whole object to a CQL tuple (see serialize(Object, ProtocolVersion)).
        Parameters:
        source - The object to read the field from.
        index - The index of the field.
        protocolVersion - The protocol version to use.
        Returns:
        The serialized field, or null if that field should be ignored.
      • deserializeAndSetField

        protected abstract T deserializeAndSetField​(java.nio.ByteBuffer input,
                                                    T target,
                                                    int index,
                                                    ProtocolVersion protocolVersion)
        Deserialize an individual field and set it on an object, as part of deserializing the whole object from a CQL tuple (see deserialize(ByteBuffer, ProtocolVersion)).
        Parameters:
        input - The serialized form of the field.
        target - The object to set the field on.
        index - The index of the field.
        protocolVersion - The protocol version to use.
        Returns:
        The target object with the field set. In most cases this should be the same as target, but if you're dealing with immutable types you'll need to return a different instance.
      • formatField

        protected abstract java.lang.String formatField​(T source,
                                                        int index)
        Format an individual field in an object as a CQL literal, as part of formatting the whole object (see format(Object)).
        Parameters:
        source - The object to read the field from.
        index - The index of the field.
        Returns:
        The formatted value.
      • parseAndSetField

        protected abstract T parseAndSetField​(java.lang.String input,
                                              T target,
                                              int index)
        Parse an individual field and set it on an object, as part of parsing the whole object (see parse(String)).
        Parameters:
        input - The String to parse the field from.
        target - The value to write to.
        index - The index of the field.
        Returns:
        The target object with the field set. In most cases this should be the same as target, but if you're dealing with immutable types you'll need to return a different instance.