org.apache.avro

## Class Conversion<T>

- java.lang.Object
- - org.apache.avro.Conversion<T>

- Type Parameters:
    `T` - a Java type that can represent the named logical type
  Direct Known Subclasses:
    Conversions.BigDecimalConversion, Conversions.DecimalConversion, Conversions.UUIDConversion, ProtoConversions.TimestampMicrosConversion, ProtoConversions.TimestampMillisConversion, TimeConversions.DateConversion, TimeConversions.LocalTimestampMicrosConversion, TimeConversions.LocalTimestampMillisConversion, TimeConversions.TimeMicrosConversion, TimeConversions.TimeMillisConversion, TimeConversions.TimestampMicrosConversion, TimeConversions.TimestampMillisConversion

      public abstract class Conversion<T>
      extends Object

  Conversion between generic and logical type instances.

  Instances of this class can be added to GenericData to convert a logical type to a particular representation. This can be done manually, using `GenericData.addLogicalTypeConversion(Conversion)`, or automatically. This last option uses the Java `ServiceLoader`, and requires the implementation to be a public class with a public no-arg constructor, be named in a file called `/META-INF/services/org.apache.avro.Conversion`, and both must available in the classpath.

Implementations must provide:

- `getConvertedType()`: get the Java class used for the logical type
- `getLogicalTypeName()`: get the logical type this implements

Subclasses must also override the conversion methods for Avro's base types that are valid for the logical type, or else risk causing `UnsupportedOperationException` at runtime.

Optionally, use `getRecommendedSchema()` to provide a Schema that will be used when generating a Schema for the class. This is useful when using `ReflectData` or `ProtobufData`, for example.

See Also:
  `ServiceLoader`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Conversion()`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `adjustAndSetValue(String varName, String valParamName)` Certain logical types may require adjusting the code within the "setter" methods to make sure the data that is set is properly formatted.
    `T` | `fromArray(Collection<?> value, Schema schema, LogicalType type)`
    `T` | `fromBoolean(Boolean value, Schema schema, LogicalType type)`
    `T` | `fromBytes(ByteBuffer value, Schema schema, LogicalType type)`
    `T` | `fromCharSequence(CharSequence value, Schema schema, LogicalType type)`
    `T` | `fromDouble(Double value, Schema schema, LogicalType type)`
    `T` | `fromEnumSymbol(GenericEnumSymbol<?> value, Schema schema, LogicalType type)`
    `T` | `fromFixed(GenericFixed value, Schema schema, LogicalType type)`
    `T` | `fromFloat(Float value, Schema schema, LogicalType type)`
    `T` | `fromInt(Integer value, Schema schema, LogicalType type)`
    `T` | `fromLong(Long value, Schema schema, LogicalType type)`
    `T` | `fromMap(Map<?,?> value, Schema schema, LogicalType type)`
    `T` | `fromRecord(IndexedRecord value, Schema schema, LogicalType type)`
    `abstract Class<T>` | `getConvertedType()` Return the Java class representing the logical type.
    `abstract String` | `getLogicalTypeName()` Return the logical type this class converts.
    `Schema` | `getRecommendedSchema()`
    `Collection<?>` | `toArray(T value, Schema schema, LogicalType type)`
    `Boolean` | `toBoolean(T value, Schema schema, LogicalType type)`
    `ByteBuffer` | `toBytes(T value, Schema schema, LogicalType type)`
    `CharSequence` | `toCharSequence(T value, Schema schema, LogicalType type)`
    `Double` | `toDouble(T value, Schema schema, LogicalType type)`
    `GenericEnumSymbol<?>` | `toEnumSymbol(T value, Schema schema, LogicalType type)`
    `GenericFixed` | `toFixed(T value, Schema schema, LogicalType type)`
    `Float` | `toFloat(T value, Schema schema, LogicalType type)`
    `Integer` | `toInt(T value, Schema schema, LogicalType type)`
    `Long` | `toLong(T value, Schema schema, LogicalType type)`
    `Map<?,?>` | `toMap(T value, Schema schema, LogicalType type)`
    `IndexedRecord` | `toRecord(T value, Schema schema, LogicalType type)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Conversion

          public Conversion()

  - ### Method Detail

    - #### getConvertedType

          public abstract Class<T> getConvertedType()

      Return the Java class representing the logical type.
      Returns:
        a Java class returned by from methods and accepted by to methods

    - #### getLogicalTypeName

          public abstract String getLogicalTypeName()

      Return the logical type this class converts.
      Returns:
        a String logical type name

    - #### adjustAndSetValue

          public String adjustAndSetValue(String varName,
                                          String valParamName)

      Certain logical types may require adjusting the code within the "setter" methods to make sure the data that is set is properly formatted. This method allows the Conversion to generate custom setter code if required.
      Parameters:
        `varName` - the name of the variable holding the converted value
        `valParamName` - the name of the parameter with the new converted value
      Returns:
        a String for the body of the setter method

    - #### getRecommendedSchema

          public Schema getRecommendedSchema()

    - #### fromBoolean

          public T fromBoolean(Boolean value,
                               Schema schema,
                               LogicalType type)

    - #### fromInt

          public T fromInt(Integer value,
                           Schema schema,
                           LogicalType type)

    - #### fromLong

          public T fromLong(Long value,
                            Schema schema,
                            LogicalType type)

    - #### fromFloat

          public T fromFloat(Float value,
                             Schema schema,
                             LogicalType type)

    - #### fromDouble

          public T fromDouble(Double value,
                              Schema schema,
                              LogicalType type)

    - #### fromCharSequence

          public T fromCharSequence(CharSequence value,
                                    Schema schema,
                                    LogicalType type)

    - #### fromEnumSymbol

          public T fromEnumSymbol(GenericEnumSymbol<?> value,
                                  Schema schema,
                                  LogicalType type)

    - #### fromFixed

          public T fromFixed(GenericFixed value,
                             Schema schema,
                             LogicalType type)

    - #### fromBytes

          public T fromBytes(ByteBuffer value,
                             Schema schema,
                             LogicalType type)

    - #### fromArray

          public T fromArray(Collection<?> value,
                             Schema schema,
                             LogicalType type)

    - #### fromMap

          public T fromMap(Map<?,?> value,
                           Schema schema,
                           LogicalType type)

    - #### fromRecord

          public T fromRecord(IndexedRecord value,
                              Schema schema,
                              LogicalType type)

    - #### toBoolean

          public Boolean toBoolean(T value,
                                   Schema schema,
                                   LogicalType type)

    - #### toInt

          public Integer toInt(T value,
                               Schema schema,
                               LogicalType type)

    - #### toLong

          public Long toLong(T value,
                             Schema schema,
                             LogicalType type)

    - #### toFloat

          public Float toFloat(T value,
                               Schema schema,
                               LogicalType type)

    - #### toDouble

          public Double toDouble(T value,
                                 Schema schema,
                                 LogicalType type)

    - #### toCharSequence

          public CharSequence toCharSequence(T value,
                                             Schema schema,
                                             LogicalType type)

    - #### toEnumSymbol

          public GenericEnumSymbol<?> toEnumSymbol(T value,
                                                   Schema schema,
                                                   LogicalType type)

    - #### toFixed

          public GenericFixed toFixed(T value,
                                      Schema schema,
                                      LogicalType type)

    - #### toBytes

          public ByteBuffer toBytes(T value,
                                    Schema schema,
                                    LogicalType type)

    - #### toArray

          public Collection<?> toArray(T value,
                                       Schema schema,
                                       LogicalType type)

    - #### toMap

          public Map<?,?> toMap(T value,
                                Schema schema,
                                LogicalType type)

    - #### toRecord

          public IndexedRecord toRecord(T value,
                                        Schema schema,
                                        LogicalType type)

Copyright © 2009–2024 The Apache Software Foundation. All rights reserved.