Class TypeConverter

  • Direct Known Subclasses:
    DefaultTypeConverter

    @AlfrescoPublicApi
    public class TypeConverter
    extends Object
    Support for generic conversion between types. Additional conversions may be added. Direct conversion and two stage conversions via Number are supported. We do not support conversion by any route at the moment
    • Constructor Detail

      • TypeConverter

        public TypeConverter()
    • Method Detail

      • convert

        public final Object convert​(DataTypeDefinition propertyType,
                                    Object value)
        General conversion method to Object types (note it cannot support conversion to primary types due the restrictions of reflection. Use the static conversion methods to primitive types)
        Parameters:
        propertyType - - the target property type
        value - - the value to be converted
        Returns:
        - the converted value as the correct type
      • convert

        public final <T> T convert​(Class<T> c,
                                   Object value)
        General conversion method to Object types (note it cannot support conversion to primary types due the restrictions of reflection. Use the static conversion methods to primitive types)
        Type Parameters:
        T - The target type for the result of the conversion
        Parameters:
        c - - a class for the target type
        value - - the value to be converted
        Returns:
        - the converted value as the correct type
        Throws:
        TypeConversionException - if the conversion cannot be performed
      • convert

        public final Collection<?> convert​(DataTypeDefinition propertyType,
                                           Object[] values)
        General conversion method to convert collection contents to the specified type. Wrapper around the Collection version for arrays.
        Parameters:
        propertyType - - the target property type
        values - - the value to be converted
        Returns:
        - the converted value as the correct type
        Throws:
        DictionaryException - if the property type's registered java class is invalid
        TypeConversionException - if the conversion cannot be performed
      • convert

        public final Collection<?> convert​(DataTypeDefinition propertyType,
                                           Collection<?> values)
        General conversion method to convert collection contents to the specified type.
        Parameters:
        propertyType - - the target property type
        values - - the value to be converted
        Returns:
        - the converted value as the correct type
        Throws:
        DictionaryException - if the property type's registered java class is invalid
        TypeConversionException - if the conversion cannot be performed
      • convert

        public final <T> Collection<T> convert​(Class<T> c,
                                               Collection<?> values)
        General conversion method to convert collection contents to the specified type.
        Type Parameters:
        T - The target type for the result of the conversion
        Parameters:
        c - - a class for the target type
        values - - the collection to be converted
        Returns:
        - the converted collection
        Throws:
        TypeConversionException - if the conversion cannot be performed
      • booleanValue

        public final boolean booleanValue​(Object value)
        Get the boolean value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        boolean
      • charValue

        public final char charValue​(Object value)
        Get the char value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        char
      • byteValue

        public final byte byteValue​(Object value)
        Get the byte value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        byte
      • shortValue

        public final short shortValue​(Object value)
        Get the short value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        short
      • intValue

        public final int intValue​(Object value)
        Get the int value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        int
      • longValue

        public final long longValue​(Object value)
        Get the long value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        long
      • floatValue

        public final float floatValue​(Object value)
        Get the bollean value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        float
      • doubleValue

        public final double doubleValue​(Object value)
        Get the bollean value for the value object May have conversion failure
        Parameters:
        value - Object
        Returns:
        double
      • isMultiValued

        public final boolean isMultiValued​(Object value)
        Is the value multi valued
        Parameters:
        value - Object
        Returns:
        true - if the underlyinf is a collection of values and not a singole value
      • size

        public final int size​(Object value)
        Get the number of values represented
        Parameters:
        value - Object
        Returns:
        1 for normal values and the size of the collection for MVPs
      • getCollection

        public final <T> Collection<T> getCollection​(Class<T> c,
                                                     Object value)
        Get a collection for the passed value converted to the specified type
        Parameters:
        value - Object
        Returns:
        Collection
      • addConverter

        public final <F,​T> void addConverter​(Class<F> source,
                                                   Class<T> destination,
                                                   TypeConverter.Converter<F,​T> converter)
        Add a converter to the list of those available
      • addDynamicTwoStageConverter

        public final <F,​I,​T> TypeConverter.Converter<F,​T> addDynamicTwoStageConverter​(Class<F> source,
                                                                                                        Class<I> intermediate,
                                                                                                        Class<T> destination)
        Add a dynamic two stage converter
      • getConverter

        public final <T> TypeConverter.Converter<Object,​T> getConverter​(Object value,
                                                                              Class<T> dest)
        Find conversion for the specified object Note: Takes into account the class of the object and any interfaces it may also support.
      • getConverter

        public <F,​T> TypeConverter.Converter<F,​T> getConverter​(Class<F> source,
                                                                           Class<T> dest)
        Find a conversion for a specific Class
        Returns:
        conversion