Package io.guise.framework.converter
Class AbstractNumberStringLiteralConverter<V extends java.lang.Number>
- java.lang.Object
-
- com.globalmentor.beans.BoundPropertyObject
-
- io.guise.framework.event.GuiseBoundPropertyObject
-
- io.guise.framework.converter.AbstractConverter<V,java.lang.String>
-
- io.guise.framework.converter.AbstractNumberStringLiteralConverter<V>
-
- Type Parameters:
V
- The value type this converter supports.
- All Implemented Interfaces:
com.globalmentor.beans.PropertyBindable
,com.globalmentor.beans.PropertyConstrainable
,Converter<V,java.lang.String>
,NumberStringLiteralConverter<V>
- Direct Known Subclasses:
DoubleStringLiteralConverter
,FloatStringLiteralConverter
,IntegerStringLiteralConverter
,LongStringLiteralConverter
public abstract class AbstractNumberStringLiteralConverter<V extends java.lang.Number> extends AbstractConverter<V,java.lang.String> implements NumberStringLiteralConverter<V>
An abstract implementation an object that can convert a number from and to a string. If the currency style is chosen, care should be taken to indicate a specific constant currency unless it is desired that the currency type change whenever the locale changes. This implementation caches a number format and only creates a new one if the locale has changed. Child classes should overridecreateNumberFormat(Locale)
if custom number formats are desired. This implementation synchronizes all conversions on theNumberFormat
object.- Author:
- Garret Wilson
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.guise.framework.converter.NumberStringLiteralConverter
NumberStringLiteralConverter.Style
-
-
Field Summary
-
Fields inherited from class com.globalmentor.beans.BoundPropertyObject
NO_PROPERTY_CHANGE_LISTENERS, NO_VETOABLE_CHANGE_LISTENERS
-
Fields inherited from interface io.guise.framework.converter.Converter
INVALID_VALUE_MESSAGE_PROPERTY
-
Fields inherited from interface io.guise.framework.converter.NumberStringLiteralConverter
EUR_CURRENCY, USD_CURRENCY
-
-
Constructor Summary
Constructors Constructor Description AbstractNumberStringLiteralConverter(NumberStringLiteralConverter.Style style, java.util.Currency currency)
Style and currency constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
convertValue(V value)
Converts a value from the value space to a literal value in the lexical space.protected java.text.NumberFormat
createNumberFormat(java.util.Locale locale)
Creates a new number format object for the indicated locale.java.util.Currency
getCurrency()
protected java.text.NumberFormat
getNumberFormat()
NumberStringLiteralConverter.Style
getStyle()
protected java.lang.Number
parseNumber(java.lang.String literal)
Converts a literal representation of a value from the lexical space into a number.-
Methods inherited from class io.guise.framework.converter.AbstractConverter
getInvalidValueMessage, isEquivalent, isValidLiteral, setInvalidValueMessage
-
Methods inherited from class io.guise.framework.event.GuiseBoundPropertyObject
getSession
-
Methods inherited from class com.globalmentor.beans.BoundPropertyObject
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, createPostponedPropertyChangeEvent, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, fireVetoableChange, getForwardPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getRepeatPropertyChangeListener, getRepeatVetoableChangeListener, getVetoableChangeListeners, getVetoableChangeListeners, getVetoableChangeSupport, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.guise.framework.converter.Converter
convertLiteral, getInvalidValueMessage, getSession, isEquivalent, isValidLiteral, setInvalidValueMessage
-
-
-
-
Constructor Detail
-
AbstractNumberStringLiteralConverter
public AbstractNumberStringLiteralConverter(NumberStringLiteralConverter.Style style, java.util.Currency currency)
Style and currency constructor.- Parameters:
style
- The representation style.currency
- The constant currency type to use, ornull
if currency representation is not requested or the currency should be dynamically determined by the locale.- Throws:
java.lang.NullPointerException
- if the given style isnull
.java.lang.IllegalArgumentException
- if a currency is provided for a style other thanNumberStringLiteralConverter.Style.CURRENCY
.
-
-
Method Detail
-
getStyle
public NumberStringLiteralConverter.Style getStyle()
- Returns:
- The representation style.
-
getCurrency
public java.util.Currency getCurrency()
- Returns:
- The constant currency type to use, or
null
if currency representation is not requested or the currency should be dynamically determined by the locale.
-
getNumberFormat
protected java.text.NumberFormat getNumberFormat()
- Returns:
- A number format object appropriate for the session's current locale.
-
createNumberFormat
protected java.text.NumberFormat createNumberFormat(java.util.Locale locale)
Creates a new number format object for the indicated locale. //TODO del This implementation defaults to formatting as many fraction digits as is possible to keep the value synchronized with the lexical form.- Parameters:
locale
- The locale for which a number format should be created.- Returns:
- A number format object appropriate for the given locale.
- See Also:
getStyle()
-
convertValue
public java.lang.String convertValue(V value) throws ConversionException
Converts a value from the value space to a literal value in the lexical space.This implementation converts the value using the number format object. This implementation synchronizes on the
NumberFormat
instance.- Specified by:
convertValue
in interfaceConverter<V extends java.lang.Number,java.lang.String>
- Parameters:
value
- The value in the value space to convert.- Returns:
- The converted value in the lexical space, or
null
if the given literal isnull
. - Throws:
ConversionException
- if the value cannot be converted.
-
parseNumber
protected java.lang.Number parseNumber(java.lang.String literal) throws ConversionException
Converts a literal representation of a value from the lexical space into a number. This implementation converts the empty string to anull
value. This implementation adds any appropriate symbols, such as a percent sign or currency symbol, if needed.- Parameters:
literal
- The literal value in the lexical space to convert.- Returns:
- A number representing the converted value in the value space in either
Long
orDouble
form, ornull
if the given literal isnull
or the empty string. - Throws:
ConversionException
- if the literal value cannot be converted.
-
-