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 * https://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 <strong>CUSIP</strong> (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 <em>Modulus 10 Double Add Double</em> 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="https://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 }
56
57 /**
58 * Convert a character at a specified position to an integer value.
59 *
60 * @param character The character to convert
61 * @param leftPos The position of the character in the code, counting from left to right
62 * @param rightPos The position of the character in the code, counting from right to left
63 * @return The integer value of the character
64 * @throws CheckDigitException if the character is not alphanumeric
65 */
66 @Override
67 protected int toInt(final char character, final int leftPos, final int rightPos)
68 throws CheckDigitException {
69 final int charValue = Character.getNumericValue(character);
70 // the final character is only allowed to reach 9
71 final int charMax = rightPos == 1 ? 9 : 35; // CHECKSTYLE IGNORE MagicNumber
72 if (charValue < 0 || charValue > charMax) {
73 throw new CheckDigitException("Invalid Character[" +
74 leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
75 }
76 return charValue;
77 }
78
79 /**
80 * <p>Calculates the <em>weighted</em> value of a character in the
81 * code at a specified position.</p>
82 *
83 * <p>For CUSIP (from right to left) <strong>odd</strong> digits are weighted
84 * with a factor of <strong>one</strong> and <strong>even</strong> digits with a factor
85 * of <strong>two</strong>. Weighted values > 9, have 9 subtracted</p>
86 *
87 * @param charValue The numeric value of the character.
88 * @param leftPos The position of the character in the code, counting from left to right
89 * @param rightPos The position of the character in the code, counting from right to left
90 * @return The weighted value of the character.
91 */
92 @Override
93 protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
94 final int weight = POSITION_WEIGHT[rightPos % 2];
95 final int weightedValue = charValue * weight;
96 return sumDigits(weightedValue);
97 }
98 }