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   * @version $Revision: 1649191 $
40   * @since Validator 1.4
41   */
42  public final class CUSIPCheckDigit extends ModulusCheckDigit {
43  
44      private static final long serialVersionUID = 666941918490152456L;
45  
46      /** Singleton CUSIP Check Digit instance */
47      public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
48  
49      /** weighting given to digits depending on their right position */
50      private static final int[] POSITION_WEIGHT = new int[] {2, 1};
51  
52      /**
53       * Construct an CUSIP Indetifier Check Digit routine.
54       */
55      public CUSIPCheckDigit() {
56          super(10);
57      }
58  
59      /**
60       * Convert a character at a specified position to an integer value.
61       *
62       * @param character The character to convert
63       * @param leftPos The position of the character in the code, counting from left to right
64       * @param rightPos The position of the character in the code, counting from right to left
65       * @return The integer value of the character
66       * @throws CheckDigitException if character is not alphanumeric
67       */
68      protected int toInt(char character, int leftPos, int rightPos)
69              throws CheckDigitException {
70          int charValue = Character.getNumericValue(character);
71          // the final character is only allowed to reach 9
72          final int charMax = rightPos == 1 ? 9 : 35;
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 charcter 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 positionof the character in the code, counting from right to left
91       * @return The weighted value of the character.
92       */
93      protected int weightedValue(int charValue, int leftPos, int rightPos) {
94          int weight = POSITION_WEIGHT[rightPos % 2];
95          int weightedValue = (charValue * weight);
96          return ModulusCheckDigit.sumDigits(weightedValue);
97      }
98  }