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>CUSIP</b> (North American Securities) Check Digit calculation/validation.
021 *
022 * <p>
023 * CUSIP Numbers are 9 character alphanumeric codes used
024 * to identify North American 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="https://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
036 * for more details.
037 * </p>
038 *
039 * @since 1.4
040 */
041public final class CUSIPCheckDigit extends ModulusCheckDigit {
042
043    private static final long serialVersionUID = 666941918490152456L;
044
045    /** Singleton CUSIP Check Digit instance */
046    public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
047
048    /** Weighting given to digits depending on their right position */
049    private static final int[] POSITION_WEIGHT = {2, 1};
050
051    /**
052     * Constructs a CUSIP Identifier Check Digit routine.
053     */
054    public CUSIPCheckDigit() {
055    }
056
057    /**
058     * Convert a character at a specified position to an integer value.
059     *
060     * @param character The character to convert
061     * @param leftPos The position of the character in the code, counting from left to right
062     * @param rightPos The position of the character in the code, counting from right to left
063     * @return The integer value of the character
064     * @throws CheckDigitException if the character is not alphanumeric
065     */
066    @Override
067    protected int toInt(final char character, final int leftPos, final int rightPos)
068            throws CheckDigitException {
069        final int charValue = Character.getNumericValue(character);
070        // the final character is only allowed to reach 9
071        final int charMax = rightPos == 1 ? 9 : 35;  // CHECKSTYLE IGNORE MagicNumber
072        if (charValue < 0 || charValue > charMax) {
073            throw new CheckDigitException("Invalid Character[" +
074                    leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
075        }
076        return charValue;
077    }
078
079    /**
080     * <p>Calculates the <i>weighted</i> value of a character in the
081     * code at a specified position.</p>
082     *
083     * <p>For CUSIP (from right to left) <b>odd</b> digits are weighted
084     * with a factor of <b>one</b> and <b>even</b> digits with a factor
085     * of <b>two</b>. Weighted values &gt; 9, have 9 subtracted</p>
086     *
087     * @param charValue The numeric value of the character.
088     * @param leftPos The position of the character in the code, counting from left to right
089     * @param rightPos The position of the character in the code, counting from right to left
090     * @return The weighted value of the character.
091     */
092    @Override
093    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
094        final int weight = POSITION_WEIGHT[rightPos % 2];
095        final int weightedValue = charValue * weight;
096        return sumDigits(weightedValue);
097    }
098}