Class ModulusCheckDigit

    • Constructor Summary

      Constructors 
      Constructor Description
      ModulusCheckDigit​(int modulus)
      Construct a CheckDigit routine for a specified modulus.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String calculate​(java.lang.String code)
      Calculate a modulus Check Digit for a code which does not yet have one.
      protected int calculateModulus​(java.lang.String code, boolean includesCheckDigit)
      Calculate the modulus for a code.
      int getModulus()
      Return the modulus value this check digit routine is based on.
      boolean isValid​(java.lang.String code)
      Validate a modulus check digit for a code.
      static int sumDigits​(int number)
      Add together the individual digits in a number.
      protected java.lang.String toCheckDigit​(int charValue)
      Convert an integer value to a check digit.
      protected int toInt​(char character, int leftPos, int rightPos)
      Convert a character at a specified position to an integer value.
      protected abstract 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 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ModulusCheckDigit

        public ModulusCheckDigit​(int modulus)
        Construct a CheckDigit routine for a specified modulus.
        Parameters:
        modulus - The modulus value to use for the check digit calculation
    • Method Detail

      • getModulus

        public int getModulus()
        Return the modulus value this check digit routine is based on.
        Returns:
        The modulus value this check digit routine is based on
      • isValid

        public boolean isValid​(java.lang.String code)
        Validate a modulus check digit for a code.
        Specified by:
        isValid in interface CheckDigit
        Parameters:
        code - The code to validate
        Returns:
        true if the check digit is valid, otherwise false
      • calculate

        public java.lang.String calculate​(java.lang.String code)
                                   throws CheckDigitException
        Calculate a modulus Check Digit for a code which does not yet have one.
        Specified by:
        calculate in interface CheckDigit
        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
      • calculateModulus

        protected int calculateModulus​(java.lang.String code,
                                       boolean includesCheckDigit)
                                throws CheckDigitException
        Calculate the modulus for a code.
        Parameters:
        code - The code to calculate the modulus for.
        includesCheckDigit - Whether the code includes the Check Digit or not.
        Returns:
        The modulus value
        Throws:
        CheckDigitException - if an error occurs calculating the modulus for the specified code
      • weightedValue

        protected abstract int weightedValue​(int charValue,
                                             int leftPos,
                                             int rightPos)
                                      throws CheckDigitException
        Calculates the weighted value of a character in the code at a specified position.

        Some modulus routines weight the value of a character depending on its position in the code (e.g. ISBN-10), while others use different weighting factors for odd/even positions (e.g. EAN or Luhn). Implement the appropriate mechanism required by overriding this method.

        Parameters:
        charValue - The numeric value of the character
        leftPos - The position of the character in the code, counting from left to right
        rightPos - The positionof the character in the code, counting from right to left
        Returns:
        The weighted value of the character
        Throws:
        CheckDigitException - if an error occurs calculating the weighted value
      • toInt

        protected int toInt​(char character,
                            int leftPos,
                            int rightPos)
                     throws CheckDigitException
        Convert a character at a specified position to an integer value.

        Note: this implementation only handlers numeric values For non-numeric characters, override this method to provide character-->integer conversion.

        Parameters:
        character - The character to convert
        leftPos - The position of the character in the code, counting from left to right (for identifiying 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 is non-numeric
      • toCheckDigit

        protected java.lang.String toCheckDigit​(int charValue)
                                         throws CheckDigitException
        Convert an integer value to a check digit.

        Note: this implementation only handles single-digit numeric values For non-numeric characters, override this method to provide integer-->character conversion.

        Parameters:
        charValue - The integer value of the character
        Returns:
        The converted character
        Throws:
        CheckDigitException - if integer character value doesn't represent a numeric character
      • sumDigits

        public static int sumDigits​(int number)
        Add together the individual digits in a number.
        Parameters:
        number - The number whose digits are to be added
        Returns:
        The sum of the digits