001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator.routines.checkdigit;
018
019import java.io.Serializable;
020
021/**
022 * <b>Verhoeff</b> (Dihedral) Check Digit calculation/validation.
023 * <p>
024 * Check digit calculation for numeric codes using a
025 * <a href="http://en.wikipedia.org/wiki/Dihedral_group">Dihedral Group</a>
026 * of order 10.
027 * <p>
028 * See <a href="http://en.wikipedia.org/wiki/Verhoeff_algorithm">Wikipedia
029 *  - Verhoeff algorithm</a> for more details.
030 *
031 * @version $Revision$
032 * @since Validator 1.4
033 */
034public final class VerhoeffCheckDigit implements CheckDigit, Serializable {
035
036    private static final long serialVersionUID = 4138993995483695178L;
037
038    /** Singleton Verhoeff Check Digit instance */
039    public static final CheckDigit VERHOEFF_CHECK_DIGIT = new VerhoeffCheckDigit();
040
041    /** D - multiplication table */
042    private static final int[][] D_TABLE = new int[][] {
043        {0,  1,  2,  3,  4,  5,  6,  7,  8,  9},
044        {1,  2,  3,  4,  0,  6,  7,  8,  9,  5},
045        {2,  3,  4,  0,  1,  7,  8,  9,  5,  6},
046        {3,  4,  0,  1,  2,  8,  9,  5,  6,  7},
047        {4,  0,  1,  2,  3,  9,  5,  6,  7,  8},
048        {5,  9,  8,  7,  6,  0,  4,  3,  2,  1},
049        {6,  5,  9,  8,  7,  1,  0,  4,  3,  2},
050        {7,  6,  5,  9,  8,  2,  1,  0,  4,  3},
051        {8,  7,  6,  5,  9,  3,  2,  1,  0,  4},
052        {9,  8,  7,  6,  5,  4,  3,  2,  1,  0}};
053
054    /** P - permutation table */
055    private static final int[][] P_TABLE = new int[][] {
056        {0,  1,  2,  3,  4,  5,  6,  7,  8,  9},
057        {1,  5,  7,  6,  2,  8,  3,  0,  9,  4},
058        {5,  8,  0,  3,  7,  9,  6,  1,  4,  2},
059        {8,  9,  1,  6,  0,  4,  3,  5,  2,  7},
060        {9,  4,  5,  3,  1,  2,  6,  8,  7,  0},
061        {4,  2,  8,  6,  5,  7,  3,  9,  0,  1},
062        {2,  7,  9,  3,  8,  0,  6,  4,  1,  5},
063        {7,  0,  4,  6,  9,  1,  3,  2,  5,  8}};
064
065    /** inv: inverse table */
066    private static final int[] INV_TABLE = new int[]
067        {0,  4,  3,  2,  1,  5,  6,  7,  8,  9};
068
069
070    /**
071     * Validate the Verhoeff <i>Check Digit</i> for a code.
072     *
073     * @param code The code to validate
074     * @return <code>true</code> if the check digit is valid,
075     * otherwise <code>false</code>
076     */
077    @Override
078    public boolean isValid(String code) {
079        if (code == null || code.length() == 0) {
080            return false;
081        }
082        try {
083            return (calculateChecksum(code, true) == 0);
084        } catch (CheckDigitException e) {
085            return false;
086        }
087    }
088
089    /**
090     * Calculate a Verhoeff <i>Check Digit</i> for a code.
091     *
092     * @param code The code to calculate the Check Digit for
093     * @return The calculated Check Digit
094     * @throws CheckDigitException if an error occurs calculating
095     * the check digit for the specified code
096     */
097    @Override
098    public String calculate(String code) throws CheckDigitException {
099        if (code == null || code.length() == 0) {
100            throw new CheckDigitException("Code is missing");
101        }
102        int checksum = calculateChecksum(code, false);
103        return Integer.toString(INV_TABLE[checksum]);
104    }
105
106    /**
107     * Calculate the checksum.
108     *
109     * @param code The code to calculate the checksum for.
110     * @param includesCheckDigit Whether the code includes the Check Digit or not.
111     * @return The checksum value
112     * @throws CheckDigitException if the code contains an invalid character (i.e. not numeric)
113     */
114    private int calculateChecksum(String code, boolean includesCheckDigit) throws CheckDigitException {
115        int checksum = 0;
116        for (int i = 0; i < code.length(); i++) {
117            int idx = code.length() - (i + 1);
118            int num = Character.getNumericValue(code.charAt(idx));
119            if (num < 0 || num > 9) { // CHECKSTYLE IGNORE MagicNumber
120                throw new CheckDigitException("Invalid Character[" +
121                        i + "] = '" + ((int)code.charAt(idx)) + "'");
122            }
123            int pos = includesCheckDigit ? i : i + 1;
124            checksum = D_TABLE[checksum][P_TABLE[pos % 8][num]]; // CHECKSTYLE IGNORE MagicNumber
125        }
126        return checksum;
127    }
128
129}