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

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.AbstractSimpleValueConverter<Object,Number>
              extended by net.sf.mmm.util.value.impl.ValueConverterToNumber
All Implemented Interfaces:
SimpleValueConverter<Object,Number>, ValueConverter<Object,Number>

@Singleton
@Named
public class ValueConverterToNumber
extends AbstractSimpleValueConverter<Object,Number>

This is an implementation of the ValueConverter interface that converts an Object to a Number. It supports objects given as CharSequence (e.g. String) or Number.

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

Field Summary
private  MathUtil mathUtil
           
 
Constructor Summary
ValueConverterToNumber()
          The constructor.
 
Method Summary
 Number convert(Object value, Object valueSource, Class<? extends Number> targetClass)
          This method converts the given pojo to the <TARGET>-type.
protected  void doInitialize()
          This method performs the actual initialization.
protected  MathUtil getMathUtil()
          This method gets the MathUtil to use.
 Class<Object> getSourceType()
          This the type of the value accepted by this converter.
 Class<Number> getTargetType()
          Is the guaranteed return-type of the conversion.
protected  boolean isFailIfUnprecise()
          This method determines if the conversion from one number-type to another should fail if it is unprecise.
 void setMathUtil(MathUtil mathUtil)
          This method set the MathUtil to use.
 
Methods inherited from class net.sf.mmm.util.value.base.AbstractSimpleValueConverter
convert
 
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
 

Field Detail

mathUtil

private MathUtil mathUtil
See Also:
getMathUtil()
Constructor Detail

ValueConverterToNumber

public ValueConverterToNumber()
The constructor.

Method Detail

getMathUtil

protected MathUtil getMathUtil()
This method gets the MathUtil to use.

Returns:
the MathUtil instance.

setMathUtil

@Inject
public void setMathUtil(MathUtil mathUtil)
This method set the MathUtil to use.

Parameters:
mathUtil - is the MathUtil 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

isFailIfUnprecise

protected boolean isFailIfUnprecise()
This method determines if the conversion from one number-type to another should fail if it is unprecise.

Returns:
the fail-if-unprecise flag.
See Also:
NumberType.valueOf(Number, boolean)

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.

getTargetType

public Class<Number> getTargetType()
Is the guaranteed return-type of the conversion. This information is used externally to choose the most specific ValueConverter that is appropriate for the conversion.
E.g. a generic converter can have Object as target-type while a specific converter may have Collection as target-type. Now if an object (compliant with the source-type) needs to be converted to a Collection or List, the specific converter is used while for other objects the generic converter is chosen.
Please note that the target-type is often more general than the actual returned result. So a ValueConverter that converts a comma-separated String to an ArrayList will typically declare List as target-type.

Returns:
the target-type.

convert

public Number convert(Object value,
                      Object valueSource,
                      Class<? extends Number> targetClass)
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.
targetClass - is the type to convert the value to.
Returns:
the converted value or null if the conversion is NOT possible. The returned value has to be an instance of the given targetType.
See Also:
ValueConverter.convert(Object, Object, GenericType)


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