View Javadoc
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  
19  import java.io.Serializable;
20  
21  /**
22   * <b>Verhoeff</b> (Dihedral) Check Digit calculation/validation.
23   * <p>
24   * Check digit calculation for numeric codes using a
25   * <a href="http://en.wikipedia.org/wiki/Dihedral_group">Dihedral Group</a>
26   * of order 10.
27   * <p>
28   * See <a href="http://en.wikipedia.org/wiki/Verhoeff_algorithm">Wikipedia
29   *  - Verhoeff algorithm</a> for more details.
30   *
31   * @since 1.4
32   */
33  public final class VerhoeffCheckDigit implements CheckDigit, Serializable {
34  
35      private static final long serialVersionUID = 4138993995483695178L;
36  
37      /** Singleton Verhoeff Check Digit instance */
38      public static final CheckDigit VERHOEFF_CHECK_DIGIT = new VerhoeffCheckDigit();
39  
40      /** D - multiplication table */
41      private static final int[][] D_TABLE = {
42          {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
43          {1, 2, 3, 4, 0, 6, 7, 8, 9, 5},
44          {2, 3, 4, 0, 1, 7, 8, 9, 5, 6},
45          {3, 4, 0, 1, 2, 8, 9, 5, 6, 7},
46          {4, 0, 1, 2, 3, 9, 5, 6, 7, 8},
47          {5, 9, 8, 7, 6, 0, 4, 3, 2, 1},
48          {6, 5, 9, 8, 7, 1, 0, 4, 3, 2},
49          {7, 6, 5, 9, 8, 2, 1, 0, 4, 3},
50          {8, 7, 6, 5, 9, 3, 2, 1, 0, 4},
51          {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}};
52  
53      /** P - permutation table */
54      private static final int[][] P_TABLE = {
55          {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
56          {1, 5, 7, 6, 2, 8, 3, 0, 9, 4},
57          {5, 8, 0, 3, 7, 9, 6, 1, 4, 2},
58          {8, 9, 1, 6, 0, 4, 3, 5, 2, 7},
59          {9, 4, 5, 3, 1, 2, 6, 8, 7, 0},
60          {4, 2, 8, 6, 5, 7, 3, 9, 0, 1},
61          {2, 7, 9, 3, 8, 0, 6, 4, 1, 5},
62          {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}};
63  
64      /** inv: inverse table */
65      private static final int[] INV_TABLE = {0, 4, 3, 2, 1, 5, 6, 7, 8, 9};
66  
67      /**
68       * Calculate a Verhoeff <i>Check Digit</i> for a code.
69       *
70       * @param code The code to calculate the Check Digit for
71       * @return The calculated Check Digit
72       * @throws CheckDigitException if an error occurs calculating
73       * the check digit for the specified code
74       */
75      @Override
76      public String calculate(final String code) throws CheckDigitException {
77          if (code == null || code.isEmpty()) {
78              throw new CheckDigitException("Code is missing");
79          }
80          final int checksum = calculateChecksum(code, false);
81          return Integer.toString(INV_TABLE[checksum]);
82      }
83  
84      /**
85       * Calculate the checksum.
86       *
87       * @param code The code to calculate the checksum for.
88       * @param includesCheckDigit Whether the code includes the Check Digit or not.
89       * @return The checksum value
90       * @throws CheckDigitException if the code contains an invalid character (i.e. not numeric)
91       */
92      private int calculateChecksum(final String code, final boolean includesCheckDigit) throws CheckDigitException {
93          int checksum = 0;
94          for (int i = 0; i < code.length(); i++) {
95              final int idx = code.length() - (i + 1);
96              final int num = Character.getNumericValue(code.charAt(idx));
97              if (num < 0 || num > 9) { // CHECKSTYLE IGNORE MagicNumber
98                  throw new CheckDigitException("Invalid Character[" +
99                          i + "] = '" + (int)code.charAt(idx) + "'");
100             }
101             final int pos = includesCheckDigit ? i : i + 1;
102             checksum = D_TABLE[checksum][P_TABLE[pos % 8][num]]; // CHECKSTYLE IGNORE MagicNumber
103         }
104         return checksum;
105     }
106 
107     /**
108      * Validate the Verhoeff <i>Check Digit</i> for a code.
109      *
110      * @param code The code to validate
111      * @return {@code true} if the check digit is valid,
112      * otherwise {@code false}
113      */
114     @Override
115     public boolean isValid(final String code) {
116         if (code == null || code.isEmpty()) {
117             return false;
118         }
119         try {
120             return calculateChecksum(code, true) == 0;
121         } catch (final CheckDigitException e) {
122             return false;
123         }
124     }
125 
126 }