Class NumberConverter

java.lang.Object
org.jhotdraw8.base.converter.NumberConverter
All Implemented Interfaces:
Converter<Number>

public class NumberConverter extends Object implements Converter<Number>
Formats real numbers.

Supports clamping into a [min,max] range (inclusive), a scale factor and a unit label.

Also allows to specify the minimum and maximum of integer digits, fraction digits, as well as the minimum of negative and positive exponent.

Author:
Werner Randelshofer
  • Constructor Details

    • NumberConverter

      public NumberConverter()
      Creates a NumberFormatter with the a default NumberFormat instance obtained from NumberFormat.getNumberInstance().
    • NumberConverter

      public NumberConverter(Class<? extends Number> valueClass)
    • NumberConverter

      public NumberConverter(double min, double max, double multiplier)
      Creates a NumberFormatter with the specified Format instance.
      Parameters:
      min - the min
      max - the max
      multiplier - the multiplier
    • NumberConverter

      public NumberConverter(double min, double max, double multiplier, boolean allowsNullValue)
      Creates a NumberFormatter with the specified Format instance.
      Parameters:
      min - the min
      max - the max
      multiplier - the multiplier
      allowsNullValue - whether null values are allowed
    • NumberConverter

      public NumberConverter(Class<? extends Number> valueClass, Number min, Number max, double multiplier, boolean allowsNullValue, String unit)
      Creates a NumberFormatter with the specified Format instance.
      Parameters:
      min - the min
      max - the max
      multiplier - the multiplier
      allowsNullValue - whether null values are allowed
      unit - the unit string
    • NumberConverter

      public NumberConverter(@NonNull Class<? extends Number> valueClass, @NonNull Number min, @NonNull Number max, double multiplier, boolean allowsNullValue, @Nullable String unit, @NonNull DecimalFormat decimalFormat, @NonNull DecimalFormat scientificFormat)
    • NumberConverter

      public NumberConverter(double min, double max, double multiplier, boolean allowsNullValue, String unit)
  • Method Details

    • getMinimum

      public @NonNull Number getMinimum()
      Returns the minimum permissible value.
      Returns:
      Minimum legal value that can be input
    • getMaximum

      public @NonNull Number getMaximum()
      Returns the maximum permissible value.
      Returns:
      Maximum legal value that can be input
    • getFactor

      public double getFactor()
      Gets the factor for use in percent, per mille, and similar formats.
      Returns:
      the factor
    • getAllowsNullValue

      public boolean getAllowsNullValue()
      Returns true if null values are allowed.
      Returns:
      true if null values are allowed
    • toString

      public void toString(@NonNull Appendable buf, @Nullable IdSupplier idSupplier, @Nullable Number value) throws IOException
      Description copied from interface: Converter
      Converts a value to a string and appends it to the provided Appendable.

      This method does not change the state of the converter.

      Specified by:
      toString in interface Converter<Number>
      Parameters:
      buf - The appendable
      idSupplier - The factory for creating object ids. Nullable for non-resolving converters.
      value - The value. Nullable.
      Throws:
      IOException - thrown by Appendable
    • fromString

      public @Nullable Number fromString(@NonNull CharBuffer str, @Nullable IdResolver idResolver) throws ParseException
      Description copied from interface: Converter
      Constructs a value from a string.

      The converter should try to create the value greedily, by consuming as many characters as possible for the value.

      This method does not change the state of the converter.

      Specified by:
      fromString in interface Converter<Number>
      Parameters:
      str - A char buffer which holds the string. The char buffer must be treated as read only! The position of the char buffer denotes the beginning of the string when this method is invoked. After completion of this method, the position is set after the last consumed character.
      idResolver - The factory for looking up object ids. Nullable for non-resolving converters.
      Returns:
      The value. Nullable.
      Throws:
      ParseException - if conversion failed. The error offset field is set to the position where parsing failed. The position of the buffer is undefined.
    • getMinimumNegativeExponent

      public int getMinimumNegativeExponent()
      Gets the minimum negative exponent value for scientific notation.
      Returns:
      the minimum negative exponent
    • getMinimumPositiveExponent

      public int getMinimumPositiveExponent()
      Gets the minimum positive exponent value for scientific notation.
      Returns:
      the minimum positive exponent
    • isUsesScientificNotation

      public boolean isUsesScientificNotation()
      Returns true if scientific notation is used.
      Returns:
      true if scientific notation is used
    • getValueClass

      public @NonNull Class<? extends Number> getValueClass()
      Gets the value class.
      Returns:
      the value class
    • getDefaultValue

      public @Nullable Number getDefaultValue()
      Description copied from interface: Converter
      Provides a default value for APIs which always require a value even if conversion from String failed.
      Specified by:
      getDefaultValue in interface Converter<Number>
      Returns:
      The default value to use when conversion from String failed.