View Javadoc
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  /**
20   * Modulus 10 <b>CUSIP</b> (North American Securities) Check Digit calculation/validation.
21   *
22   * <p>
23   * CUSIP Numbers are 9 character alphanumeric codes used
24   * to identify North American Securities.
25   * </p>
26   *
27   * <p>
28   * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
29   * with every second digit being weighted by 2. Alphabetic characters are
30   * converted to numbers by their position in the alphabet starting with A being 10.
31   * Weighted numbers greater than ten are treated as two separate numbers.
32   * </p>
33   *
34   * <p>
35   * See <a href="http://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
36   * for more details.
37   * </p>
38   *
39   * @since 1.4
40   */
41  public final class CUSIPCheckDigit extends ModulusCheckDigit {
42  
43      private static final long serialVersionUID = 666941918490152456L;
44  
45      /** Singleton CUSIP Check Digit instance */
46      public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
47  
48      /** weighting given to digits depending on their right position */
49      private static final int[] POSITION_WEIGHT = {2, 1};
50  
51      /**
52       * Constructs a CUSIP Identifier Check Digit routine.
53       */
54      public CUSIPCheckDigit() {
55          super();
56      }
57  
58      /**
59       * Convert a character at a specified position to an integer value.
60       *
61       * @param character The character to convert
62       * @param leftPos The position of the character in the code, counting from left to right
63       * @param rightPos The position of the character in the code, counting from right to left
64       * @return The integer value of the character
65       * @throws CheckDigitException if the character is not alphanumeric
66       */
67      @Override
68      protected int toInt(final char character, final int leftPos, final int rightPos)
69              throws CheckDigitException {
70          final int charValue = Character.getNumericValue(character);
71          // the final character is only allowed to reach 9
72          final int charMax = rightPos == 1 ? 9 : 35;  // CHECKSTYLE IGNORE MagicNumber
73          if (charValue < 0 || charValue > charMax) {
74              throw new CheckDigitException("Invalid Character[" +
75                      leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
76          }
77          return charValue;
78      }
79  
80      /**
81       * <p>Calculates the <i>weighted</i> value of a character in the
82       * code at a specified position.</p>
83       *
84       * <p>For CUSIP (from right to left) <b>odd</b> digits are weighted
85       * with a factor of <b>one</b> and <b>even</b> digits with a factor
86       * of <b>two</b>. Weighted values &gt; 9, have 9 subtracted</p>
87       *
88       * @param charValue The numeric value of the character.
89       * @param leftPos The position of the character in the code, counting from left to right
90       * @param rightPos The position of the character in the code, counting from right to left
91       * @return The weighted value of the character.
92       */
93      @Override
94      protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
95          final int weight = POSITION_WEIGHT[rightPos % 2];
96          final int weightedValue = charValue * weight;
97          return ModulusCheckDigit.sumDigits(weightedValue);
98      }
99  }