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,
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(Class<T> clazz)
Returns the
Coder to use by default for values of the given
class. |
<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> 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 class to handle encoding and
decoding instances of clazz, overriding prior registrations if any exist. |
void |
registerCoder(Class<?> clazz,
CoderFactory coderFactory)
Registers
coderFactory as the default CoderFactory to produce Coder
instances to decode and encode instances of clazz. |
<T> void |
registerCoder(Class<T> rawClazz,
Coder<T> coder)
Register the provided
Coder for encoding all values of the specified Class. |
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 class to handle encoding and
decoding instances of clazz, overriding prior registrations if any exist.
Supposing T is the static type corresponding to the clazz, then
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.
clazz - the class of objects to be encodedcoderClazz - a class with static factory methods to provide coderspublic void registerCoder(Class<?> clazz, CoderFactory coderFactory)
coderFactory as the default CoderFactory to produce Coder
instances to decode and encode instances of clazz. This will override prior
registrations if any exist.public <T> void registerCoder(Class<T> rawClazz, Coder<T> coder)
Coder for encoding all values of the specified Class.
This will override prior registrations if any exist.
Not for use with generic rawtypes. Instead, register a CoderFactory via
registerCoder(Class, CoderFactory) or ensure your Coder class has the
appropriate static methods and register it directly via registerCoder(Class, Class).
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, 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
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<T> getDefaultCoder(Class<T> clazz) throws CannotProvideCoderException
Coder to use by default for values of the given
class.
Coder class registered explicitly via
a call to registerCoder(java.lang.Class<?>, java.lang.Class<?>),
DefaultCoder annotation on the class,
CoderProvider, which
may be able to generate a coder for an arbitrary class.
CannotProvideCoderException - if a coder cannot be providedpublic void setFallbackCoderProvider(CoderProvider coderProvider)
public CoderProvider getFallbackCoderProvider()