# Class CASNumberCheckDigit

java.lang.Object
org.apache.commons.validator.routines.checkdigit.ModulusCheckDigit
org.apache.commons.validator.routines.checkdigit.CASNumberCheckDigit

All Implemented Interfaces:
  `Serializable, CheckDigit`

public final class CASNumberCheckDigit extends ModulusCheckDigit
Modulus 10 CAS Registry Number (or Chemical Abstracts Service (CAS RN)) Check Digit calculation/validation.

CAS Numbers are unique identification numbers used to identify chemical substance described in the open scientific literature.

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.

For further information see Wikipedia - CAS Registry Number.

Since:
  1.9.0
See Also:
  - Serialized Form

- ## Method Summary

  Modifier and Type
  Method
  Description
  `String`
  `calculate(String code)`
  Calculates a modulus Check Digit for a code which does not yet have one.
  `static CheckDigit`
  `getInstance()`
  Gets the singleton instance of this validator.
  `boolean`
  `isValid(String code)`
  Validates a modulus check digit for a code.
  `protected int`
  `weightedValue(int charValue, int leftPos, int rightPos)`
  Calculates the weighted value of a character in the code at a specified position.

  ### Methods inherited from class ModulusCheckDigit

  `calculateModulus, getModulus, sumDigits, toCheckDigit, toInt`
  Modifier and Type
  Method
  Description
  `protected int`
  `calculateModulus(String code, boolean includesCheckDigit)`
  Calculates the modulus for a code.
  `int`
  `getModulus()`
  Gets the modulus value this check digit routine is based on.
  `static int`
  `sumDigits(int number)`
  Adds together the individual digits in a number.
  `protected String`
  `toCheckDigit(int charValue)`
  Converts an integer value to a check digit.
  `protected int`
  `toInt(char character, int leftPos, int rightPos)`
  Converts a character at a specified position to an integer value.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getInstance

    public static CheckDigit getInstance()
    Gets the singleton instance of this validator.
    Returns:
      A singleton instance of the CAS Number validator.

  - ### calculate

    public String calculate(String code) throws CheckDigitException
    Calculates a modulus Check Digit for a code which does not yet have one.
    Specified by:
      `calculate` in interface `CheckDigit`
    Overrides:
      `calculate` in class `ModulusCheckDigit`
    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

    public boolean isValid(String code)
    Validates a modulus check digit for a code.
    Specified by:
      `isValid` in interface `CheckDigit`
    Overrides:
      `isValid` in class `ModulusCheckDigit`
    Parameters:
      `code` - The code to validate.
    Returns:
      `true` if the check digit is valid, otherwise `false`.

  - ### 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:
      `weightedValue` in class `ModulusCheckDigit`
    Parameters:
      `charValue` - The numeric value of the character.
      `leftPos` - The position of the character in the code, counting from left to right.
      `rightPos` - The position of the character in the code, counting from right to left.
    Returns:
      The weighted value of the character.