AbstractNumberValidator (Apache Commons Validator 1.11.0 API)
Description: declaration: package: org.apache.commons.validator.routines, class: AbstractNumberValidator

# Class AbstractNumberValidator

java.lang.Object
org.apache.commons.validator.routines.AbstractFormatValidator
org.apache.commons.validator.routines.AbstractNumberValidator

All Implemented Interfaces:
  `Serializable`

Direct Known Subclasses:
  `BigDecimalValidator, BigIntegerValidator, ByteValidator, DoubleValidator, FloatValidator, IntegerValidator, LongValidator, ShortValidator`

public abstract class AbstractNumberValidator extends AbstractFormatValidator
Abstract class for Number Validation.

This is a base class for building Number Validators using format parsing.

Since:
  1.3.0
See Also:
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `CURRENCY_FORMAT`
  Currency `NumberFormat` type.
  `static final int`
  `PERCENT_FORMAT`
  Percent `NumberFormat` type.
  `static final int`
  `STANDARD_FORMAT`
  Standard `NumberFormat` type.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions)`
  Constructs an instance with specified strict and decimal parameters.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected int`
  `determineScale(NumberFormat format)`
  Returns the multiplier of the `NumberFormat`.
  `protected Format`
  `getFormat(String pattern, Locale locale)`
  Gets a `NumberFormat` for the specified pattern and/or `Locale`.
  `protected Format`
  `getFormat(Locale locale)`
  Gets the `NumberFormat` for the specified Locale.
  `int`
  `getFormatType()`
  Gets the type of `NumberFormat` created by this validator instance.
  `boolean`
  `isAllowFractions()`
  Tests whether the number being validated is a decimal or integer.
  `boolean`
  `isInRange(Number value, Number min, Number max)`
  Tests if the value is within a specified range.
  `boolean`
  `isValid(String value, String pattern, Locale locale)`
  Tests validity using the specified `Locale`.
  `boolean`
  `maxValue(Number value, Number max)`
  Tests if the value is less than or equal to a maximum.
  `boolean`
  `minValue(Number value, Number min)`
  Tests if the value is greater than or equal to a minimum.
  `protected Object`
  `parse(String value, String pattern, Locale locale)`
  Parses the value using the specified pattern.
  `protected abstract Object`
  `processParsedValue(Object value, Format formatter)`
  Processes the parsed value, performing any further validation and type conversion required.

  ### Methods inherited from class AbstractFormatValidator

  `format, format, format, format, format, isStrict, isValid, isValid, isValid, parse`
  Modifier and Type
  Method
  Description
  `String`
  `format(Object value)`
  Formats an object into a `String` using the default Locale.
  `String`
  `format(Object value, String pattern)`
  Formats an object into a `String` using the specified pattern.
  `String`
  `format(Object value, String pattern, Locale locale)`
  Formats an object using the specified pattern and/or `Locale`.
  `protected String`
  `format(Object value, Format formatter)`
  Formats a value with the specified `Format`.
  `String`
  `format(Object value, Locale locale)`
  Formats an object into a `String` using the specified Locale.
  `boolean`
  `isStrict()`
  Tests whether validated values should adhere strictly to the `Format` used.
  `boolean`
  `isValid(String value)`
  Validates using the default `Locale`.
  `boolean`
  `isValid(String value, String pattern)`
  Validates using the specified pattern.
  `boolean`
  `isValid(String value, Locale locale)`
  Validates using the specified `Locale`.
  `protected Object`
  `parse(String value, Format formatter)`
  Parses the value with the specified `Format`.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### STANDARD_FORMAT

    public static final int STANDARD_FORMAT
    Standard `NumberFormat` type.
    See Also:
      - Constant Field Values

  - ### CURRENCY_FORMAT

    public static final int CURRENCY_FORMAT
    Currency `NumberFormat` type.
    See Also:
      - Constant Field Values

  - ### PERCENT_FORMAT

    public static final int PERCENT_FORMAT
    Percent `NumberFormat` type.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### AbstractNumberValidator

    public AbstractNumberValidator(boolean strict, int formatType, boolean allowFractions)
    Constructs an instance with specified strict and decimal parameters.
    Parameters:
      `strict` - `true` if strict `Format` parsing should be used.
      `formatType` - The `NumberFormat` type to create for validation, default is STANDARD_FORMAT.
      `allowFractions` - `true` if fractions are allowed or `false` if integers only.

- ## Method Details

  - ### determineScale

    protected int determineScale(NumberFormat format)
    Returns the multiplier of the `NumberFormat`.
    Parameters:
      `format` - The `NumberFormat` to determine the multiplier of.
    Returns:
      The multiplying factor for the format.

  - ### getFormat

    protected Format getFormat(Locale locale)
    Gets the `NumberFormat` for the specified Locale.
    Parameters:
      `locale` - The locale a `NumberFormat` is required for, system default if null.
    Returns:
      The `NumberFormat` to created.

  - ### getFormat

    protected Format getFormat(String pattern, Locale locale)
    Gets a `NumberFormat` for the specified pattern and/or `Locale`.
    Specified by:
      `getFormat` in class `AbstractFormatValidator`
    Parameters:
      `pattern` - The pattern used to validate the value against or `null` to use the default for the `Locale`.
      `locale` - The locale to use for the currency format, system default if null.
    Returns:
      The `NumberFormat` to created.

  - ### getFormatType

    public int getFormatType()
    Gets the type of `NumberFormat` created by this validator instance.
    Returns:
      The format type created.

  - ### isAllowFractions

    public boolean isAllowFractions()
    Tests whether the number being validated is a decimal or integer.
    Returns:
      `true` if decimals are allowed or `false` if the number is an integer.

  - ### isInRange

    public boolean isInRange(Number value, Number min, Number max)
    Tests if the value is within a specified range.
    Parameters:
      `value` - The value validation is being performed on.
      `min` - The minimum value of the range.
      `max` - The maximum value of the range.
    Returns:
      `true` if the value is within the specified range.

  - ### isValid

    public boolean isValid(String value, String pattern, Locale locale)
    Tests validity using the specified `Locale`.
    Specified by:
      `isValid` in class `AbstractFormatValidator`
    Parameters:
      `value` - The value validation is being performed on.
      `pattern` - The pattern used to validate the value against, or the default for the `Locale` if `null`.
      `locale` - The locale to use for the date format, system default if null.
    Returns:
      `true` if the value is valid.

  - ### maxValue

    public boolean maxValue(Number value, Number max)
    Tests if the value is less than or equal to a maximum.

    For an integer validator the bound is compared exactly rather than narrowed to a `long`, so a `BigInteger` or `BigDecimal` bound outside the long range keeps its magnitude and a fractional bound keeps its fractional part. A non-finite `Double` or `Float` operand keeps the `doubleValue()` comparison, since `BigDecimal` cannot represent `NaN` or an infinity.

    Parameters:
      `value` - The value validation is being performed on.
      `max` - The maximum value.
    Returns:
      `true` if the value is less than or equal to the maximum.

  - ### minValue

    public boolean minValue(Number value, Number min)
    Tests if the value is greater than or equal to a minimum.

    For an integer validator the bound is compared exactly rather than narrowed to a `long`, so a `BigInteger` or `BigDecimal` bound outside the long range keeps its magnitude and a fractional bound keeps its fractional part. A non-finite `Double` or `Float` operand keeps the `doubleValue()` comparison, since `BigDecimal` cannot represent `NaN` or an infinity.

    Parameters:
      `value` - The value validation is being performed on.
      `min` - The minimum value.
    Returns:
      `true` if the value is greater than or equal to the minimum.

  - ### parse

    protected Object parse(String value, String pattern, Locale locale)
    Parses the value using the specified pattern.
    Parameters:
      `value` - The value validation is being performed on.
      `pattern` - The pattern used to validate the value against, or the default for the `Locale` if `null`.
      `locale` - The locale to use for the date format, system default if null.
    Returns:
      The parsed value if valid or `null` if invalid.

  - ### processParsedValue

    protected abstract Object processParsedValue(Object value, Format formatter)
    Processes the parsed value, performing any further validation and type conversion required.
    Specified by:
      `processParsedValue` in class `AbstractFormatValidator`
    Parameters:
      `value` - The parsed object created.
      `formatter` - The Format used to parse the value with.
    Returns:
      The parsed value converted to the appropriate type if valid or `null` if invalid.