net.sf.morph.transform.converters
Class NumberToTextConverter

java.lang.Object
  extended by net.sf.morph.transform.transformers.BaseTransformer
      extended by net.sf.morph.transform.converters.NumberToTextConverter
All Implemented Interfaces:
net.sf.composite.Component, Converter, DecoratedConverter, DecoratedTransformer, ExplicitTransformer, ImpreciseTransformer, Transformer

public class NumberToTextConverter
extends BaseTransformer
implements DecoratedConverter, ImpreciseTransformer

Converts Numbers into basic text types (String, StringBuffer or Character).

Since:
Jan 26, 2006
Author:
Matt Sgarlata

Field Summary
 
Fields inherited from class net.sf.morph.transform.transformers.BaseTransformer
destinationClasses, log, sourceClasses
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Fields inherited from interface net.sf.morph.transform.Transformer
TRANSFORMATION_TYPE_CONVERT, TRANSFORMATION_TYPE_COPY
 
Constructor Summary
NumberToTextConverter()
           
 
Method Summary
protected  java.lang.Object convertImpl(java.lang.Class destinationClass, java.lang.Object source, java.util.Locale locale)
          The implementation of the convert method, which may omit the invalid argument checks already performed by this base class.
protected  java.lang.Class[] getDestinationClassesImpl()
          Transformer.getDestinationClasses() implementation template method.
 Converter getNumberConverter()
          Get the number converter used by this NumberToTextConverter.
protected  java.text.NumberFormat getNumberFormat(java.util.Locale locale)
          Retrieves the NumberFormat instance to be used to in the given locale to format numbers as text.
protected  java.lang.Class[] getSourceClassesImpl()
          Transformer.getSourceClasses() implementation template method.
 Converter getTextConverter()
          Get the text converter used by this NumberToTextConverter.
protected  boolean isImpreciseTransformationImpl(java.lang.Class destinationClass, java.lang.Class sourceClass)
          Implementation of isImpreciseTransformation
protected  boolean isWrappingRuntimeExceptions()
          Indicates whether runtime exceptions should be wrapped as TransformationExceptions.
 void setNumberConverter(Converter numberConverter)
          Sets the numberConverter to be used.
 void setTextConverter(Converter textConverter)
          Sets the textConverter to be used.
 
Methods inherited from class net.sf.morph.transform.transformers.BaseTransformer
clone, convert, convert, copy, copy, copyImpl, createDefaultReflector, createNewInstance, createNewInstanceImpl, createReusableSource, equals, equals, equalsUnidirectionalTest, getDestinationClasses, getInstantiatingReflector, getLocale, getLog, getNestedTransformer, getReflector, getReflector, getSourceClasses, getTransformableCallCache, getTransformerName, initialize, initializeImpl, isAutomaticallyHandlingNulls, isCachingIsTransformableCalls, isImpreciseTransformation, isInitialized, isPerformingLogging, isTransformable, isTransformableImpl, setCachingIsTransformableCalls, setDestinationClasses, setInitialized, setLog, setNestedTransformer, setReflector, setSourceClasses, setTransformableCallCache, setTransformerName, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.morph.transform.DecoratedConverter
convert, equals, equals
 
Methods inherited from interface net.sf.morph.transform.Converter
convert
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.Transformer
getDestinationClasses, getSourceClasses
 
Methods inherited from interface net.sf.morph.transform.ExplicitTransformer
isTransformable
 
Methods inherited from interface net.sf.morph.transform.ImpreciseTransformer
isImpreciseTransformation
 

Constructor Detail

NumberToTextConverter

public NumberToTextConverter()
Method Detail

convertImpl

protected java.lang.Object convertImpl(java.lang.Class destinationClass,
                                       java.lang.Object source,
                                       java.util.Locale locale)
                                throws java.lang.Exception
The implementation of the convert method, which may omit the invalid argument checks already performed by this base class. By default, this method creates a new instance of the destinationClass and copies information from the source to the destination. This implementation should be fine as-is for Copiers, but Converters will need to implement this method since they will not be implementing the copy method.

Overrides:
convertImpl in class BaseTransformer
locale - the locale in which the conversion should take place. for converters that are not locale-aware, the local argument can simply be ignored
Throws:
java.lang.Exception

getSourceClassesImpl

protected java.lang.Class[] getSourceClassesImpl()
                                          throws java.lang.Exception
Transformer.getSourceClasses() implementation template method.

Specified by:
getSourceClassesImpl in class BaseTransformer
Returns:
Class[]
Throws:
java.lang.Exception

getDestinationClassesImpl

protected java.lang.Class[] getDestinationClassesImpl()
                                               throws java.lang.Exception
Transformer.getDestinationClasses() implementation template method.

Specified by:
getDestinationClassesImpl in class BaseTransformer
Returns:
Class[]
Throws:
java.lang.Exception

isImpreciseTransformationImpl

protected boolean isImpreciseTransformationImpl(java.lang.Class destinationClass,
                                                java.lang.Class sourceClass)
Implementation of isImpreciseTransformation

Overrides:
isImpreciseTransformationImpl in class BaseTransformer
Returns:
boolean

getNumberConverter

public Converter getNumberConverter()
Get the number converter used by this NumberToTextConverter.

Returns:
Converter

setNumberConverter

public void setNumberConverter(Converter numberConverter)
Sets the numberConverter to be used. Note that this method should be called before the transformer is used. Otherwise, if another thread is in the middle of transforming an object graph and this method is called, the behavior of the transformer can change partway through the transformation.

Parameters:
numberConverter - the numberConverter to be used

getTextConverter

public Converter getTextConverter()
Get the text converter used by this NumberToTextConverter.

Returns:
Converter

setTextConverter

public void setTextConverter(Converter textConverter)
Sets the textConverter to be used. Note that this method should be called before the transformer is used. Otherwise, if another thread is in the middle of transforming an object graph and this method is called, the behavior of the transformer can change partway through the transformation.

Parameters:
textConverter - the textConverter to be used

getNumberFormat

protected java.text.NumberFormat getNumberFormat(java.util.Locale locale)
Retrieves the NumberFormat instance to be used to in the given locale to format numbers as text. Subclasses can override this method to customize the behavior of this converter.

Parameters:
locale - the locale
Returns:
the NumberFormat instance to be used to in the given locale to format numbers as text

isWrappingRuntimeExceptions

protected boolean isWrappingRuntimeExceptions()
Indicates whether runtime exceptions should be wrapped as TransformationExceptions. By default, this method returns true.

Simple transformers in Morph that operate on JDK types like Numbers and Strings will usually set this value to true so that they throw TransformationExceptions if problems occur. More complex transformers that operate on graphs of objects are encouraged to set this value to false so that runtime exceptions are not wrapped. This way, problems accessing data will be expressed by the native API of a user's domain objects and avoid the need to catch Morph-specific exceptions (assuming the use of runtime exceptions in said domain objects).

Overrides:
isWrappingRuntimeExceptions in class BaseTransformer
Returns:
true


Copyright © 2004-2008.