Class AbstractNumberStringLiteralConverter<V extends java.lang.Number>

  • 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 override createNumberFormat(Locale) if custom number formats are desired. This implementation synchronizes all conversions on the NumberFormat object.
    Author:
    Garret Wilson
    • 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 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 com.globalmentor.beans.PropertyBindable

        addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, hasPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
    • 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, or null if currency representation is not requested or the currency should be dynamically determined by the locale.
        Throws:
        java.lang.NullPointerException - if the given style is null.
        java.lang.IllegalArgumentException - if a currency is provided for a style other than NumberStringLiteralConverter.Style.CURRENCY.
    • Method Detail

      • 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 interface Converter<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 is null.
        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 a null 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 or Double form, or null if the given literal is null or the empty string.
        Throws:
        ConversionException - if the literal value cannot be converted.