Class TypeConverterManager


  • public class TypeConverterManager
    extends java.lang.Object
    Provides dynamic object conversion to a type. Contains a map of registered converters. User may add new converters.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <C extends java.util.Collection<T>,​T>
      C
      convertToCollection​(java.lang.Object value, java.lang.Class<? extends java.util.Collection> destinationType, java.lang.Class<T> componentType)
      Special case of convertType(Object, Class) when target is collection and when component type is known.
      <T> T convertType​(java.lang.Object value, java.lang.Class<T> destinationType)
      Converts an object to destination type.
      static TypeConverterManager get()
      Returns default implementation.
      <T> TypeConverter<T> lookup​(java.lang.Class<T> type)
      Retrieves converter for provided type.
      <T> void register​(java.lang.Class<T> type, TypeConverter<T> typeConverter)
      Registers a converter for specified type.
      void registerDefaults()
      Registers default set of converters.
      void unregister​(java.lang.Class type)
      Un-registers converter for given type.
      • Methods inherited from class java.lang.Object

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

      • TypeConverterManager

        public TypeConverterManager()
    • Method Detail

      • registerDefaults

        public void registerDefaults()
        Registers default set of converters.
      • register

        public <T> void register​(java.lang.Class<T> type,
                                 TypeConverter<T> typeConverter)
        Registers a converter for specified type. User must register converter for all super-classes as well.
        Parameters:
        type - class that converter is for
        typeConverter - converter for provided class
      • unregister

        public void unregister​(java.lang.Class type)
        Un-registers converter for given type.
      • lookup

        public <T> TypeConverter<T> lookup​(java.lang.Class<T> type)
        Retrieves converter for provided type. Only registered types are matched, therefore subclasses must be also registered.
        Returns:
        founded converter or null
      • convertType

        public <T> T convertType​(java.lang.Object value,
                                 java.lang.Class<T> destinationType)
        Converts an object to destination type. If type is registered, it's TypeConverter will be used. If not, it scans of destination is an array or enum, as those two cases are handled in a special way.

        If destination type is one of common types, consider using Converter instead for somewhat faster approach (no lookup).

      • convertToCollection

        public <C extends java.util.Collection<T>,​T> C convertToCollection​(java.lang.Object value,
                                                                                 java.lang.Class<? extends java.util.Collection> destinationType,
                                                                                 java.lang.Class<T> componentType)
        Special case of convertType(Object, Class) when target is collection and when component type is known.