Class Luhn

java.lang.Object
com.globalmentor.math.Luhn

public class Luhn extends Object
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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static final int
    doubleAdd(int digitValue)
    Doubles the given digit value and adds the two decimal digits of the outcome.
    static final char
    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
    Determines if the given sequence of digits ends with the correct check digit.

    Methods inherited from class java.lang.Object

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

    • Luhn

      public Luhn()
  • Method Details

    • isDigitsCheck

      public static final boolean isDigitsCheck(CharSequence digits)
      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 returns false.
      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 is null.
      IllegalArgumentException - if one of the the given digits is not a character between '0' to '9', inclusive.
    • getCheckDigit

      public static final char getCheckDigit(CharSequence digits)
      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 is null.
      IllegalArgumentException - if one of the the given digits is not a character between '0' to '9', inclusive.
    • getCheckDigit

      public static final char getCheckDigit(CharSequence digits, int start, int end)
      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 is null.
      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 is null.
      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.