    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
    019/**
    020 * Modulus 10 <b>ISIN</b> (International Securities Identifying Number) Check Digit calculation/validation.
    021 *
    022 * <p>
    023 * ISIN Numbers are 12 character alphanumeric codes used
    024 * to identify Securities.
    025 * </p>
    026 *
    027 * <p>
    028 * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
    029 * with every second digit being weighted by 2. Alphabetic characters are
    030 * converted to numbers by their position in the alphabet starting with A being 10.
    031 * Weighted numbers greater than ten are treated as two separate numbers.
    032 * </p>
    033 *
    034 * <p>
    035 * See <a href="http://en.wikipedia.org/wiki/ISIN">Wikipedia - ISIN</a>
    036 * for more details.
    037 * </p>
    038 *
    039 * @version $Revision$
    040 * @since Validator 1.4
    041 */
    042public final class ISINCheckDigit extends ModulusCheckDigit {
    043
    044    private static final long serialVersionUID = -1239211208101323599L;
    045
    046    private static final int MAX_ALPHANUMERIC_VALUE = 35; // Character.getNumericValue('Z')
    047
    048    /** Singleton ISIN Check Digit instance */
    049    public static final CheckDigit ISIN_CHECK_DIGIT = new ISINCheckDigit();
    050
    051    /** weighting given to digits depending on their right position */
    052    private static final int[] POSITION_WEIGHT = new int[] {2, 1};
    053
    054    /**
    055     * Construct an ISIN Indetifier Check Digit routine.
    056     */
    057    public ISINCheckDigit() {
    058        super(10); // CHECKSTYLE IGNORE MagicNumber
    059    }
    060
    061    /**
    062     * Calculate the modulus for an ISIN code.
    063     *
    064     * @param code The code to calculate the modulus for.
    065     * @param includesCheckDigit Whether the code includes the Check Digit or not.
    066     * @return The modulus value
    067     * @throws CheckDigitException if an error occurs calculating the modulus
    068     * for the specified code
    069     */
    070    @Override
    071    protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException {
    072        StringBuilder transformed = new  StringBuilder(code.length() * 2); // CHECKSTYLE IGNORE MagicNumber
    073        if (includesCheckDigit) {
    074            char checkDigit = code.charAt(code.length()-1); // fetch the last character
    075            if (!Character.isDigit(checkDigit)){
    076                throw new CheckDigitException("Invalid checkdigit["+ checkDigit+ "] in " + code);
    077            }
    078        }
    079        for (int i = 0; i < code.length(); i++) {
    080            int charValue = Character.getNumericValue(code.charAt(i));
    081            if (charValue < 0 || charValue > MAX_ALPHANUMERIC_VALUE) {
    082                throw new CheckDigitException("Invalid Character[" +
    083                        (i + 1) + "] = '" + charValue + "'");
    084            }
    085             // this converts alphanumerics to two digits
    086             // so there is no need to overload toInt()
    087            transformed.append(charValue);
    088        }
    089        return super.calculateModulus(transformed.toString(), includesCheckDigit);
    090    }
    091
    092    /**
    093     * <p>Calculates the <i>weighted</i> value of a charcter in the
    094     * code at a specified position.</p>
    095     *
    096     * <p>For ISIN (from right to left) <b>odd</b> digits are weighted
    097     * with a factor of <b>one</b> and <b>even</b> digits with a factor
    098     * of <b>two</b>. Weighted values are reduced to their digital root</p>
    099     *
    100     * @param charValue The numeric value of the character.
    101     * @param leftPos  The position of the character in the code, counting from left to right
    102     * @param rightPos The position of the character in the code, counting from right to left
    103     * @return The weighted value of the character.
    104     */
    105    @Override
    106    protected int weightedValue(int charValue, int leftPos, int rightPos) {
    107        int weight = POSITION_WEIGHT[rightPos % 2]; // CHECKSTYLE IGNORE MagicNumber
    108        int weightedValue = charValue * weight;
    109        return ModulusCheckDigit.sumDigits(weightedValue);
    110    }
    111}