T
- a Java type that can represent the named logical typepublic abstract class Conversion<T> extends Object
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
typegetLogicalTypeName()
: 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.
ServiceLoader
Constructor and Description |
---|
Conversion() |
public abstract Class<T> getConvertedType()
public abstract String getLogicalTypeName()
public String adjustAndSetValue(String varName, String valParamName)
varName
- the name of the variable holding the converted valuevalParamName
- the name of the parameter with the new converted valuepublic Schema getRecommendedSchema()
public T fromBoolean(Boolean value, Schema schema, LogicalType type)
public T fromInt(Integer value, Schema schema, LogicalType type)
public T fromLong(Long value, Schema schema, LogicalType type)
public T fromFloat(Float value, Schema schema, LogicalType type)
public T fromDouble(Double value, Schema schema, LogicalType type)
public T fromCharSequence(CharSequence value, Schema schema, LogicalType type)
public T fromEnumSymbol(GenericEnumSymbol<?> value, Schema schema, LogicalType type)
public T fromFixed(GenericFixed value, Schema schema, LogicalType type)
public T fromBytes(ByteBuffer value, Schema schema, LogicalType type)
public T fromArray(Collection<?> value, Schema schema, LogicalType type)
public T fromMap(Map<?,?> value, Schema schema, LogicalType type)
public T fromRecord(IndexedRecord value, Schema schema, LogicalType type)
public Boolean toBoolean(T value, Schema schema, LogicalType type)
public Integer toInt(T value, Schema schema, LogicalType type)
public Long toLong(T value, Schema schema, LogicalType type)
public Float toFloat(T value, Schema schema, LogicalType type)
public Double toDouble(T value, Schema schema, LogicalType type)
public CharSequence toCharSequence(T value, Schema schema, LogicalType type)
public GenericEnumSymbol<?> toEnumSymbol(T value, Schema schema, LogicalType type)
public GenericFixed toFixed(T value, Schema schema, LogicalType type)
public ByteBuffer toBytes(T value, Schema schema, LogicalType type)
public Collection<?> toArray(T value, Schema schema, LogicalType type)
public Map<?,?> toMap(T value, Schema schema, LogicalType type)
public IndexedRecord toRecord(T value, Schema schema, LogicalType type)
Copyright © 2009–2023 The Apache Software Foundation. All rights reserved.