Class ModulusTenCheckDigit
- java.lang.Object
-
- org.apache.commons.validator.routines.checkdigit.ModulusCheckDigit
-
- org.apache.commons.validator.routines.checkdigit.ModulusTenCheckDigit
-
- All Implemented Interfaces:
java.io.Serializable,CheckDigit
public final class ModulusTenCheckDigit extends ModulusCheckDigit
General Modulus 10 Check Digit calculation/validation.How if Works
This implementation calculates/validates the check digit in the following way:
- Converting each character to an integer value using
Character.getNumericValue(char)- negative integer values from that method are invalid. - Calculating a weighted value by multiplying the character's
integer value by a weighting factor. The weighting factor is
selected from the configured
postitionWeightarray based on its position. ThepostitionWeightvalues are used either left-to-right (whenuseRightPos=false) or right-to-left (whenuseRightPos=true). - If
sumWeightedDigits=true, the weighted value is re-calculated by summing its digits. - The weighted values of each character are totalled.
- The total modulo 10 will be zero for a code with a valid Check Digit.
Limitations
This implementation has the following limitations:
- It assumes the last character in the code is the Check Digit and validates that it is a numeric character.
- The only limitation on valid characters are those that
Character.getNumericValue(char)returns a positive value. If, for example, the code should only contain numbers, this implementation does not check that. - There are no checks on code length.
Note: This implementation can be combined with the
CodeValidatorin order to ensure the length and characters are valid.Example Usage
This implementation was added after a number of Modulus 10 routines and these are shown re-implemented using this routine below:
ABA Number Check Digit Routine (equivalent of
ABANumberCheckDigit). Weighting factors are[1, 7, 3]applied from right to left.CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 7, 3 }, true);CUSIP Check Digit Routine (equivalent of
CUSIPCheckDigit). Weighting factors are[1, 2]applied from right to left and the digits of the weighted value are summed.CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true);EAN-13 / UPC Check Digit Routine (equivalent of
EAN13CheckDigit). Weighting factors are[1, 3]applied from right to left.CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3 }, true);Luhn Check Digit Routine (equivalent of
LuhnCheckDigit). Weighting factors are[1, 2]applied from right to left and the digits of the weighted value are summed.CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true);SEDOL Check Digit Routine (equivalent of
SedolCheckDigit). Weighting factors are[1, 3, 1, 7, 3, 9, 1]applied from left to right.CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3, 1, 7, 3, 9, 1 });- Since:
- Validator 1.6
- Version:
- $Revision: 1739356 $
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description ModulusTenCheckDigit(int[] postitionWeight)Construct a modulus 10 Check Digit routine with the specified weighting from left to right.ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos)Construct a modulus 10 Check Digit routine with the specified weighting, indicating whether its from the left or right.ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos, boolean sumWeightedDigits)Construct a modulus 10 Check Digit routine with the specified weighting, indicating whether its from the left or right and whether the weighted digits should be summed.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanisValid(java.lang.String code)Validate a modulus check digit for a code.protected inttoInt(char character, int leftPos, int rightPos)Convert a character at a specified position to an integer value.java.lang.StringtoString()Return a string representation of this implementation.protected intweightedValue(int charValue, int leftPos, int rightPos)Calculates the weighted value of a character in the code at a specified position.-
Methods inherited from class org.apache.commons.validator.routines.checkdigit.ModulusCheckDigit
calculate, calculateModulus, getModulus, sumDigits, toCheckDigit
-
-
-
-
Constructor Detail
-
ModulusTenCheckDigit
public ModulusTenCheckDigit(int[] postitionWeight)
Construct a modulus 10 Check Digit routine with the specified weighting from left to right.- Parameters:
postitionWeight- the weighted values to apply based on the character position
-
ModulusTenCheckDigit
public ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos)Construct a modulus 10 Check Digit routine with the specified weighting, indicating whether its from the left or right.- Parameters:
postitionWeight- the weighted values to apply based on the character positionuseRightPos-trueif use positionWeights from right to left
-
ModulusTenCheckDigit
public ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos, boolean sumWeightedDigits)Construct a modulus 10 Check Digit routine with the specified weighting, indicating whether its from the left or right and whether the weighted digits should be summed.- Parameters:
postitionWeight- the weighted values to apply based on the character positionuseRightPos-trueif use positionWeights from right to leftsumWeightedDigits-trueif sum the digits of the weighted value
-
-
Method Detail
-
isValid
public boolean isValid(java.lang.String code)
Validate a modulus check digit for a code.Note: assumes last digit is the check digit
- Specified by:
isValidin interfaceCheckDigit- Overrides:
isValidin classModulusCheckDigit- Parameters:
code- The code to validate- Returns:
trueif the check digit is valid, otherwisefalse
-
toInt
protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitExceptionConvert a character at a specified position to an integer value.Note: this implementation only handlers values that Character.getNumericValue(char) returns a non-negative number.
- Overrides:
toIntin classModulusCheckDigit- Parameters:
character- The character to convertleftPos- The position of the character in the code, counting from left to right (for identifying the position in the string)rightPos- The position of the character in the code, counting from right to left (not used here)- Returns:
- The integer value of the character
- Throws:
CheckDigitException- if Character.getNumericValue(char) returns a negative number
-
weightedValue
protected int weightedValue(int charValue, int leftPos, int rightPos)Calculates the weighted value of a character in the code at a specified position.- Specified by:
weightedValuein classModulusCheckDigit- Parameters:
charValue- The numeric value of the character.leftPos- The position of the character in the code, counting from left to rightrightPos- The position of the character in the code, counting from right to left- Returns:
- The weighted value of the character.
-
toString
public java.lang.String toString()
Return a string representation of this implementation.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string representation
-
-