net.sf.mmm.util.value.impl
Class AbstractValueConverterToContainer<CONTAINER>

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.AbstractValueConverter<SOURCE,TARGET>
              extended by net.sf.mmm.util.value.base.AbstractRecursiveValueConverter<Object,CONTAINER>
                  extended by net.sf.mmm.util.value.impl.AbstractValueConverterToContainer<CONTAINER>
Type Parameters:
CONTAINER - is the generic type of the container.
All Implemented Interfaces:
SimpleValueConverter<Object,CONTAINER>, ValueConverter<Object,CONTAINER>
Direct Known Subclasses:
AbstractConverterToArray, ValueConverterToCollection, ValueConverterToMap

@Singleton
@Named
public abstract class AbstractValueConverterToContainer<CONTAINER>
extends AbstractRecursiveValueConverter<Object,CONTAINER>

This is the abstract base-implementation of a ValueConverter that converts an Object to a container type. A container type is an array, Collection or Map. It supports objects given as CharSequence (e.g. String), Collection, or array. If a value is given as CharSequence it will be parsed as comma-separated values. An individual value can be escaped by enclosing it with "<{[" and "]}>" so it can itself contain the separator character.
Here are some examples:

value target-type result
"123" List<Integer> {123}
"1, 2,3" int[] {1,2,3}
"a, <{[b,c,d]}>,e" List<List<Character>> {{'a'},{'b','c','d'},{'e'}}
"42=true,84=false" Map<Integer, Boolean> {42->true, 84->false}

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

Field Summary
private  CollectionReflectionUtil collectionReflectionUtil
           
protected static String ELEMENT_ESCAPE_END
          The suffix used to escape an element (that may contain ELEMENT_SEPARATOR).
protected static String ELEMENT_ESCAPE_START
          The prefix used to escape an element (that may contain ELEMENT_SEPARATOR).
protected static char ELEMENT_SEPARATOR
          The character used to separate the element of the collection.
 
Constructor Summary
AbstractValueConverterToContainer()
          The constructor.
 
Method Summary
 CONTAINER convert(Object value, Object valueSource, GenericType<? extends CONTAINER> targetType)
          This method converts the given pojo to the <TARGET>-type.
protected abstract  void convertContainerEntry(Object element, int index, CONTAINER container, Object valueSource, GenericType<? extends CONTAINER> targetType, Object value)
          This method converts a single entry of a container.
protected  CONTAINER convertFromArray(Object arrayValue, Object valueSource, GenericType<? extends CONTAINER> targetType)
          This method performs the conversion for array values.
protected  CONTAINER convertFromCollection(Collection collectionValue, Object valueSource, GenericType<? extends CONTAINER> targetType)
          This method performs the conversion for Collection values.
protected  CONTAINER convertFromString(String stringValue, Object valueSource, GenericType<? extends CONTAINER> targetType)
          This method performs the conversion for String values.
protected abstract  CONTAINER createContainer(GenericType<? extends CONTAINER> targetType, int length)
          This method creates the according container for the given containerType.
protected  void doInitialize()
          This method performs the actual initialization.
protected  CollectionReflectionUtil getCollectionReflectionUtil()
          This method gets the CollectionReflectionUtilImpl instance to use.
 Class<Object> getSourceType()
          This the type of the value accepted by this converter.
 void setCollectionReflectionUtil(CollectionReflectionUtil collectionReflectionUtil)
           
 
Methods inherited from class net.sf.mmm.util.value.base.AbstractRecursiveValueConverter
getComposedValueConverter, setComposedValueConverter
 
Methods inherited from class net.sf.mmm.util.value.base.AbstractValueConverter
convert, getReflectionUtil, setReflectionUtil
 
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.ValueConverter
getTargetType
 

Field Detail

ELEMENT_SEPARATOR

protected static final char ELEMENT_SEPARATOR
The character used to separate the element of the collection.

See Also:
Constant Field Values

ELEMENT_ESCAPE_START

protected static final String ELEMENT_ESCAPE_START
The prefix used to escape an element (that may contain ELEMENT_SEPARATOR).

See Also:
Constant Field Values

ELEMENT_ESCAPE_END

protected static final String ELEMENT_ESCAPE_END
The suffix used to escape an element (that may contain ELEMENT_SEPARATOR).

See Also:
Constant Field Values

collectionReflectionUtil

private CollectionReflectionUtil collectionReflectionUtil
See Also:
getCollectionReflectionUtil()
Constructor Detail

AbstractValueConverterToContainer

public AbstractValueConverterToContainer()
The constructor.

Method Detail

getCollectionReflectionUtil

protected CollectionReflectionUtil getCollectionReflectionUtil()
This method gets the CollectionReflectionUtilImpl instance to use.

Returns:
the CollectionReflectionUtilImpl to use.

setCollectionReflectionUtil

@Inject
public void setCollectionReflectionUtil(CollectionReflectionUtil collectionReflectionUtil)
Parameters:
collectionReflectionUtil - is the collectionReflectionUtil to set

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 AbstractRecursiveValueConverter<Object,CONTAINER>

getSourceType

public Class<Object> getSourceType()
This the type of the value accepted by this converter. Use Object if you want to accept any value. A very common source-type is String.

Returns:
the source-type.

convert

public CONTAINER convert(Object value,
                         Object valueSource,
                         GenericType<? extends CONTAINER> 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.

convertFromCollection

protected CONTAINER convertFromCollection(Collection collectionValue,
                                          Object valueSource,
                                          GenericType<? extends CONTAINER> targetType)
This method performs the conversion for Collection values.

Parameters:
collectionValue - is the Collection value to convert.
valueSource - describes the source of the value or null if NOT available.
targetType - is the target-type to convert to.
Returns:
the converted container.

convertFromString

protected CONTAINER convertFromString(String stringValue,
                                      Object valueSource,
                                      GenericType<? extends CONTAINER> targetType)
This method performs the conversion for String values.

Parameters:
stringValue - is the String value to convert.
valueSource - describes the source of the value or null if NOT available.
targetType - is the target-type to convert to.
Returns:
the converted container.

convertFromArray

protected CONTAINER convertFromArray(Object arrayValue,
                                     Object valueSource,
                                     GenericType<? extends CONTAINER> targetType)
This method performs the conversion for array values.

Parameters:
arrayValue - is the array value to convert.
valueSource - describes the source of the value or null if NOT available.
targetType - is the target-type to convert to.
Returns:
the converted container.

convertContainerEntry

protected abstract void convertContainerEntry(Object element,
                                              int index,
                                              CONTAINER container,
                                              Object valueSource,
                                              GenericType<? extends CONTAINER> targetType,
                                              Object value)
This method converts a single entry of a container.

Parameters:
element - is the single entry (element) of the container to convert.
index - is the index of the given element in the order of occurrence.
container - is the current container where to add the given element as entry.
valueSource - describes the source of the value or null if NOT available.
targetType - is the target-type to convert to.
value - is the original value to convert.

createContainer

protected abstract CONTAINER createContainer(GenericType<? extends CONTAINER> targetType,
                                             int length)
This method creates the according container for the given containerType.

Parameters:
targetType - is the GenericType of the container.
length - is the length (or capacity) of the container to create.
Returns:
the container instance.


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