Class CASNumberCheckDigit
- All Implemented Interfaces:
Serializable,CheckDigit
CAS Registry Numbers are a numeric codes.
Check digit calculation is based on modulus 10 with digits being weighted
based on their position (from right to left).
The check digit is found by taking the last digit times 1, the preceding digit times 2,
the preceding digit times 3 etc., adding all these up and computing the sum modulo 10.
For example, the CAS number of water is 7732-18-5:
the checksum 5 is calculated as (8×1 + 1×2 + 2×3 + 3×4 + 7×5 + 7×6) = 105; 105 mod 10 = 5.
Note that these do not validate the input for syntax.
Such validation is performed by the CASNumberValidator
- Since:
- 1.9.0
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionCalculate a modulus Check Digit for a code which does not yet have one.static CheckDigitGets the singleton instance of this validator.booleanValidate a modulus check digit for a code.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
calculateModulus, getCheckdigitLength, getModulus, sumDigits, toCheckDigit, toInt
-
Field Details
-
MIN_LEN
public static final int MIN_LENThe minimum length without dashes.CAS number consists of 3 groups of numbers separated dashes (-). First group has 2 to 7 digits, the second 2 digits, the third is the check digit. Example: water is 7732-18-5
- See Also:
-
MAX_LEN
public static final int MAX_LENThe maximum length without dashes.9999999-99-5- See Also:
-
-
Method Details
-
getInstance
Gets the singleton instance of this validator.- Returns:
- A singleton instance of the CAS Number validator.
-
weightedValue
protected int weightedValue(int charValue, int leftPos, int rightPos) Calculates the weighted value of a character in the code at a specified position.CAS numbers are weighted in the following manner:
right position: 1 2 3 4 5 6 7 8 9 10 weight: 1 2 3 4 5 6 7 8 9 0- 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 positionof the character in the code, counting from right to left- Returns:
- The weighted value of the character.
-
calculate
Calculate a modulus Check Digit for a code which does not yet have one.- Specified by:
calculatein interfaceCheckDigit- Overrides:
calculatein classModulusCheckDigit- Parameters:
code- The code for which to calculate the Check Digit; the check digit should not be included- Returns:
- The calculated Check Digit
- Throws:
CheckDigitException- if an error occurs calculating the check digit
-
isValid
Validate a modulus check digit for a code.- Specified by:
isValidin interfaceCheckDigit- Overrides:
isValidin classModulusCheckDigit- Parameters:
code- The code to validate- Returns:
trueif the check digit is valid, otherwisefalse
-