Package com.globalmentor.math
Class Luhn
java.lang.Object
com.globalmentor.math.Luhn
Methods implementing the Luhn Algorithm or "modulus 10". Used, for example, by the Primary Account Number (PAN) of an identification card as defined in
ISO/IEC 7812-1:2000(E), "Identification cards — Identification of issuers — Part 1: Numbering system". See ISO/IEC 7812-1:2000(E),
"Annex B: Luhn formula for computing modulus 10 'double-add-double' check digit".
- Author:
- Garret Wilson
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic final int
doubleAdd
(int digitValue) Doubles the given digit value and adds the two decimal digits of the outcome.static final char
getCheckDigit
(CharSequence digits) Calculates the check digit of the entire given sequence of digits.static final char
getCheckDigit
(CharSequence digits, int start, int end) Calculates the check digit of the given sequence of digits within the given range.static final int
getCheckDigitValue
(int[] digitValues) Determines the value of the check digit for the given digit values.static final boolean
isDigitsCheck
(CharSequence digits) Determines if the given sequence of digits ends with the correct check digit.
-
Constructor Details
-
Luhn
public Luhn()
-
-
Method Details
-
isDigitsCheck
Determines if the given sequence of digits ends with the correct check digit. Each digit must be a value from '0' to '9', inclusive. If the sequence of digits is empty, this method returnsfalse
.- Parameters:
digits
- The digits to be verified against a trailing check digit.- Returns:
true
if this character sequence ends with a check digit that correctly checks the other characters according to the Luhn algorithm.- Throws:
NullPointerException
- if the given sequence of digits isnull
.IllegalArgumentException
- if one of the the given digits is not a character between '0' to '9', inclusive.
-
getCheckDigit
Calculates the check digit of the entire given sequence of digits. Each digit must be a value from '0' to '9', inclusive.- Parameters:
digits
- The digits for which a check digit should be calculated.- Returns:
- The character representing the check digit generated for the given digits according to the Luhn algorithm.
- Throws:
NullPointerException
- if the given sequence of digits isnull
.IllegalArgumentException
- if one of the the given digits is not a character between '0' to '9', inclusive.
-
getCheckDigit
Calculates the check digit of the given sequence of digits within the given range. Each digit must be a value from '0' to '9', inclusive. If the sequence of digits is empty, this method returns '0'.- Parameters:
digits
- The digits for which a check digit should be calculated.start
- The starting index for which a check digit should be calculated.end
- One more than the last index to include in a check digit calculation.- Returns:
- The character representing the check digit generated for the given digits according to the Luhn algorithm.
- Throws:
NullPointerException
- if the given sequence of digits isnull
.IndexOutOfBoundsException
- if the given range references one or more indexes outside the character sequence.IllegalArgumentException
- if one of the the given digits is not a character between '0' to '9', inclusive.
-
getCheckDigitValue
public static final int getCheckDigitValue(int[] digitValues) Determines the value of the check digit for the given digit values. If the sequence of digits is empty, this method returns zero.- Parameters:
digitValues
- The values digits represent.- Returns:
- The check digit value the Luhn algorithm specifies.
- Throws:
NullPointerException
- if the given array of values isnull
.IllegalArgumentException
- if one of the the given digit values is 10 or greater.
-
doubleAdd
public static final int doubleAdd(int digitValue) Doubles the given digit value and adds the two decimal digits of the outcome.- Parameters:
digitValue
- The digit value to double and then add.- Returns:
- The sum of the decimal digits of the doubled value.
- Throws:
IllegalArgumentException
- if the given digit value is 10 or greater.
-