com.fedmest.cw.keyboard.validators
Class IntValidator

java.lang.Object
  extended by com.fedmest.cw.keyboard.validators.ValueTypeValidator<java.lang.Integer>
      extended by com.fedmest.cw.keyboard.validators.IntValidator
All Implemented Interfaces:
KeyboardInputValidator<java.lang.Integer>

public final class IntValidator
extends ValueTypeValidator<java.lang.Integer>

This class is used internally by the KeyboardUtils.readIntFromKeyboard(), KeyboardUtils.readHexFromKeyboard(), KeyboardUtils.readOctalFromKeyboard() and KeyboardUtils.readBinaryFromKeyboard() methods. It is used as a singleton via getInstance(). There is really no need to use it other than through KeyboardUtils.

Version:
1.0
Author:
Federico Mestrone

Field Summary
 
Fields inherited from class com.fedmest.cw.keyboard.validators.ValueTypeValidator
errorMessage
 
Method Summary
 java.lang.Integer cancelValue()
          Allows you to specify the return value to offer when the user cancels input on a cancelable instance of KeyboardUtils .
 java.lang.Integer checkInput(java.lang.String txt)
          Takes the input string, checks its validity and converts it to the data type specified by the type argument.
static IntValidator getInstance()
          Returns the only available instance of this class
static void resetMax()
          This method resets the maximum integer value that will be accepted by this validator to the highest possible integer value.
static void resetMin()
          This method resets the minimum integer value that will be accepted by this validator to the lowest possible integer value.
static void resetRadix()
          This method resets the number base to 10 (decimal numbers).
static void resetRange()
          This method resets the minimum and maximum integer values that will be accepted by this validator to the lowest and highest possible integer values respectively.
static void resetRangeMessage()
          This method resets to the default text the error message that will be displayed to users when the value is out of range ("Value out of range - retry").
static void setMax(int max)
          This method sets the maximum integer value that will be accepted by this validator.
static void setMin(int min)
          This method sets the minimum integer value that will be accepted by this validator.
static void setRadix(int radix)
          This method sets the number base that will be used the next time you pass this class to KeyboardUtils for validation.
static void setRange(int min, int max)
          This method sets the maximum and minimum integer values that will be accepted by this validator.
static void setRangeMessage(java.lang.String rangeMessage)
          This method sets the error message that will be displayed to the user if validation fails due to a value being outside of the range specified with setMax(int), setMin(int) or setRange(int, int).
 
Methods inherited from class com.fedmest.cw.keyboard.validators.ValueTypeValidator
resetErrorMessage, setErrorMessage
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setRangeMessage

public static void setRangeMessage(java.lang.String rangeMessage)
This method sets the error message that will be displayed to the user if validation fails due to a value being outside of the range specified with setMax(int), setMin(int) or setRange(int, int). Invoke it before you call any method in KeyboardUtils that uses a class in this validator hierarchy to customize the error message that will be used. Note that the message applies to all subclasses of this class and will be used until reset with resetRangeMessage() or changed with this method again.

Parameters:
rangeMessage - the new error message to display to users

resetRangeMessage

public static void resetRangeMessage()
This method resets to the default text the error message that will be displayed to users when the value is out of range ("Value out of range - retry").

See Also:
setRangeMessage(String)

setMax

public static void setMax(int max)
This method sets the maximum integer value that will be accepted by this validator. The value is inclusive. The validator will fail if the input value is greater than the value of the max parameter.

Parameters:
max - the maximum integer value allowed in parsing (inclusive)

resetMax

public static void resetMax()
This method resets the maximum integer value that will be accepted by this validator to the highest possible integer value.

See Also:
setMax(int)

setMin

public static void setMin(int min)
This method sets the minimum integer value that will be accepted by this validator. The value is inclusive. The validator will fail if the input value is less than the value of the min parameter.

Parameters:
min - the minimum integer value allowed in parsing (inclusive)

resetMin

public static void resetMin()
This method resets the minimum integer value that will be accepted by this validator to the lowest possible integer value.

See Also:
setMin(int)

setRange

public static void setRange(int min,
                            int max)
This method sets the maximum and minimum integer values that will be accepted by this validator. The values are inclusive. The validator will fail if the input value is not within the range specified by the min and max parameters. Invoking this method is equivalent to invoking setMin(int) and setMax(int).

Parameters:
min - the minimum integer value allowed in parsing (inclusive)
max - the maximum integer value allowed in parsing (inclusive)
See Also:
setMin(int), setMax(int)

resetRange

public static void resetRange()
This method resets the minimum and maximum integer values that will be accepted by this validator to the lowest and highest possible integer values respectively.

See Also:
setMin(int), setMax(int), resetMin(), resetMax()

setRadix

public static void setRadix(int radix)
This method sets the number base that will be used the next time you pass this class to KeyboardUtils for validation. Note that the methods that use this validator in KeyboardUtils already deal with the radix of numbers to parse, so there is no need to explicitly call this method.

Parameters:
radix - the number base to use for integer conversion

resetRadix

public static void resetRadix()
This method resets the number base to 10 (decimal numbers). Note that the methods that use this validator in KeyboardUtils already deal with the radix of numbers to parse, so there is no need to explicitly call this method.

See Also:
setRadix(int)

getInstance

public static IntValidator getInstance()
Returns the only available instance of this class

Returns:
the instance

checkInput

public java.lang.Integer checkInput(java.lang.String txt)
                             throws KeyboardInputException
Description copied from interface: KeyboardInputValidator
Takes the input string, checks its validity and converts it to the data type specified by the type argument. If validation fails, a KeyboardInputException is raised and the user will have to type their input again. If a null reference is passed into the method, the method may throw a NullPointerException, so programmers do not need to worry about checking for null pointers.

Parameters:
txt - the input text. If the string is null, NullPointerException will be thrown.
Returns:
the result of converting the typed string into the data type expected
Throws:
KeyboardInputException - if validation against the input string failed

cancelValue

public java.lang.Integer cancelValue()
Description copied from interface: KeyboardInputValidator
Allows you to specify the return value to offer when the user cancels input on a cancelable instance of KeyboardUtils . Though null should be returned in most cases, this method is useful when - for example - you want to return a primitive type from your validator and can't use null as a return value without causing a bit of inconvenienve in your application code (explicit unboxing and checks for null values).

Returns:
the value to return when user cancels input


Copyright © 2008 Federico Mestrone. All Rights Reserved.