public class CoderRegistry extends Object implements CoderProvider
CoderRegistry allows registering the
default Coder to use for a Java class,
and looking up and instantiating the default
Coder for a Java type.
CoderRegistry uses the following mechanisms to determine a
default Coder for a Java class, in order of precedence:
registerCoder(java.lang.Class<?>, java.lang.Class<?>). Built-in types are registered via
registerStandardCoders().
DefaultCoder can be used to annotate a type with
the default Coder type.
Serializable objects are given a default
Coder of SerializableCoder.
| Constructor and Description |
|---|
CoderRegistry() |
| Modifier and Type | Method and Description |
|---|---|
<T> Coder<T> |
getCoder(TypeDescriptor<T> typeDescriptor)
|
<T,OutputT> |
getDefaultCoder(Class<? extends T> subClass,
Class<T> baseClass,
Coder<?>... knownCoders)
Returns the Coder to use for the last type parameter specialization
of the subclass given Coders to use for all other type parameters
specializations (if any).
|
<T,OutputT> |
getDefaultCoder(Class<? extends T> subClass,
Class<T> baseClass,
Map<Type,? extends Coder<?>> knownCoders,
TypeVariable<?> param)
Returns the Coder to use for the specified type parameter specialization
of the subclass, given Coders to use for all other type parameters
(if any).
|
<T> Coder<T> |
getDefaultCoder(T exampleValue)
Returns the Coder to use for the provided example value, if it can
be determined.
|
<InputT,OutputT> |
getDefaultCoder(TypeDescriptor<OutputT> typeDescriptor,
TypeDescriptor<InputT> contextTypeDescriptor,
Coder<InputT> contextCoder)
Returns the Coder to use by default for values of the given type,
where the given context type uses the given context coder.
|
<T> Coder<T> |
getDefaultCoder(TypeDescriptor<T> typeDescriptor)
Returns the Coder to use by default for values of the given type.
|
<T> Coder<?>[] |
getDefaultCoders(Class<? extends T> subClass,
Class<T> baseClass,
Coder<?>[] knownCoders)
Returns an array listing, for each of
baseClass's type parameters, the
Coder to use by default for it, in the context of subClass's specialization
of baseClass. |
<T> Map<Type,Coder<?>> |
getDefaultCoders(Class<? extends T> subClass,
Class<T> baseClass,
Map<Type,? extends Coder<?>> knownCoders)
Returns a Map from each of baseClass's type parameters to the Coder to
use by default for it, in the context of subClass's specialization of
baseClass.
|
<InputT,OutputT> |
getDefaultOutputCoder(SerializableFunction<InputT,OutputT> fn,
Coder<InputT> inputCoder)
Returns the Coder to use on elements produced by this function, given
the coder used for its input elements.
|
CoderProvider |
getFallbackCoderProvider() |
void |
registerCoder(Class<?> clazz,
Class<?> coderClazz)
Registers
coderClazz as the default Coder<T>
class to handle encoding and decoding instances of clazz
of type T. |
void |
registerCoder(Class<?> rawClazz,
Coder<?> coder) |
void |
registerCoder(Class<?> rawClazz,
CoderFactory coderFactory) |
void |
registerStandardCoders()
Registers standard Coders with this CoderRegistry.
|
void |
setFallbackCoderProvider(CoderProvider coderProvider) |
public void registerStandardCoders()
public void registerCoder(Class<?> clazz, Class<?> coderClazz)
coderClazz as the default Coder<T>
class to handle encoding and decoding instances of clazz
of type T.
coderClazz should have a static factory method with the
following signature:
public static Coder<T> of(Coder<X> argCoder1, Coder<Y> argCoder2, ...)
This method will be called to create instances of Coder<T>
for values of type T, passing Coders for each of the generic type
parameters of T. If T takes no generic type parameters,
then the of() factory method should have no arguments.
If T is a parameterized type, then it should additionally
have a method with the following signature:
public static List<Object> getInstanceComponents(T exampleValue);
This method will be called to decompose a value during the coder inference process, to automatically choose coders for the components
public void registerCoder(Class<?> rawClazz, CoderFactory coderFactory)
public <T> Coder<T> getDefaultCoder(TypeDescriptor<T> typeDescriptor) throws CannotProvideCoderException
CannotProvideCoderException - if there is no default Coder.public <T> Coder<T> getCoder(TypeDescriptor<T> typeDescriptor) throws CannotProvideCoderException
getCoder in interface CoderProviderCannotProvideCoderException - if no coder can be providedpublic <InputT,OutputT> Coder<OutputT> getDefaultCoder(TypeDescriptor<OutputT> typeDescriptor, TypeDescriptor<InputT> contextTypeDescriptor, Coder<InputT> contextCoder) throws CannotProvideCoderException
CannotProvideCoderException - if there is no default Coder.public <InputT,OutputT> Coder<OutputT> getDefaultOutputCoder(SerializableFunction<InputT,OutputT> fn, Coder<InputT> inputCoder) throws CannotProvideCoderException
CannotProvideCoderExceptionpublic <T,OutputT> Coder<OutputT> getDefaultCoder(Class<? extends T> subClass, Class<T> baseClass, Coder<?>... knownCoders) throws CannotProvideCoderException
CannotProvideCoderException - if there is no default Coder.public <T,OutputT> Coder<OutputT> getDefaultCoder(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders, TypeVariable<?> param) throws CannotProvideCoderException
CannotProvideCoderException - if there is no default Coder.public <T> Coder<T> getDefaultCoder(T exampleValue) throws CannotProvideCoderException
CannotProvideCoderException - if there is no default Coder or
more than one coder matchespublic <T> Map<Type,Coder<?>> getDefaultCoders(Class<? extends T> subClass, Class<T> baseClass, Map<Type,? extends Coder<?>> knownCoders)
If no coder can be inferred for a particular type parameter, then that type variable will be absent from the returned map.
For example, if baseClass is Map.class and subClass extends
Map<String, Integer> then this will return the registered Coders
to use for String and Integer as a {"K": stringCoder, "V": intCoder} Map.
The knownCoders parameter can be used to provide known coders for any of
the parameters that will be used to infer the others.
Note that inference is attempted for every type variable.
For a type MyType<One, Two, Three> inference will will be
attempted for all of One, Two, Three,
even if the requester only wants a coder for Two.
For this reason, getDefaultCoders (plural) does not throw
an exception if a coder for a particular type variable cannot be
inferred. Instead, it is left absent from the map. It is the responsibility
of the caller (usually getDefaultCoder(com.google.cloud.dataflow.sdk.values.TypeDescriptor<T>) to extract the
desired coder or throw a CannotProvideCoderException when appropriate.
subClass - the concrete type whose specializations are being inferredbaseClass - the base type, a parameterized classknownCoders - a map corresponding to the set of known coders indexed
by parameter namepublic <T> Coder<?>[] getDefaultCoders(Class<? extends T> subClass, Class<T> baseClass, Coder<?>[] knownCoders)
baseClass's type parameters, the
Coder to use by default for it, in the context of subClass's specialization
of baseClass.
If a coder cannot be inferred for a type variable, its slot in the
resulting array will be null.
For example, if baseClass is Map.class and subClass
extends Map<String, Integer> then this will return the registered Coders
to use for String and Integer, in that order.
The knownCoders parameter can be used to provide known coders
for any of the parameters that will be used to infer the others.
Note that inference is attempted for every type variable.
For a type MyType<One, Two, Three> inference will will be
attempted for all of One, Two, Three,
even if the requester only wants a coder for Two.
For this reason getDefaultCoders (plural) does not throw
an exception if a coder for a particular type variable cannot be
inferred. Instead, it results in a null in the array.
It is the responsibility of the caller (usually getDefaultCoder(com.google.cloud.dataflow.sdk.values.TypeDescriptor<T>)
to extract the desired coder or throw a CannotProvideCoderException
when appropriate.
subClass - the concrete type whose specializations are being inferredbaseClass - the base type, a parameterized classknownCoders - an array corresponding to the set of base class
type parameters. Each entry is can be either a Coder (in which
case it will be used for inference) or null (in which case it
will be inferred). May be null to indicate the entire set of
parameters should be inferred.IllegalArgumentException - if baseClass doesn't have type parameters
or if the length of knownCoders is not equal to the number of type
parameterspublic void setFallbackCoderProvider(CoderProvider coderProvider)
public CoderProvider getFallbackCoderProvider()