Class ModulusCheckDigit
- All Implemented Interfaces:
Serializable, CheckDigit
- Direct Known Subclasses:
ABANumberCheckDigit, CASNumberCheckDigit, CUSIPCheckDigit, EAN13CheckDigit, ECNumberCheckDigit, ISBN10CheckDigit, ISINCheckDigit, ISSNCheckDigit, LuhnCheckDigit, ModulusTenCheckDigit, SedolCheckDigit
Provides a base class for building modulus Check Digit routines.
This implementation only handles single-digit numeric codes, such as EAN-13. For alphanumeric codes such as
EAN-128 you will need to implement/override the toInt() and toChar() methods.
- Since:
- 1.4
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionModulusCheckDigit(int modulus) Constructs aCheckDigitroutine for a specified modulus. -
Method Summary
Modifier and TypeMethodDescriptionCalculates a modulus Check Digit for a code which does not yet have one.protected intcalculateModulus(String code, boolean includesCheckDigit) Calculates the modulus for a code.intGets the modulus value this check digit routine is based on.booleanValidates a modulus check digit for a code.static intsumDigits(int number) Adds together the individual digits in a number.protected StringtoCheckDigit(int charValue) Converts an integer value to a check digit.protected inttoInt(char character, int leftPos, int rightPos) Converts a character at a specified position to an integer value.protected abstract intweightedValue(int charValue, int leftPos, int rightPos) Calculates the weighted value of a character in the code at a specified position.
-
Constructor Details
-
ModulusCheckDigit
Constructs aCheckDigitroutine for a specified modulus.- Parameters:
modulus- The modulus value to use for the check digit calculation.
-
-
Method Details
-
sumDigits
Adds together the individual digits in a number.- Parameters:
number- The number whose digits are to be added.- Returns:
- The sum of the digits.
-
calculate
Calculates a modulus Check Digit for a code which does not yet have one.- Specified by:
calculatein interfaceCheckDigit- 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
Calculates 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.
-
getModulus
Gets the modulus value this check digit routine is based on.- Returns:
- The modulus value this check digit routine is based on.
-
isValid
Validates a modulus check digit for a code.- Specified by:
isValidin interfaceCheckDigit- Parameters:
code- The code to validate.- Returns:
trueif the check digit is valid, otherwisefalse.
-
toCheckDigit
Converts 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.
-
toInt
Converts 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 identifying 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.
-
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 (for example, ISBN-10), while others use different weighting factors for odd/even positions (for example, 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 position of 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.
-