CUSIPCheckDigit.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.validator.routines.checkdigit;

  18. /**
  19.  * Modulus 10 <b>CUSIP</b> (North American Securities) Check Digit calculation/validation.
  20.  *
  21.  * <p>
  22.  * CUSIP Numbers are 9 character alphanumeric codes used
  23.  * to identify North American Securities.
  24.  * </p>
  25.  *
  26.  * <p>
  27.  * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
  28.  * with every second digit being weighted by 2. Alphabetic characters are
  29.  * converted to numbers by their position in the alphabet starting with A being 10.
  30.  * Weighted numbers greater than ten are treated as two separate numbers.
  31.  * </p>
  32.  *
  33.  * <p>
  34.  * See <a href="https://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
  35.  * for more details.
  36.  * </p>
  37.  *
  38.  * @since 1.4
  39.  */
  40. public final class CUSIPCheckDigit extends ModulusCheckDigit {

  41.     private static final long serialVersionUID = 666941918490152456L;

  42.     /** Singleton CUSIP Check Digit instance */
  43.     public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();

  44.     /** Weighting given to digits depending on their right position */
  45.     private static final int[] POSITION_WEIGHT = {2, 1};

  46.     /**
  47.      * Constructs a CUSIP Identifier Check Digit routine.
  48.      */
  49.     public CUSIPCheckDigit() {
  50.     }

  51.     /**
  52.      * Convert a character at a specified position to an integer value.
  53.      *
  54.      * @param character The character to convert
  55.      * @param leftPos The position of the character in the code, counting from left to right
  56.      * @param rightPos The position of the character in the code, counting from right to left
  57.      * @return The integer value of the character
  58.      * @throws CheckDigitException if the character is not alphanumeric
  59.      */
  60.     @Override
  61.     protected int toInt(final char character, final int leftPos, final int rightPos)
  62.             throws CheckDigitException {
  63.         final int charValue = Character.getNumericValue(character);
  64.         // the final character is only allowed to reach 9
  65.         final int charMax = rightPos == 1 ? 9 : 35;  // CHECKSTYLE IGNORE MagicNumber
  66.         if (charValue < 0 || charValue > charMax) {
  67.             throw new CheckDigitException("Invalid Character[" +
  68.                     leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
  69.         }
  70.         return charValue;
  71.     }

  72.     /**
  73.      * <p>Calculates the <i>weighted</i> value of a character in the
  74.      * code at a specified position.</p>
  75.      *
  76.      * <p>For CUSIP (from right to left) <b>odd</b> digits are weighted
  77.      * with a factor of <b>one</b> and <b>even</b> digits with a factor
  78.      * of <b>two</b>. Weighted values &gt; 9, have 9 subtracted</p>
  79.      *
  80.      * @param charValue The numeric value of the character.
  81.      * @param leftPos The position of the character in the code, counting from left to right
  82.      * @param rightPos The position of the character in the code, counting from right to left
  83.      * @return The weighted value of the character.
  84.      */
  85.     @Override
  86.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
  87.         final int weight = POSITION_WEIGHT[rightPos % 2];
  88.         final int weightedValue = charValue * weight;
  89.         return sumDigits(weightedValue);
  90.     }
  91. }