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
postitionWeight
array based on its
position. The postitionWeight
values are used either
left-to-right (when useRightPos=false
) or right-to-left (when
useRightPos=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 });