Class ArrayToCollection

java.lang.Object
org.springframework.binding.convert.converters.ArrayToCollection
All Implemented Interfaces:
Converter, TwoWayConverter

public class ArrayToCollection extends Object implements TwoWayConverter
Special converter that converts from a source array to a target collection. Supports the selection of an "approximate" collection implementation when a target collection interface such as List.class is specified. Supports type conversion of array elements when a concrete parameterized collection class is provided, such as IntegerList<Integer>.class. Note that type erasure prevents arbitrary access to generic collection element type information at runtime, preventing the ability to convert elements for collections declared as properties. Mainly used internally by ConversionService implementations.
Author:
Keith Donald
  • Constructor Details

    • ArrayToCollection

      public ArrayToCollection(ConversionService conversionService)
      Creates a new array to collection converter.
      Parameters:
      conversionService - the conversion service to use to lookup the converter to apply to array elements added to the target collection
    • ArrayToCollection

      public ArrayToCollection(ConversionExecutor elementConverter)
      Creates a new array to collection converter.
      Parameters:
      elementConverter - A specific converter to use on array elements when adding them to the target collection
  • Method Details

    • getSourceClass

      public Class<?> getSourceClass()
      Description copied from interface: Converter
      The source class this converter can convert from. May be an interface or abstract type to allow this converter to convert specific subclasses as well.
      Specified by:
      getSourceClass in interface Converter
      Returns:
      the source type
    • getTargetClass

      public Class<?> getTargetClass()
      Description copied from interface: Converter
      The target class this converter can convert to. May be an interface or abstract type to allow this converter to convert specific subclasses as well.
      Specified by:
      getTargetClass in interface Converter
      Returns:
      the target type
    • convertSourceToTargetClass

      public Object convertSourceToTargetClass(Object source, Class<?> targetClass) throws Exception
      Description copied from interface: Converter
      Convert the provided source object argument to an instance of the specified target class.
      Specified by:
      convertSourceToTargetClass in interface Converter
      Parameters:
      source - the source object to convert, which must be an instance of Converter.getSourceClass()
      targetClass - the target class to convert the source to, which must be equal to or a specialization of Converter.getTargetClass()
      Returns:
      the converted object, which must be an instance of the targetClass
      Throws:
      Exception - an exception occurred performing the conversion
    • convertTargetToSourceClass

      public Object convertTargetToSourceClass(Object target, Class<?> sourceClass)
      Description copied from interface: TwoWayConverter
      Convert the provided target object argument to an instance of the specified source class.
      Specified by:
      convertTargetToSourceClass in interface TwoWayConverter
      Parameters:
      target - the target object to convert, which must be an instance of Converter.getTargetClass()
      sourceClass - the source class to convert the target to, which must be equal to or a specialization of Converter.getSourceClass()
      Returns:
      the converted object, which must be an instance of the sourceClass