Class ValueConverterRegistry
ValueConverter
instances, supplemented with quality-of-life features that most applications need.
For example, the registry will automatically generate and cache off ValueConverter
instances when a requested 'from' type is String
and the 'to' type is an Enum
if no converter was previously specified (this is almost always the behavior you want).
The registry will also perform primitive mapping when locating ValueConverter
instances.
For example, if a requested 'from' String
and 'to' int
are specified and that converter does not exist, but a 'from' String
and 'to' Integer
does exist, it will be returned.
Finally, reflexive ValueConverter
instances are automatically created and cached off when the requested 'from' and 'to' types are identical.
Value conversion is documented in detail at https://www.soklet.com/docs/value-conversions.
- Author:
- Mark Allen
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters()
.ValueConverterRegistry
(Set<ValueConverter<?, ?>> customValueConverters) Creates a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters()
, optionally supplemented with custom converters. -
Method Summary
Modifier and TypeMethodDescription<F,
T> Optional <ValueConverter<F, T>> get
(TypeReference<F> fromTypeReference, TypeReference<T> toTypeReference) Obtains aValueConverter
that matches the 'from' and 'to' type references specified.Obtain aValueConverter
that matches the 'from' and 'to' types specified.static ValueConverterRegistry
The system's default shared registry instance.
-
Constructor Details
-
ValueConverterRegistry
public ValueConverterRegistry()Creates a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters()
. -
ValueConverterRegistry
Creates a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters()
, optionally supplemented with custom converters.- Parameters:
customValueConverters
- the custom value converters to include in the registry
-
-
Method Details
-
get
@Nonnull public <F,T> Optional<ValueConverter<F,T>> get(@Nonnull TypeReference<F> fromTypeReference, @Nonnull TypeReference<T> toTypeReference) Obtains aValueConverter
that matches the 'from' and 'to' type references specified.Because of type erasure, you cannot directly express a generic type like
List<String>.class
. You must encode it as a type parameter - in this case,new TypeReference<List<String>>() {}
.- Type Parameters:
F
- the 'from' typeT
- the 'to' type- Parameters:
fromTypeReference
- reference to the 'from' type of the convertertoTypeReference
- reference to the 'to' type of the converter- Returns:
- a matching
ValueConverter
, orOptional.empty()
if not found
-
get
@Nonnull public Optional<ValueConverter<Object,Object>> get(@Nonnull Type fromType, @Nonnull Type toType) Obtain aValueConverter
that matches the 'from' and 'to' types specified.- Parameters:
fromType
- the 'from' typetoType
- the 'to' type- Returns:
- a matching
ValueConverter
, orOptional.empty()
if not found