    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 *      https://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 <strong>ISIN</strong> (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 <em>Modulus 10 Double Add Double</em> 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="https://en.wikipedia.org/wiki/ISIN">Wikipedia - ISIN</a>
    036 * for more details.
    037 * </p>
    038 *
    039 * @since 1.4
    040 */
    041public final class ISINCheckDigit extends ModulusCheckDigit {
    042
    043    private static final long serialVersionUID = -1239211208101323599L;
    044
    045    /**
    046     * Singleton ISIN Check Digit instance.
    047     */
    048    public static final CheckDigit ISIN_CHECK_DIGIT = new ISINCheckDigit();
    049
    050    /** Weighting given to digits depending on their right position */
    051    private static final int[] POSITION_WEIGHT = {2, 1};
    052
    053    /**
    054     * Constructs an ISIN Identifier Check Digit routine.
    055     */
    056    public ISINCheckDigit() {
    057    }
    058
    059    /**
    060     * Calculate the modulus for an ISIN code.
    061     *
    062     * @param code               The code to calculate the modulus for.
    063     * @param includesCheckDigit Whether the code includes the Check Digit or not.
    064     * @return The modulus value.
    065     * @throws CheckDigitException if an error occurs calculating the modulus for the specified code.
    066     */
    067    @Override
    068    protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException {
    069        final StringBuilder transformed = new StringBuilder(code.length() * 2); // CHECKSTYLE IGNORE MagicNumber
    070        if (includesCheckDigit) {
    071            final char checkDigit = code.charAt(code.length() - 1); // fetch the last character
    072            if (!isAsciiDigit(checkDigit)) {
    073                throw new CheckDigitException("Invalid checkdigit[%c] in %s", checkDigit, code);
    074            }
    075        }
    076        for (int i = 0; i < code.length(); i++) {
    077            final char character = code.charAt(i);
    078            if (!isAsciiAlphaNum(character)) {
    079                throw new CheckDigitException("Invalid Character[%d] = '%c'", i + 1, character);
    080            }
    081            // this converts alphanumerics to two digits
    082            // so there is no need to overload toInt()
    083            transformed.append(Character.getNumericValue(character));
    084        }
    085        return super.calculateModulus(transformed.toString(), includesCheckDigit);
    086    }
    087
    088    /**
    089     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
    090     *
    091     * <p>
    092     * For ISIN (from right to left) <strong>odd</strong> digits are weighted with a factor of <strong>one</strong> and <strong>even</strong> digits with a
    093     * factor of <strong>two</strong>. Weighted values are reduced to their digital root
    094     * </p>
    095     *
    096     * @param charValue The numeric value of the character.
    097     * @param leftPos   The position of the character in the code, counting from left to right.
    098     * @param rightPos  The position of the character in the code, counting from right to left.
    099     * @return The weighted value of the character.
    100     */
    101    @Override
    102    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    103        final int weight = POSITION_WEIGHT[rightPos % 2]; // CHECKSTYLE IGNORE MagicNumber
    104        final int weightedValue = charValue * weight;
    105        return sumDigits(weightedValue);
    106    }
    107}