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 */
017
018package org.apache.commons.validator.routines.checkdigit;
019
020/**
021 * Modulus 10 <strong>CUSIP</strong> (North American Securities) Check Digit calculation/validation.
022 *
023 * <p>
024 * CUSIP Numbers are 9 character alphanumeric codes used to identify North American Securities.
025 * </p>
026 *
027 * <p>
028 * Check digit calculation uses the <em>Modulus 10 Double Add Double</em> technique with every second digit being weighted by 2. Alphabetic characters are
029 * converted to numbers by their position in the alphabet starting with A being 10. Weighted numbers greater than ten are treated as two separate numbers.
030 * </p>
031 *
032 * <p>
033 * See <a href="https://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a> for more details.
034 * </p>
035 *
036 * @since 1.4
037 */
038public final class CUSIPCheckDigit extends ModulusCheckDigit {
039
040    private static final long serialVersionUID = 666941918490152456L;
041
042    /**
043     * Singleton CUSIP Check Digit instance.
044     */
045    public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
046
047    /** Weighting given to digits depending on their right position */
048    private static final int[] POSITION_WEIGHT = { 2, 1 };
049
050    /** A CUSIP is exactly nine characters, the last being the check digit. */
051    private static final int CUSIP_LEN = 9;
052
053    /**
054     * Constructs a CUSIP Identifier Check Digit routine.
055     */
056    public CUSIPCheckDigit() {
057    }
058
059    /**
060     * {@inheritDoc}
061     *
062     * <p>
063     * The weight is taken from {@code rightPos}, which does not change when a character is prepended, so
064     * {@code ModulusCheckDigit} would accept an over-length code whose leading character lands on a no-op weight (for
065     * example {@code 0037833100}). The nine-character length is checked here before the check digit test.
066     * </p>
067     */
068    @Override
069    public boolean isValid(final String code) {
070        return isLength(code, CUSIP_LEN) && super.isValid(code);
071    }
072
073    /**
074     * Convert a character at a specified position to an integer value.
075     *
076     * @param character The character to convert.
077     * @param leftPos   The position of the character in the code, counting from left to right.
078     * @param rightPos  The position of the character in the code, counting from right to left.
079     * @return The integer value of the character.
080     * @throws CheckDigitException if the character is not alphanumeric.
081     */
082    @Override
083    protected int toInt(final char character, final int leftPos, final int rightPos) throws CheckDigitException {
084        final int charValue = Character.getNumericValue(character);
085        // the final character is only allowed to reach 9
086        final int charValueMax = rightPos == 1 ? 9 : 35; // CHECKSTYLE IGNORE MagicNumber
087        if (charValue > charValueMax || !isAsciiAlphaNum(character)) {
088            throw new CheckDigitException("Invalid Character[%d,%d] = '%d' out of range 0 to %d", leftPos, rightPos, charValue, charValueMax);
089        }
090        return charValue;
091    }
092
093    /**
094     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
095     *
096     * <p>
097     * For CUSIP (from right to left) <strong>odd</strong> digits are weighted with a factor of <strong>one</strong> and <strong>even</strong> digits with a
098     * factor of <strong>two</strong>. Weighted values &gt; 9, have 9 subtracted
099     * </p>
100     *
101     * @param charValue The numeric value of the character.
102     * @param leftPos   The position of the character in the code, counting from left to right.
103     * @param rightPos  The position of the character in the code, counting from right to left.
104     * @return The weighted value of the character.
105     */
106    @Override
107    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
108        final int weight = POSITION_WEIGHT[rightPos % 2];
109        final int weightedValue = charValue * weight;
110        return sumDigits(weightedValue);
111    }
112}