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 * @version $Revision: 1649191 $
040 * @since Validator 1.4
041 */
042public final class CUSIPCheckDigit extends ModulusCheckDigit {
043
044    private static final long serialVersionUID = 666941918490152456L;
045
046    /** Singleton CUSIP Check Digit instance */
047    public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
048
049    /** weighting given to digits depending on their right position */
050    private static final int[] POSITION_WEIGHT = new int[] {2, 1};
051
052    /**
053     * Construct an CUSIP Indetifier Check Digit routine.
054     */
055    public CUSIPCheckDigit() {
056        super(10);
057    }
058
059    /**
060     * Convert a character at a specified position to an integer value.
061     *
062     * @param character The character to convert
063     * @param leftPos The position of the character in the code, counting from left to right
064     * @param rightPos The position of the character in the code, counting from right to left
065     * @return The integer value of the character
066     * @throws CheckDigitException if character is not alphanumeric
067     */
068    protected int toInt(char character, int leftPos, int rightPos)
069            throws CheckDigitException {
070        int charValue = Character.getNumericValue(character);
071        // the final character is only allowed to reach 9
072        final int charMax = rightPos == 1 ? 9 : 35;
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 charcter 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 positionof the character in the code, counting from right to left
091     * @return The weighted value of the character.
092     */
093    protected int weightedValue(int charValue, int leftPos, int rightPos) {
094        int weight = POSITION_WEIGHT[rightPos % 2];
095        int weightedValue = (charValue * weight);
096        return ModulusCheckDigit.sumDigits(weightedValue);
097    }
098}