Class EnumCodec<T extends Enum<T>>

java.lang.Object
io.r2dbc.postgresql.codec.EnumCodec<T>
Type Parameters:
T - enum type
All Implemented Interfaces:
Codec<T>, CodecMetadata

public class EnumCodec<T extends Enum<T>> extends Object implements Codec<T>, CodecMetadata
Codec to map Postgres enumerated types to Java Enum values. This codec uses Enum.name() to map Postgres enum values as these are represented as string values.

Note that enum values are case-sensitive.

Since:
0.8.4
  • Constructor Details

    • EnumCodec

      public EnumCodec(io.netty.buffer.ByteBufAllocator byteBufAllocator, Class<T> type, int oid)
  • Method Details

    • canDecode

      public boolean canDecode(int dataType, io.r2dbc.postgresql.message.Format format, Class<?> type)
      Description copied from interface: Codec
      Determine whether this Codec is capable of decoding a value for the given dataType and Format and whether it can represent the decoded value as the desired type.
      Specified by:
      canDecode in interface Codec<T extends Enum<T>>
      Parameters:
      dataType - the Postgres OID to decode
      format - the data type Format, text or binary
      type - the desired value type
      Returns:
      true if this codec is able to decode values for tge given dataType and Format
    • canEncode

      public boolean canEncode(Object value)
      Description copied from interface: Codec
      Determine whether this Codec is capable of encoding the value.
      Specified by:
      canEncode in interface Codec<T extends Enum<T>>
      Parameters:
      value - the parameter value
      Returns:
      true if this Codec is able to encode the value.
      See Also:
    • canEncodeNull

      public boolean canEncodeNull(Class<?> type)
      Description copied from interface: Codec
      Determine whether this Codec is capable of encoding a null value for the given Class type.
      Specified by:
      canEncodeNull in interface Codec<T extends Enum<T>>
      Parameters:
      type - the desired value type
      Returns:
      true if this Codec is able to encode null values for the given Class type.
      See Also:
    • decode

      public T decode(@Nullable io.netty.buffer.ByteBuf buffer, int dataType, io.r2dbc.postgresql.message.Format format, Class<? extends T> type)
      Description copied from interface: Codec
      Decode the buffer and return it as the requested type.
      Specified by:
      decode in interface Codec<T extends Enum<T>>
      Parameters:
      buffer - the data buffer
      dataType - the Postgres OID to encode
      format - the data type Format, text or binary
      type - the desired value type
      Returns:
      the decoded value. Can be null if the value is null.
    • encode

      public io.r2dbc.postgresql.client.EncodedParameter encode(Object value)
      Description copied from interface: Codec
      Encode the value to be used as RPC parameter.
      Specified by:
      encode in interface Codec<T extends Enum<T>>
      Parameters:
      value - the value
      Returns:
      the encoded value
    • encode

      public io.r2dbc.postgresql.client.EncodedParameter encode(Object value, int dataType)
      Description copied from interface: Codec
      Encode the value to be used as RPC parameter.
      Specified by:
      encode in interface Codec<T extends Enum<T>>
      Parameters:
      value - the value
      dataType - the Postgres OID to encode
      Returns:
      the encoded value
    • encodeNull

      public io.r2dbc.postgresql.client.EncodedParameter encodeNull()
      Description copied from interface: Codec
      Encode a null value.
      Specified by:
      encodeNull in interface Codec<T extends Enum<T>>
      Returns:
      the encoded null value
    • type

      public Class<?> type()
      Description copied from interface: CodecMetadata
      Returns the Java type of this codec.
      Specified by:
      type in interface CodecMetadata
      Returns:
      the Java type
    • getDataTypes

      public Iterable<PostgresTypeIdentifier> getDataTypes()
      Description copied from interface: CodecMetadata
      Returns the collection of PostgresTypeIdentifier this codec can handle
      Specified by:
      getDataTypes in interface CodecMetadata
      Returns:
      the datatypes
    • builder

      public static EnumCodec.Builder builder()
      Create a new EnumCodec.Builder to build a CodecRegistrar to dynamically register Postgres enum types to Enum values.
      Returns:
      a new builder.