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
-
Method Summary
Modifier and TypeMethodDescription<F,T> Optional <ValueConverter<F, T>> get(TypeReference<F> fromTypeReference, TypeReference<T> toTypeReference) Obtains aValueConverterthat matches the 'from' and 'to' type references specified.Obtain aValueConverterthat matches the 'from' and 'to' types specified.static ValueConverterRegistryAcquires a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters().static ValueConverterRegistrywithDefaultsSupplementedBy(Set<ValueConverter<?, ?>> customValueConverters) Acquires a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters(), supplemented with custom converters.
-
Method Details
-
withDefaults
Acquires a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters().This method is guaranteed to return a new instance.
- Returns:
- a registry instance with sensible defaults
-
withDefaultsSupplementedBy
@Nonnull public static ValueConverterRegistry withDefaultsSupplementedBy(@Nonnull Set<ValueConverter<?, ?>> customValueConverters) Acquires a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters(), supplemented with custom converters.This method is guaranteed to return a new instance.
- Parameters:
customValueConverters- the custom value converters to include in the registry- Returns:
- a registry instance with sensible defaults, supplemented with custom converters
-
get
@Nonnull public <F,T> Optional<ValueConverter<F,T>> get(@Nonnull TypeReference<F> fromTypeReference, @Nonnull TypeReference<T> toTypeReference) Obtains aValueConverterthat 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 aValueConverterthat matches the 'from' and 'to' types specified.- Parameters:
fromType- the 'from' typetoType- the 'to' type- Returns:
- a matching
ValueConverter, orOptional.empty()if not found
-