    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="http://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        super();
    056    }
    057
    058    /**
    059     * Convert a character at a specified position to an integer value.
    060     *
    061     * @param character The character to convert
    062     * @param leftPos The position of the character in the code, counting from left to right
    063     * @param rightPos The position of the character in the code, counting from right to left
    064     * @return The integer value of the character
    065     * @throws CheckDigitException if the character is not alphanumeric
    066     */
    067    @Override
    068    protected int toInt(final char character, final int leftPos, final int rightPos)
    069            throws CheckDigitException {
    070        final int charValue = Character.getNumericValue(character);
    071        // the final character is only allowed to reach 9
    072        final int charMax = rightPos == 1 ? 9 : 35;  // CHECKSTYLE IGNORE MagicNumber
    073        if (charValue < 0 || charValue > charMax) {
    074            throw new CheckDigitException("Invalid Character[" +
    075                    leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
    076        }
    077        return charValue;
    078    }
    079
    080    /**
    081     * <p>Calculates the <i>weighted</i> value of a character in the
    082     * code at a specified position.</p>
    083     *
    084     * <p>For CUSIP (from right to left) <b>odd</b> digits are weighted
    085     * with a factor of <b>one</b> and <b>even</b> digits with a factor
    086     * of <b>two</b>. Weighted values &gt; 9, have 9 subtracted</p>
    087     *
    088     * @param charValue The numeric value of the character.
    089     * @param leftPos The position of the character in the code, counting from left to right
    090     * @param rightPos The position of the character in the code, counting from right to left
    091     * @return The weighted value of the character.
    092     */
    093    @Override
    094    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    095        final int weight = POSITION_WEIGHT[rightPos % 2];
    096        final int weightedValue = charValue * weight;
    097        return ModulusCheckDigit.sumDigits(weightedValue);
    098    }
    099}