net.sf.morph.transform.converters
Class TextToNumberConverter

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

public class TextToNumberConverter
extends BaseTransformer
implements DecoratedConverter

Converts basic text types into primitive numbers or Number objects.

Since:
Jan 4, 2005
Author:
Matt Sgarlata

Field Summary
static int CURRENCY_IGNORE
          Constant indicating currency symbols should be ignored when converting text to numbers.
static int CURRENCY_REJECT
          Constant indicating the presence of currency symbols in text should prevent conversion of the text into a number (i.e.
static int PARENTHESES_IGNORE
          Constant indicating parentheses should be ignored when converting text to numbers.
static int PARENTHESES_NEGATE
          Constant indicating parentheses enclosing a number should cause the text to be treated as a negative number.
static int PARENTHESES_REJECT
          Constant indicating the presence of parentheses in text should prevent conversion of the text into a number (i.e.
static int PERCENTAGE_CONVERT_TO_DECIMAL
          Constant indicating a percentage symbol at the end of text should cause the text to be treated as a percentage and converted into a corresponding decimal number.
static int PERCENTAGE_IGNORE
          Constant indicating percentage symbols should be ignored when converting text to numbers.
static int PERCENTAGE_REJECT
          Constant indicating the presence of percentage symbols in text should prevent conversion of the text into a number (i.e.
static int WHITESPACE_IGNORE
          Constant indicating whitespace characters should be ignored when converting text to numbers.
static int WHITESPACE_REJECT
          Constant indicating the presence of whitespace characters in text should prevent conversion of the text into a number (i.e.
 
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
TextToNumberConverter()
           
 
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.
 int getCurrencyHandling()
          Retrieves the configuration option indicating how currencies should be treated by this converter.
protected  java.lang.Class[] getDestinationClassesImpl()
          Transformer.getDestinationClasses() implementation template method.
 Converter getNumberConverter()
          Sets the converter used to convert text types from one type to another.
 int getParenthesesHandling()
          Retrieves the configuration option indicating how parantheses should be treated by this converter.
 int getPercentageHandling()
          Gets the configuration option indicating how percentages should be treated by this converter.
protected  java.lang.Class[] getSourceClassesImpl()
          Transformer.getSourceClasses() implementation template method.
 Converter getTextConverter()
          Gets the converter used to convert text types from one type to another.
 int getWhitespaceHandling()
          Gets the configuration option indicating how whitespace should be treated by this converter.
protected  boolean isParseSuccessful(java.lang.String stringWithoutIgnoredSymbolsStr, java.text.ParsePosition position)
          Learn whether the entire string was consumed.
protected  boolean isWrappingRuntimeExceptions()
          Indicates whether runtime exceptions should be wrapped as TransformationExceptions.
 void setCurrencyHandling(int currencyHandling)
          Sets the configuration option indicating how currencies should be treated by this converter.
 void setNumberConverter(Converter numberConverter)
          Sets the converter used to convert text types from one type to another.
 void setParenthesesHandling(int parenthesesHandling)
          Sets the configuration option indicating how parantheses should be treated by this converter.
 void setPercentageHandling(int percentageHandling)
          Sets the configuration option indicating how percentages should be treated by this converter.
 void setTextConverter(Converter textConverter)
          Sets the converter used to convert text types from one type to another.
 void setWhitespaceHandling(int whitespaceHandling)
          Sets the configuration option indicating how whitespace should be treated by this converter.
 
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, isImpreciseTransformationImpl, 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
 

Field Detail

WHITESPACE_IGNORE

public static final int WHITESPACE_IGNORE
Constant indicating whitespace characters should be ignored when converting text to numbers. This is the default treatment of whitespace characters by this converter.

See Also:
Constant Field Values

WHITESPACE_REJECT

public static final int WHITESPACE_REJECT
Constant indicating the presence of whitespace characters in text should prevent conversion of the text into a number (i.e. a TransformationException will be thrown if whitespace is in the text).

See Also:
Constant Field Values

CURRENCY_IGNORE

public static final int CURRENCY_IGNORE
Constant indicating currency symbols should be ignored when converting text to numbers. This is the default treatment of currency symbols by this converter.

See Also:
Constant Field Values

CURRENCY_REJECT

public static final int CURRENCY_REJECT
Constant indicating the presence of currency symbols in text should prevent conversion of the text into a number (i.e. a TransformationException will be thrown if currency symbols are present in the text).

See Also:
Constant Field Values

PERCENTAGE_IGNORE

public static final int PERCENTAGE_IGNORE
Constant indicating percentage symbols should be ignored when converting text to numbers.

See Also:
Constant Field Values

PERCENTAGE_REJECT

public static final int PERCENTAGE_REJECT
Constant indicating the presence of percentage symbols in text should prevent conversion of the text into a number (i.e. a TransformationException will be thrown if percentage symbols are present in the text).

See Also:
Constant Field Values

PERCENTAGE_CONVERT_TO_DECIMAL

public static final int PERCENTAGE_CONVERT_TO_DECIMAL
Constant indicating a percentage symbol at the end of text should cause the text to be treated as a percentage and converted into a corresponding decimal number. For example, 10 would be converted to 10 and 10% would be converted to .10. If there are percentage symbols in any position other than the last character of the text, a TransformationException will be thrown. This is the default treatment of percentages by this converter.

See Also:
Constant Field Values

PARENTHESES_IGNORE

public static final int PARENTHESES_IGNORE
Constant indicating parentheses should be ignored when converting text to numbers.

See Also:
Constant Field Values

PARENTHESES_REJECT

public static final int PARENTHESES_REJECT
Constant indicating the presence of parentheses in text should prevent conversion of the text into a number (i.e. a TransformationException will be thrown if parentheses are present in the text).

See Also:
Constant Field Values

PARENTHESES_NEGATE

public static final int PARENTHESES_NEGATE
Constant indicating parentheses enclosing a number should cause the text to be treated as a negative number. For example, 10 would be converted to 10 and (10) would be converted to -10. If there are parentheses in positions other than the first or last character of the text, a TransformationException will be thrown. This is the default treatment of parentheses by this converter.

See Also:
Constant Field Values
Constructor Detail

TextToNumberConverter

public TextToNumberConverter()
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

isParseSuccessful

protected boolean isParseSuccessful(java.lang.String stringWithoutIgnoredSymbolsStr,
                                    java.text.ParsePosition position)
Learn whether the entire string was consumed.

Parameters:
stringWithoutIgnoredSymbolsStr -
position -
Returns:
boolean

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

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

getNumberConverter

public Converter getNumberConverter()
Sets the converter used to convert text types from one type to another.

Returns:
the converter used to convert text types from one type to another

setNumberConverter

public void setNumberConverter(Converter numberConverter)
Sets the converter used to convert text types from one type to another.

Parameters:
numberConverter - the converter used to convert text types from one type to another

getTextConverter

public Converter getTextConverter()
Gets the converter used to convert text types from one type to another.

Returns:
the converter used to convert text types from one type to another

setTextConverter

public void setTextConverter(Converter textConverter)
Sets the converter used to convert text types from one type to another.

Parameters:
textConverter - the converter used to convert text types from one type to another

getCurrencyHandling

public int getCurrencyHandling()
Retrieves the configuration option indicating how currencies should be treated by this converter. Default is CURRENCY_IGNORE.

Returns:
the configuration option indicating how currencies should be treated by this converter

setCurrencyHandling

public void setCurrencyHandling(int currencyHandling)
Sets the configuration option indicating how currencies should be treated by this converter. Default is CURRENCY_IGNORE.

Parameters:
currencyHandling - the configuration option indicating how currencies should be treated by this converter. Default is CURRENCY_IGNORE.

getParenthesesHandling

public int getParenthesesHandling()
Retrieves the configuration option indicating how parantheses should be treated by this converter. Default is PARENTHESES_NEGATE.

Returns:
the configuration option indicating how parantheses should be treated by this converter. Default is PARENTHESES_NEGATE.

setParenthesesHandling

public void setParenthesesHandling(int parenthesesHandling)
Sets the configuration option indicating how parantheses should be treated by this converter. Default is PARENTHESES_NEGATE.

Parameters:
parenthesesHandling - the configuration option indicating how parantheses should be treated by this converter. Default is PARENTHESES_NEGATE.

getPercentageHandling

public int getPercentageHandling()
Gets the configuration option indicating how percentages should be treated by this converter. Default is PERCENTAGE_CONVERT_TO_DECIMAL.

Returns:
the configuration option indicating how percentages should be treated by this converter. Default is PERCENTAGE_CONVERT_TO_DECIMAL.

setPercentageHandling

public void setPercentageHandling(int percentageHandling)
Sets the configuration option indicating how percentages should be treated by this converter. Default is PERCENTAGE_CONVERT_TO_DECIMAL.

Parameters:
percentageHandling - the configuration option indicating how percentages should be treated by this converter. Default is PERCENTAGE_CONVERT_TO_DECIMAL.

getWhitespaceHandling

public int getWhitespaceHandling()
Gets the configuration option indicating how whitespace should be treated by this converter. Default is WHITESPACE_IGNORE.

Returns:
the configuration option indicating how whitespace should be treated by this converter. Default is WHITESPACE_IGNORE.

setWhitespaceHandling

public void setWhitespaceHandling(int whitespaceHandling)
Sets the configuration option indicating how whitespace should be treated by this converter. Default is WHITESPACE_IGNORE.

Parameters:
whitespaceHandling - the configuration option indicating how whitespace should be treated by this converter. Default is WHITESPACE_IGNORE.


Copyright © 2004-2008.