-
- All Implemented Interfaces:
public class ConvertersManagerRegistry that holds functions to convert from one type to another.
-
-
Field Summary
Fields Modifier and Type Field Description public final static ConvertersManagerINSTANCE
-
Method Summary
Modifier and Type Method Description final <S extends Any, T extends Any> Unitregister(Pair<KClass<S>, KClass<T>> key, Function1<S, T> block)Register a mapping function from one type to another. final Unitremove(Pair<KClass<?>, KClass<?>> key)Delete an existing mapping by its key. final <S extends Any, T extends Any> Tconvert(S source, KClass<T> target)Convert one type to another using the registered mapper function among both types. final <S extends Any, T extends Any> List<T>convertObjects(Iterable<S> source, KClass<T> target)Convert a group of instances of one type to another type using the registered mapper function among both types. -
-
Method Detail
-
register
final <S extends Any, T extends Any> Unit register(Pair<KClass<S>, KClass<T>> key, Function1<S, T> block)
Register a mapping function from one type to another.
- Parameters:
key- Pair which key is the source type and the value is the target type.block- Block that converts an instance of the source type to the target one.
-
remove
final Unit remove(Pair<KClass<?>, KClass<?>> key)
Delete an existing mapping by its key.
- Parameters:
key- Key of the mapping to be removed.
-
convert
final <S extends Any, T extends Any> T convert(S source, KClass<T> target)
Convert one type to another using the registered mapper function among both types. On sources of the same type as target, the source object is returned without conversion.
Converter search DOES NOT CONSIDER SOURCE'S INTERFACES OR PARENT CLASSES. If no exact type is registered for the converter, it won't be found. There is an exception with maps: if no converter is found and source implements Map,
Map::class to KClass<Target>will be searched in the converters' registry.If no mapper function is defined for the specified types, an exception is thrown.
- Parameters:
source- Value to convert to another type.target- Target type for the source instance.
-
convertObjects
final <S extends Any, T extends Any> List<T> convertObjects(Iterable<S> source, KClass<T> target)
Convert a group of instances of one type to another type using the registered mapper function among both types. If no mapper function is defined for the specified types, an exception is thrown.
- Parameters:
source- Values to convert to another type.target- Target type for the source instances in the group.
-
-
-
-