public final class ModulusTenCheckDigit extends ModulusCheckDigit
This implementation calculates/validates the check digit in the following way:
Character.getNumericValue(char)
- negative integer values from
that method are invalid.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
).sumWeightedDigits=true
, the weighted value is
re-calculated by summing its digits.This implementation has the following limitations:
Character.getNumericValue(char)
returns a positive value. If,
for example, the code should only contain numbers, this implementation does
not check that.
Note: This implementation can be combined with the
CodeValidator
in order to ensure the length and characters are valid.
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 });
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
isValid(String code)
Validate a modulus check digit for a code.
|
protected int |
toInt(char character,
int leftPos,
int rightPos)
Convert a character at a specified position to an integer value.
|
String |
toString()
Return a string representation of this implementation.
|
protected int |
weightedValue(int charValue,
int leftPos,
int rightPos)
Calculates the weighted value of a character in the code at a
specified position.
|
calculate, calculateModulus, getModulus, sumDigits, toCheckDigit
public ModulusTenCheckDigit(int[] postitionWeight)
postitionWeight
- the weighted values to apply based on the
character positionpublic ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos)
postitionWeight
- the weighted values to apply based on the
character positionuseRightPos
- true
if use positionWeights from right to
leftpublic ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos, boolean sumWeightedDigits)
postitionWeight
- the weighted values to apply based on the
character positionuseRightPos
- true
if use positionWeights from right to
leftsumWeightedDigits
- true
if sum the digits of the
weighted valuepublic boolean isValid(String code)
Note: assumes last digit is the check digit
isValid
in interface CheckDigit
isValid
in class ModulusCheckDigit
code
- The code to validatetrue
if the check digit is valid, otherwise
false
protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException
Note: this implementation only handlers values that Character.getNumericValue(char) returns a non-negative number.
toInt
in class ModulusCheckDigit
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)CheckDigitException
- if Character.getNumericValue(char) returns a
negative numberprotected int weightedValue(int charValue, int leftPos, int rightPos)
weightedValue
in class ModulusCheckDigit
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 leftCopyright © 2002–2020 The Apache Software Foundation. All rights reserved.