Class Converters


  • public final class Converters
    extends Object
    Convenience class for converting an object from one type to an object of another.
    Since:
    2.4
    Author:
    Justin Deoliveira, The Open Planning Project
    • Constructor Detail

      • Converters

        public Converters()
    • Method Detail

      • getConverterFactories

        public static Set<ConverterFactory> getConverterFactories​(Hints hints)
        Returns a set of all available implementations for the ConverterFactory interface.
        Parameters:
        hints - An optional map of hints, or null if none.
        Returns:
        Set of available ConverterFactory implementations.
      • getConverterFactories

        public static Set<ConverterFactory> getConverterFactories​(Class<?> source,
                                                                  Class<?> target)
        Returns a set of all available ConverterFactory's which can handle convert from the source to destination class.

        This method essentially returns all factories in which the following returns non null.

         factory.createConverter( source, target );
         
        Since:
        2.5
      • convert

        public static <T> T convert​(Object source,
                                    Class<T> target)
        Converts an object of a particular type into an object of a different type.

        Convenience for convert(Object, Class, Hints)

        Parameters:
        source - The object to convert.
        target - The type of the converted value.
        Returns:
        The converted value as an instance of target, or null if a converter could not be found
        Since:
        2.4
      • convert

        public static <T> T convert​(Object source,
                                    Class<T> target,
                                    Hints hints)
        Converts an object of a particular type into an object of a different type.

        This method uses the ConverterFactory extension point to find a converter capable of performing the conversion. The first converter found is the one used. Using this class there is no way to guarantee which converter will be used.

        Parameters:
        source - The object to convert.
        target - The type of the converted value.
        hints - Any hints for the converter factory.
        Returns:
        The converted value as an instance of target, or null if a converter could not be found.
        Since:
        2.4