Class ModulusCheckDigit
- java.lang.Object
-
- org.apache.commons.validator.routines.checkdigit.ModulusCheckDigit
-
- All Implemented Interfaces:
java.io.Serializable
,CheckDigit
- Direct Known Subclasses:
ABANumberCheckDigit
,CUSIPCheckDigit
,EAN13CheckDigit
,ISBN10CheckDigit
,ISINCheckDigit
,ISSNCheckDigit
,LuhnCheckDigit
,SedolCheckDigit
public abstract class ModulusCheckDigit extends java.lang.Object implements CheckDigit, java.io.Serializable
Abstract Modulus Check digit calculation/validation.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()
andtoChar()
methods.- Since:
- Validator 1.4
- Version:
- $Revision: 1713562 $
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private int
modulus
private static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description ModulusCheckDigit(int modulus)
Construct aCheckDigit
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.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
modulus
private final int modulus
-
-
Constructor Detail
-
ModulusCheckDigit
public ModulusCheckDigit(int modulus)
Construct aCheckDigit
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 interfaceCheckDigit
- Parameters:
code
- The code to validate- Returns:
true
if the check digit is valid, otherwisefalse
-
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 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
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 characterleftPos
- 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
- 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 convertleftPos
- 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
-
-