VerhoeffCheckDigit.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.validator.routines.checkdigit;

  18. import java.io.Serializable;

  19. import org.apache.commons.validator.GenericValidator;

  20. /**
  21.  * <b>Verhoeff</b> (Dihedral) Check Digit calculation/validation.
  22.  * <p>
  23.  * Check digit calculation for numeric codes using a
  24.  * <a href="https://en.wikipedia.org/wiki/Dihedral_group">Dihedral Group</a>
  25.  * of order 10.
  26.  * <p>
  27.  * See <a href="https://en.wikipedia.org/wiki/Verhoeff_algorithm">Wikipedia
  28.  *  - Verhoeff algorithm</a> for more details.
  29.  *
  30.  * @since 1.4
  31.  */
  32. public final class VerhoeffCheckDigit extends AbstractCheckDigit implements Serializable {

  33.     private static final long serialVersionUID = 4138993995483695178L;

  34.     /** Singleton Verhoeff Check Digit instance */
  35.     public static final CheckDigit VERHOEFF_CHECK_DIGIT = new VerhoeffCheckDigit();

  36.     /** D - multiplication table */
  37.     private static final int[][] D_TABLE = {
  38.         {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  39.         {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
  40.         {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
  41.         {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
  42.         {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
  43.         {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
  44.         {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
  45.         {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
  46.         {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
  47.         {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}};

  48.     /** P - permutation table */
  49.     private static final int[][] P_TABLE = {
  50.         {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  51.         {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
  52.         {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
  53.         {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
  54.         {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
  55.         {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
  56.         {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
  57.         {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}};

  58.     /** Inverse table */
  59.     private static final int[] INV_TABLE = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9};

  60.     /**
  61.      * Calculate a Verhoeff <i>Check Digit</i> for a code.
  62.      *
  63.      * @param code The code to calculate the Check Digit for
  64.      * @return The calculated Check Digit
  65.      * @throws CheckDigitException if an error occurs calculating
  66.      * the check digit for the specified code
  67.      */
  68.     @Override
  69.     public String calculate(final String code) throws CheckDigitException {
  70.         if (GenericValidator.isBlankOrNull(code)) {
  71.             throw new CheckDigitException("Code is missing");
  72.         }
  73.         final int checksum = calculateChecksum(code, false);
  74.         return Integer.toString(INV_TABLE[checksum]);
  75.     }

  76.     /**
  77.      * Calculate the checksum.
  78.      *
  79.      * @param code The code to calculate the checksum for.
  80.      * @param includesCheckDigit Whether the code includes the Check Digit or not.
  81.      * @return The checksum value
  82.      * @throws CheckDigitException if the code contains an invalid character (i.e. not numeric)
  83.      */
  84.     private int calculateChecksum(final String code, final boolean includesCheckDigit) throws CheckDigitException {
  85.         int checksum = 0;
  86.         for (int i = 0; i < code.length(); i++) {
  87.             final int idx = code.length() - (i + 1);
  88.             final int num = Character.getNumericValue(code.charAt(idx));
  89.             if (num < 0 || num > 9) { // CHECKSTYLE IGNORE MagicNumber
  90.                 throw new CheckDigitException("Invalid Character[" +
  91.                         i + "] = '" + (int) code.charAt(idx) + "'");
  92.             }
  93.             final int pos = includesCheckDigit ? i : i + 1;
  94.             checksum = D_TABLE[checksum][P_TABLE[pos % 8][num]]; // CHECKSTYLE IGNORE MagicNumber
  95.         }
  96.         return checksum;
  97.     }

  98.     /**
  99.      * Validate the Verhoeff <i>Check Digit</i> for a code.
  100.      *
  101.      * @param code The code to validate
  102.      * @return {@code true} if the check digit is valid,
  103.      * otherwise {@code false}
  104.      */
  105.     @Override
  106.     public boolean isValid(final String code) {
  107.         if (GenericValidator.isBlankOrNull(code)) {
  108.             return false;
  109.         }
  110.         try {
  111.             return calculateChecksum(code, true) == 0;
  112.         } catch (final CheckDigitException e) {
  113.             return false;
  114.         }
  115.     }

  116. }