Package 

Object ConvertersManager

  • All Implemented Interfaces:

    
    public class ConvertersManager
    
                        

    Registry that holds functions to convert from one type to another.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      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.
      final Unit remove(Pair<KClass<?>, KClass<?>> key) Delete an existing mapping by its key.
      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.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • 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.