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 TypeMethodDescriptionstatic @NonNull ValueConverterRegistryAcquires a registry without any default converters or automatic enum/reflexive conversions.static @NonNull ValueConverterRegistryfromBlankSlateSupplementedBy(@NonNull Set<@NonNull ValueConverter<?, ?>> customValueConverters) Acquires a registry without any default converters or automatic enum/reflexive conversions, supplemented with custom converters.static @NonNull ValueConverterRegistryAcquires a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters().static @NonNull ValueConverterRegistryfromDefaultsSupplementedBy(@NonNull Set<@NonNull ValueConverter<?, ?>> customValueConverters) Acquires a registry with a sensible default set of converters as specified byValueConverters.defaultValueConverters(), supplemented with custom converters.<F,T> @NonNull Optional <ValueConverter<F, T>> get(@NonNull TypeReference<F> fromTypeReference, @NonNull TypeReference<T> toTypeReference) Obtains aValueConverterthat matches the 'from' and 'to' type references specified.Obtain aValueConverterthat matches the 'from' and 'to' types specified.
-
Method Details
-
fromDefaults
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
-
fromDefaultsSupplementedBy
public static @NonNull ValueConverterRegistry fromDefaultsSupplementedBy(@NonNull Set<@NonNull 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
-
fromBlankSlate
Acquires a registry without any default converters or automatic enum/reflexive conversions.This method is guaranteed to return a new instance.
- Returns:
- a registry instance without defaults or automatic conversions
-
fromBlankSlateSupplementedBy
public static @NonNull ValueConverterRegistry fromBlankSlateSupplementedBy(@NonNull Set<@NonNull ValueConverter<?, ?>> customValueConverters) Acquires a registry without any default converters or automatic enum/reflexive conversions, 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 without defaults or automatic conversions, supplemented with custom converters
-
get
public <F,T> @NonNull 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
public @NonNull 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
-