Class NumberTransformerBase<T extends Number>

  • Type Parameters:
    T - the type handled by this transformer
    All Implemented Interfaces:
    Transformer, Validator
    Direct Known Subclasses:
    AbstractDecimalTransformer, AbstractIntegerTransformer

    public abstract class NumberTransformerBase<T extends Number>
    extends Object
    implements Transformer, Validator

    An abstract base class for transformers and validators for numbers.

    This base class already provides the major part of functionality for validating numeric input and transforming strings to number objects. Concrete sub classes are responsible for the creation of a java.text.NumberFormat object that is used for parsing the user input. The class also supports certain semantic checks, especially whether an entered number lies in a specified interval.

    This class makes use of Java generics to be independent on a concrete number type. The returned (transformed) object will be of this type, and also the specified minimum or maximum values must use this type.

    The following properties are supported by this class:

    Property Description Default
    minimum Here the minimum value can be defined. Entered numbers are checked to be greater or equal than this number. If this property is undefined, no minimum checks will be performed. undefined
    maximum Here the maximum value can be defined. Entered numbers are checked to be less or equal than this number. If this property is undefined, no maximum checks will be performed. undefined

    Validation of user input can fail for multiple reasons. The following table lists the possible error messages:

    Message key Description Parameters
    "ERR_INVALID_NUMBER" The passed in string cannot be parsed to a number object. {0} = the input string
    "ERR_NUMBER_TOO_SMALL" The entered number is too small. This error message is returned if the number is less than the specified minimum number and no maximum number was specified. (If both a minimum and a maximum number are specified, the error code "ERR_NUMBER_INTERVAL" is used.) {0} = the minimum number
    "ERR_NUMBER_TOO_BIG" The entered number is too big. This error message is returned if the number is greater than the specified maximum number and no minimum number was specified. (If both a minimum and a maximum number are specified, the error code "ERR_NUMBER_INTERVAL" is used.) {0} = the maximum number
    "ERR_NUMBER_INTERVAL" The entered number is not in the interval spanned by the minimum and the maximum value. If both a minimum and a maximum are specified and the entered number does not meet these constraints, this error message is produced rather than one of "ERR_NUMBER_TOO_SMALL" or "ERR_NUMBER_TOO_BIG". {0} = the minimum value, {1} = the maximum value

    The class implements both the Transformer and Validator interfaces. It is safe to use an instance concurrently as transformer and validator for the same or multiple input fields.

    Version:
    $Id: NumberTransformerBase.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • NumberTransformerBase

        public NumberTransformerBase()
    • Method Detail

      • getMinimum

        public T getMinimum()
        Returns the minimum value.
        Returns:
        the minimum value (can be null)
      • setMinimum

        public void setMinimum​(T minimum)
        Sets the minimum value. If a minimum value is specified, the validator implementation will check whether an entered number is not less than this minimum value.
        Parameters:
        minimum - the minimum value
      • getMaximum

        public T getMaximum()
        Returns the maximum value.
        Returns:
        the maximum value (can be null)
      • setMaximum

        public void setMaximum​(T maximum)
        Sets the maximum value. If a maximum value is specified, the validator implementation will check whether an entered number is not greater than this maximum value.
        Parameters:
        maximum - the maximum value
      • transform

        public Object transform​(Object o,
                                TransformerContext ctx)
                         throws Exception
        Transforms the specified object to the target format. This implementation tries to convert the passed in object to a Number of the type specified by the generics parameter for this class. This is done by using a java.text.NumberFormat object. If the passed in object is null, null will also be returned.
        Specified by:
        transform in interface Transformer
        Parameters:
        o - the object to be transformed
        ctx - the transformer context
        Returns:
        the transformed object
        Throws:
        Exception - if conversion fails
      • isValid

        public ValidationResult isValid​(Object o,
                                        TransformerContext ctx)
        Validates the specified object. This implementation checks whether the object can be transformed to a number. If this is the case, isNumberValid() will be called to check whether the number lies in a valid range. Depending on these checks a validation result object is returned. A null object or an empty string are considered valid.
        Specified by:
        isValid in interface Validator
        Parameters:
        o - the object to be validated
        ctx - the transformer context
        Returns:
        an object with the results of the validation
      • transformToNumber

        protected T transformToNumber​(Object o,
                                      TransformerContext ctx,
                                      NumberFormat fmt)
                               throws ParseException
        Transforms the given object into a number. This method is called by both transform() and isValid(). It performs the actual transformation. The passed in object may be null or empty, in which case null is returned.
        Parameters:
        o - the object to be transformed
        ctx - the transformer context
        fmt - the format object to be used
        Returns:
        the converted number
        Throws:
        ParseException - if transformation fails
      • isNumberValid

        protected ValidationResult isNumberValid​(T n,
                                                 NumberFormat fmt,
                                                 TransformerContext ctx,
                                                 T min,
                                                 T max)
        Validates an entered number. This method is called by isValid() if the passed in object can be successfully converted into a number. It checks this number against the minimum and maximum values (if defined).
        Parameters:
        n - the number to check
        fmt - the format object (used for formatting the minimum and/or maximum values in error messages)
        ctx - the transformation context
        min - the minimum value (can be null)
        max - the maximum value (can be null)
        Returns:
        an object with the results of the validation
      • errorResult

        protected ValidationResult errorResult​(String errorKey,
                                               TransformerContext ctx,
                                               Object... params)
        Creates a validation result if an error occurred.
        Parameters:
        errorKey - the key of the error message
        ctx - the transformer context
        params - optional parameters for the error message
        Returns:
        the validation result with this error
      • convert

        protected abstract T convert​(Number n)
        Converts the specified number to the target type supported by this transformer. This method is called after the object to be transformed was parsed by a NumberFormat object. There is no guarantee that the result of this parsing process matches the desired type. If this is not the case, this method will be called.
        Parameters:
        n - the number to be converted
        Returns:
        the converted number
        Throws:
        IllegalArgumentException - if the number cannot be converted to the target type (e.g. because it does not fit into the supported range)
      • createFormat

        protected abstract NumberFormat createFormat​(Locale locale)
        Creates the format object for parsing a number. This method is called whenever a string (entered by the user) has to be converted into a number.
        Parameters:
        locale - the locale to use
        Returns:
        the format object for parsing the number
      • fetchProperty

        protected abstract T fetchProperty​(org.apache.commons.configuration.Configuration config,
                                           String property,
                                           T defaultValue)
        Fetches a property of the supported type from the specified configuration object. This method is called for determining the current values of type-related properties (e.g. the minimum and maximum values). An implementation has to invoke the appropriate methods on the passed in Configuration object.
        Parameters:
        config - the configuration object
        property - the name of the property to be obtained
        defaultValue - the default value for this property
        Returns:
        the value of this property