net.sf.mmm.util.value.impl
Class ComposedValueConverterImpl

java.lang.Object
  extended by net.sf.mmm.util.component.base.AbstractComponent
      extended by net.sf.mmm.util.component.base.AbstractLoggableComponent
          extended by net.sf.mmm.util.value.base.AbstractGenericValueConverter<Object>
              extended by net.sf.mmm.util.value.base.AbstractComposedValueConverter
                  extended by net.sf.mmm.util.value.impl.ComposedValueConverterImpl
All Implemented Interfaces:
ComposedValueConverter, GenericValueConverter<Object>, SimpleValueConverter<Object,Object>, ValueConverter<Object,Object>
Direct Known Subclasses:
DefaultComposedValueConverter

@Singleton
@Named
public class ComposedValueConverterImpl
extends AbstractComposedValueConverter

This is the implementation of the ComposedValueConverter interface.

Since:
1.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Nested Class Summary
protected  class ComposedValueConverterImpl.ComposedTargetTypeConverter<TARGET>
          This inner class is a composed converter for all ValueConverters with the same target-type.
protected  class ComposedValueConverterImpl.TargetClass2ConverterMap
          This inner class is an AdvancedClassHierarchieMap for ComposedValueConverterImpl.ComposedTargetTypeConverters.
 
Field Summary
private  List<ValueConverter<?,?>> converters
           
private  ComposedValueConverterImpl.TargetClass2ConverterMap targetArrayClass2converterMap
           
private  ComposedValueConverterImpl.TargetClass2ConverterMap targetClass2converterMap
           
 
Constructor Summary
ComposedValueConverterImpl()
          The constructor.
 
Method Summary
 void addConverter(ValueConverter<?,?> converter)
          This method registers the given converter to this composed converter.
private  ValueConverter<?,?> addConverterInternal(ValueConverter<?,?> converter)
          This method registers the given converter to this composed converter.
 Object convert(Object value, Object valueSource, GenericType<? extends Object> targetType)
          This method converts the given pojo to the <TARGET>-type.
protected  Object convertRecursive(Object value, Object valueSource, GenericType<?> targetType, Class<?> currentTargetClass, ValueConverter previousConverter, ComposedValueConverterImpl.TargetClass2ConverterMap converterMap)
          This method performs the conversion recursive.
protected  void doInitialize()
          This method performs the actual initialization.
protected  boolean isAccepted(Class<?> type)
          This method determines if the given type is accepted as significant type for registration and lookup of ValueConverters.
protected  boolean isApplicable(Class<?> converterTargetClass, Class<?> expectedTargetClass)
          This method determines if the given converterTargetClass is applicable for the expectedTargetClass.
protected  boolean isApplicable(ValueConverter<?,?> converter, GenericType<?> targetType)
          This method determines if the given converter is applicable for the given targetType.
 void setConverters(List<ValueConverter<?,?>> converterList)
          This method allows to add a List of ValueConverters.
 
Methods inherited from class net.sf.mmm.util.value.base.AbstractComposedValueConverter
convert, convertValue, convertValue, getReflectionUtil, getSourceType, getTargetType, setReflectionUtil
 
Methods inherited from class net.sf.mmm.util.value.base.AbstractGenericValueConverter
convertValue, convertValue, convertValue, convertValue
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractLoggableComponent
getLogger, setLogger
 
Methods inherited from class net.sf.mmm.util.component.base.AbstractComponent
doInitialized, getInitializationState, initialize
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.sf.mmm.util.value.api.GenericValueConverter
convertValue, convertValue, convertValue, convertValue
 

Field Detail

targetClass2converterMap

private final ComposedValueConverterImpl.TargetClass2ConverterMap targetClass2converterMap
See Also:
addConverter(ValueConverter)

targetArrayClass2converterMap

private final ComposedValueConverterImpl.TargetClass2ConverterMap targetArrayClass2converterMap
See Also:
addConverter(ValueConverter)

converters

private List<ValueConverter<?,?>> converters
See Also:
setConverters(List)
Constructor Detail

ComposedValueConverterImpl

public ComposedValueConverterImpl()
The constructor.

Method Detail

doInitialize

protected void doInitialize()
This method performs the actual initialization. It is called when AbstractComponent.initialize() is invoked for the first time.
ATTENTION:
When you override this method from a sub-class you need to do a super.AbstractComponent.doInitialize().

Overrides:
doInitialize in class AbstractComposedValueConverter

addConverter

public void addConverter(ValueConverter<?,?> converter)
This method registers the given converter to this composed converter.

Parameters:
converter - is the converter to add.

addConverterInternal

private ValueConverter<?,?> addConverterInternal(ValueConverter<?,?> converter)
This method registers the given converter to this composed converter.

Parameters:
converter - is the converter to add.
Returns:
the converter with the same source-type and target-type that has been replaced by converter or null if no converter has been replaced.

setConverters

@Inject
public void setConverters(List<ValueConverter<?,?>> converterList)
This method allows to add a List of ValueConverters.

Parameters:
converterList - is the list of converters to register.

convert

public Object convert(Object value,
                      Object valueSource,
                      GenericType<? extends Object> targetType)
This method converts the given pojo to the <TARGET>-type.

Parameters:
value - is the value to convert.
valueSource - describes the source of the value. This may be the filename where the value was read from, an XPath where the value was located in an XML document, etc. It is used in exceptions thrown if something goes wrong. This will help to find the problem easier.
targetType - is the GenericType to convert the value to. It is potentially generic and therefore contains more detailed information than a Class. E.g. the targetType could be java.util.List<Long>. This could help e.g. if the value is a string like "2, 47, 4252525".
Returns:
the converted value or null if the conversion is NOT possible. The returned value has to be an instance of the given targetType.

isApplicable

protected boolean isApplicable(ValueConverter<?,?> converter,
                               GenericType<?> targetType)
This method determines if the given converter is applicable for the given targetType.

Parameters:
converter - is the ValueConverter to check.
targetType - is the GenericType to match with ValueConverter.getTargetType().
Returns:
true if the given converter is applicable, false otherwise.
See Also:
ValueConverter.getTargetType()

isApplicable

protected boolean isApplicable(Class<?> converterTargetClass,
                               Class<?> expectedTargetClass)
This method determines if the given converterTargetClass is applicable for the expectedTargetClass.

Parameters:
converterTargetClass - is the target-class of the ValueConverter to check.
expectedTargetClass - is the target-class to convert to.
Returns:
true if the conversion is applicable.

isAccepted

protected boolean isAccepted(Class<?> type)
This method determines if the given type is accepted as significant type for registration and lookup of ValueConverters. E.g. interfaces such as Cloneable or Serializable are not more significant than Object in order to choose the appropriate ValueConverter and should therefore be skipped when the Class-hierarchy is recursively traversed.
ATTENTION:
If this method returns false the behaviour differs between interfaces and regular classes. For an interface the entire traversal of super-interfaces is skipped, while for a regular class, just that class is skipped, but super-classes are recursively traversed.

Parameters:
type - is the Class reflecting the type to check.
Returns:
true if the given type is acceptable, false if the given type should be ignored.

convertRecursive

protected Object convertRecursive(Object value,
                                  Object valueSource,
                                  GenericType<?> targetType,
                                  Class<?> currentTargetClass,
                                  ValueConverter previousConverter,
                                  ComposedValueConverterImpl.TargetClass2ConverterMap converterMap)
This method performs the conversion recursive.

Parameters:
value - is the value to convert.
valueSource - describes the source of the value. This may be the filename where the value was read from, an XPath where the value was located in an XML document, etc. It is used in exceptions thrown if something goes wrong. This will help to find the problem easier.
targetType - is the GenericType to convert the value to.
currentTargetClass - is the current target-type to try.
previousConverter - is the converter that has been tried last time without success. It is used to avoid trying the same converter again. Will initially be null.
converterMap - is the ComposedValueConverterImpl.TargetClass2ConverterMap.
Returns:
the converted value or null if the conversion is NOT possible. The returned value has to be an instance of the given targetType.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.