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)
021 * Check Digit calculation/validation.
022 * <p>
023 * CUSIP Numbers are 9 character alphanumeric codes used
024 * to identify North American Securities.
025 * <p>
026 * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
027 * with every second digit being weighted by 2. Alphabetic characters are
028 * converted to numbers by their position in the alphabet starting with A being 10.
029 * Weighted numbers greater than ten are treated as two separate numbers.
030 * <p>
031 *
032 * <p>
033 * See <a href="http://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
034 * for more details.
035 *
036 * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Do, 05 Jan 2012) $
037 * @since Validator 1.4
038 */
039public final class CUSIPCheckDigit extends ModulusCheckDigit {
040
041    private static final long serialVersionUID = 666941918490152456L;
042
043    /** Singleton CUSIP Check Digit instance */
044    public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
045
046    /** weighting given to digits depending on their right position */
047    private static final int[] POSITION_WEIGHT = new int[] {2, 1};
048
049    /**
050     * Construct an CUSIP Indetifier Check Digit routine.
051     */
052    public CUSIPCheckDigit() {
053        super(10);
054    }
055
056    /**
057     * Convert a character at a specified position to an integer value.
058     *
059     * @param character The character to convert
060     * @param leftPos The position of the character in the code, counting from left to right
061     * @param rightPos The positionof the character in the code, counting from right to left
062     * @return The integer value of the character
063     * @throws CheckDigitException if character is not alphanumeric
064     */
065    protected int toInt(char character, int leftPos, int rightPos)
066            throws CheckDigitException {
067        int charValue = Character.getNumericValue(character);
068        if (charValue < 0 || charValue > 35) {
069            throw new CheckDigitException("Invalid Character[" +
070                    leftPos + "] = '" + charValue + "'");
071        }
072        return charValue;
073    }
074
075    /**
076     * <p>Calculates the <i>weighted</i> value of a charcter in the
077     * code at a specified position.</p>
078     *
079     * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
080     * with a factor of <b>one</b> and <b>even</b> digits with a factor
081     * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
082     *
083     * @param charValue The numeric value of the character.
084     * @param leftPos The position of the character in the code, counting from left to right
085     * @param rightPos The positionof the character in the code, counting from right to left
086     * @return The weighted value of the character.
087     */
088    protected int weightedValue(int charValue, int leftPos, int rightPos) {
089        int weight = POSITION_WEIGHT[rightPos % 2];
090        int weightedValue = (charValue * weight);
091        return ModulusCheckDigit.sumDigits(weightedValue);
092    }
093}