java.lang.Object
org.jhotdraw8.base.converter.NumberConverter
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 Summary
ConstructorsConstructorDescriptionCreates aNumberFormatterwith the a defaultNumberFormatinstance obtained fromNumberFormat.getNumberInstance().NumberConverter(double min, double max, double multiplier) Creates a NumberFormatter with the specified Format instance.NumberConverter(double min, double max, double multiplier, boolean allowsNullValue) Creates a NumberFormatter with the specified Format instance.NumberConverter(double min, double max, double multiplier, boolean allowsNullValue, String unit) 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(Class<? extends Number> valueClass) NumberConverter(Class<? extends Number> valueClass, Number min, Number max, double multiplier, boolean allowsNullValue, String unit) Creates a NumberFormatter with the specified Format instance. -
Method Summary
Modifier and TypeMethodDescriptionfromString(@NonNull CharBuffer str, @Nullable IdResolver idResolver) Constructs a value from a string.booleanReturns true if null values are allowed.Provides a default value for APIs which always require a value even if conversion from String failed.doubleGets the factor for use in percent, per mille, and similar formats.Returns the maximum permissible value.Returns the minimum permissible value.intGets the minimum negative exponent value for scientific notation.intGets the minimum positive exponent value for scientific notation.Gets the value class.booleanReturns true if scientific notation is used.voidtoString(@NonNull Appendable buf, @Nullable IdSupplier idSupplier, @Nullable Number value) Converts a value to a string and appends it to the providedAppendable.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.jhotdraw8.base.converter.Converter
fromString, fromString, fromString, getHelpText, needsIdResolver, toString, toString, toString
-
Constructor Details
-
NumberConverter
public NumberConverter()Creates aNumberFormatterwith the a defaultNumberFormatinstance obtained fromNumberFormat.getNumberInstance(). -
NumberConverter
-
NumberConverter
public NumberConverter(double min, double max, double multiplier) Creates a NumberFormatter with the specified Format instance.- Parameters:
min- the minmax- the maxmultiplier- the multiplier
-
NumberConverter
public NumberConverter(double min, double max, double multiplier, boolean allowsNullValue) Creates a NumberFormatter with the specified Format instance.- Parameters:
min- the minmax- the maxmultiplier- the multiplierallowsNullValue- 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 minmax- the maxmultiplier- the multiplierallowsNullValue- whether null values are allowedunit- the unit string
-
NumberConverter
-
NumberConverter
public NumberConverter(double min, double max, double multiplier, boolean allowsNullValue, String unit)
-
-
Method Details
-
getMinimum
Returns the minimum permissible value.- Returns:
- Minimum legal value that can be input
-
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:ConverterConverts a value to a string and appends it to the providedAppendable.This method does not change the state of the converter.
- Specified by:
toStringin interfaceConverter<Number>- Parameters:
buf- The appendableidSupplier- 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:ConverterConstructs 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:
fromStringin interfaceConverter<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
Gets the value class.- Returns:
- the value class
-
getDefaultValue
Description copied from interface:ConverterProvides a default value for APIs which always require a value even if conversion from String failed.- Specified by:
getDefaultValuein interfaceConverter<Number>- Returns:
- The default value to use when conversion from String failed.
-