Package io.guise.framework.converter
Class AbstractNumberStringLiteralConverter<V extends Number>
java.lang.Object
com.globalmentor.beans.BoundPropertyObject
io.guise.framework.event.GuiseBoundPropertyObject
io.guise.framework.converter.AbstractConverter<V,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,
,String> NumberStringLiteralConverter<V>
- Direct Known Subclasses:
DoubleStringLiteralConverter
,FloatStringLiteralConverter
,IntegerStringLiteralConverter
,LongStringLiteralConverter
public abstract class AbstractNumberStringLiteralConverter<V extends Number>
extends AbstractConverter<V,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 override
createNumberFormat(Locale)
if custom number formats are desired.
This implementation synchronizes all conversions on the NumberFormat
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
ConstructorDescriptionStyle and currency constructor. -
Method Summary
Modifier and TypeMethodDescriptionconvertValue
(V value) Converts a value from the value space to a literal value in the lexical space.protected NumberFormat
createNumberFormat
(Locale locale) Creates a new number format object for the indicated locale.protected NumberFormat
getStyle()
protected Number
parseNumber
(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
Methods inherited from interface com.globalmentor.beans.PropertyBindable
addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, hasPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
Constructor Details
-
AbstractNumberStringLiteralConverter
public AbstractNumberStringLiteralConverter(NumberStringLiteralConverter.Style style, 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:
NullPointerException
- if the given style isnull
.IllegalArgumentException
- if a currency is provided for a style other thanNumberStringLiteralConverter.Style.CURRENCY
.
-
-
Method Details
-
getStyle
- Returns:
- The representation style.
-
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
- Returns:
- A number format object appropriate for the session's current locale.
-
createNumberFormat
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:
-
convertValue
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 Number,
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
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.
-