Class ModulusTenCheckDigit
- All Implemented Interfaces:
Serializable,CheckDigit
How it 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
positionWeightarray 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
CodeValidator in 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:
- 1.6
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionModulusTenCheckDigit(int[] positionWeight) Constructs a modulus 10 Check Digit routine with the specified weighting from left to right.ModulusTenCheckDigit(int[] positionWeight, boolean useRightPos) Constructs a modulus 10 Check Digit routine with the specified weighting, indicating whether its from the left or right.ModulusTenCheckDigit(int[] positionWeight, boolean useRightPos, boolean sumWeightedDigits) Constructs 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
Modifier and TypeMethodDescriptionbooleanValidate 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.toString()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 Details
-
ModulusTenCheckDigit
Constructs a modulus 10 Check Digit routine with the specified weighting from left to right.- Parameters:
positionWeight- the weighted values to apply based on the character position
-
ModulusTenCheckDigit
Constructs a modulus 10 Check Digit routine with the specified weighting, indicating whether its from the left or right.- Parameters:
positionWeight- the weighted values to apply based on the character positionuseRightPos-trueif use positionWeights from right to left
-
ModulusTenCheckDigit
Constructs 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:
positionWeight- 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 Details
-
isValid
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
Convert 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
-
toString
Return a string representation of this implementation. -
weightedValue
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.
-