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
  • Constructor Details

  • Method Details

    • getStyle

      Returns:
      The representation style.
    • getCurrency

      public 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 NumberFormat getNumberFormat()
      Returns:
      A number format object appropriate for the session's current locale.
    • createNumberFormat

      protected NumberFormat createNumberFormat(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:
    • convertValue

      public 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 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 is null.
      Throws:
      ConversionException - if the value cannot be converted.
    • parseNumber

      protected Number parseNumber(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.