net.sf.mmm.util.value.base
Class StringValueConverterImpl

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<String>
              extended by net.sf.mmm.util.value.base.StringValueConverterImpl
All Implemented Interfaces:
GenericValueConverter<String>, StringValueConverter

@Singleton
@Named
public class StringValueConverterImpl
extends AbstractGenericValueConverter<String>
implements StringValueConverter

This is a utility class providing support for dealing with values (e.g. when reading configurations).

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

Field Summary
private static StringValueConverterImpl instance
           
private  Iso8601Util iso8601Util
           
private  StringUtil stringUtil
           
 
Constructor Summary
StringValueConverterImpl()
          The constructor.
 
Method Summary
protected
<V> V
convertUnknownValue(String value, Class<V> type, Object valueSource)
          This method converts the given String-value to the given type.
<TARGET> TARGET
convertValue(String value, Object valueSource, Class<TARGET> type, Type targetType)
          This method converts the given value to the given type.
protected  void doInitialize()
          This method performs the actual initialization.
static StringValueConverterImpl getInstance()
          This method gets the singleton instance of this StringValueConverterImpl.
protected  Iso8601Util getIso8601Util()
          This method gets the Iso8601Util used to parse and format date and time according to the standard ISO-8601.
protected  StringUtil getStringUtil()
          This method gets the StringUtil used to deal with strings.
private static Number parseNumber(String numberValue, Object valueSource)
          This method parses a numeric value.
 void setIso8601Util(Iso8601Util iso8601Util)
          This method sets the Iso8601Util.
 void setStringUtil(StringUtil stringUtil)
           
 
Methods inherited from class net.sf.mmm.util.value.base.AbstractGenericValueConverter
convertValue, 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, convertValue
 

Field Detail

instance

private static StringValueConverterImpl instance
See Also:
getInstance()

iso8601Util

private Iso8601Util iso8601Util
See Also:
getIso8601Util()

stringUtil

private StringUtil stringUtil
See Also:
getStringUtil()
Constructor Detail

StringValueConverterImpl

public StringValueConverterImpl()
The constructor.

Method Detail

getInstance

public static StringValueConverterImpl getInstance()
This method gets the singleton instance of this StringValueConverterImpl.
This design is the best compromise between easy access (via this indirection you have direct, static access to all offered functionality) and IoC-style design which allows extension and customization.
For IoC usage, simply ignore all static getInstance() methods and construct new instances via the container-framework of your choice (like plexus, pico, springframework, etc.). To wire up the dependent components everything is properly annotated using common-annotations (JSR-250). If your container does NOT support this, you should consider using a better one.

Returns:
the singleton instance.

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 AbstractLoggableComponent

getIso8601Util

protected Iso8601Util getIso8601Util()
This method gets the Iso8601Util used to parse and format date and time according to the standard ISO-8601.

Returns:
the iso8601Util

setIso8601Util

@Inject
public void setIso8601Util(Iso8601Util iso8601Util)
This method sets the Iso8601Util.

Parameters:
iso8601Util - the iso8601Util to set

getStringUtil

protected StringUtil getStringUtil()
This method gets the StringUtil used to deal with strings.

Returns:
the stringUtil

setStringUtil

@Inject
public void setStringUtil(StringUtil stringUtil)
Parameters:
stringUtil - is the stringUtil to set

parseNumber

private static Number parseNumber(String numberValue,
                                  Object valueSource)
                           throws WrongValueTypeException
This method parses a numeric value.

Parameters:
numberValue - is the number value as string.
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.
Returns:
the value as number.
Throws:
WrongValueTypeException - if the given string is no number.

convertValue

public final <TARGET> TARGET convertValue(String value,
                                          Object valueSource,
                                          Class<TARGET> type,
                                          Type targetType)
                          throws ValueNotSetException,
                                 WrongValueTypeException
This method converts the given value to the given type.

Specified by:
convertValue in interface GenericValueConverter<String>
Type Parameters:
TARGET - is the type to convert to.
Parameters:
value - is the value to convert. It may be null.
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.
type - is the type the value should be converted to. It is the raw-type of the given targetType.
targetType - is the type to convert the value to. It is potentially generic and therefore contains more detailed information than targetClass. E.g. the targetClass may be java.util.List while this targetType could be java.util.List<Long>. This could help e.g. if the value is a string like "2, 47, 4252525". The caller may supply the targetClass again here.
Returns:
the value converted to type.
Throws:
ValueNotSetException - if the given value is null.
WrongValueTypeException - if the given value is NOT null but can NOT be converted to the given type (e.g. if value is "12x" and type is Integer.class).

convertUnknownValue

protected <V> V convertUnknownValue(String value,
                                    Class<V> type,
                                    Object valueSource)
                         throws ValueNotSetException,
                                WrongValueTypeException
This method converts the given String-value to the given type. It is called from convertValue(String, Object, Class, Type) if the given type is unknown. This default implementation simply throws a new WrongValueTypeException. You can extend this class and override this method in order to support the conversion for additional types. You should first handle the conversion for all value types you like. Then for all other types you should delegate to the super method implementation.

Type Parameters:
V - is the type the value should be converted to.
Parameters:
value - is the value to convert.
type - is the type the value should be converted to.
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.
Returns:
the value converted to type.
Throws:
ValueNotSetException - if the given value is null.
WrongValueTypeException - if the given value is NOT null but can NOT be converted to the given type (e.g. if value is "12x" and type is Integer.class).


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