org.spicefactory.cinnamon.reflect.convert
Class AbstractSequenceConverter<T>

java.lang.Object
  extended by org.spicefactory.cinnamon.reflect.convert.AbstractSequenceConverter<T>
All Implemented Interfaces:
Converter<T>
Direct Known Subclasses:
ArrayConverter, CollectionConverter

public abstract class AbstractSequenceConverter<T>
extends Object
implements Converter<T>

Abstract Converter implementation for converting "sequences". Serves as a common base class for CollectionConverter and ArrayConverter.

Author:
Jens Halm

Constructor Summary
protected AbstractSequenceConverter(ReflectionFactory factory)
           
 
Method Summary
protected abstract  void addElement(T sequence, Object element, int index)
          Adds the given element to the sequence (Array or Collection) at the specified index.
 T convert(Object value, TypeContext typeContext)
          Converts the given value to the target type of this converter.
protected abstract  T createInstance(TypeContext typeContext, int size, Object originalValue)
          Create a new instance for the required target type.
protected abstract  Type getElementType(TypeContext typeContext)
          Return the generic element type of the sequence (Array or Collection).
protected  Iterator<?> getIterator(Object value)
          Return an Iterator for the given sequence (Array or Collection).
protected  int getSize(Object value)
           
 boolean supports(Class<?> type)
          Checks if this converter can convert instances of the given class to the target type of this converter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractSequenceConverter

protected AbstractSequenceConverter(ReflectionFactory factory)
Method Detail

convert

public T convert(Object value,
                 TypeContext typeContext)
Description copied from interface: Converter
Converts the given value to the target type of this converter. The TypeContext parameter serves as an additional hint for the required type of the converted value. Example: If a Converter implementation handles all kinds of Collections (implements Converter<Collection>) the given TypeContext might provide information for the exact nature of the Collection (like List<String>).

Specified by:
convert in interface Converter<T>
Parameters:
value - the value to be converted
typeContext - additional information for the required type of the converted value
Returns:
the converted value

getSize

protected int getSize(Object value)

getIterator

protected Iterator<?> getIterator(Object value)
Return an Iterator for the given sequence (Array or Collection).

Parameters:
value - the sequence for which an Iterator should be created
Returns:
the Iterator for the given sequence

createInstance

protected abstract T createInstance(TypeContext typeContext,
                                    int size,
                                    Object originalValue)
Create a new instance for the required target type. The given TypeContext instance gives additional hints on the exact required type of the sequence (i.e. a Set or List or an Array of Numbers).

Parameters:
typeContext - the context from which the required type of the new instance be obtained
size - the size of the Array/Collection to create
originalValue - the original value (Array or Collection - mainly for logging purposes)
Returns:
the new instance

getElementType

protected abstract Type getElementType(TypeContext typeContext)
Return the generic element type of the sequence (Array or Collection).

Parameters:
typeContext - the context from which the element type can be obtained
Returns:
the generic element type of the sequence (Array or Collection)

addElement

protected abstract void addElement(T sequence,
                                   Object element,
                                   int index)
Adds the given element to the sequence (Array or Collection) at the specified index.

Parameters:
sequence - the sequence (Array or Collection) the item should be added to
element - the new element to be added
index - the index at which the element should be added

supports

public boolean supports(Class<?> type)
Description copied from interface: Converter
Checks if this converter can convert instances of the given class to the target type of this converter. Of course this does not give any guarantee that conversion will succeed for any instance of the given type.

Specified by:
supports in interface Converter<T>
Parameters:
type - the class to be checked
Returns:
true if this converter can convert instances of the given class