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>SEDOL</b> (UK Securities) Check Digit calculation/validation.
21 * <p>
22 * SEDOL Numbers are 7 character alphanumeric codes used
23 * to identify UK Securities (SEDOL stands for Stock Exchange Daily Official List).
24 * <p>
25 * Check digit calculation is based on <i>modulus 10</i> with digits being weighted
26 * based on their position, from left to right, as follows:
27 * <p>
28 * <pre><code>
29 * position: 1 2 3 4 5 6 7
30 * weighting: 1 3 1 7 3 9 1
31 * </code></pre>
32 * <p>
33 * See <a href="http://en.wikipedia.org/wiki/SEDOL">Wikipedia - SEDOL</a>
34 * for more details.
35 *
36 * @version $Revision: 1227719 $ $Date: 2012-01-05 12:45:51 -0500 (Thu, 05 Jan 2012) $
37 * @since Validator 1.4
38 */
39 public final class SedolCheckDigit extends ModulusCheckDigit {
40
41 private static final long serialVersionUID = -8976881621148878443L;
42
43 /** Singleton SEDOL check digit instance */
44 public static final CheckDigit SEDOL_CHECK_DIGIT = new SedolCheckDigit();
45
46 /** weighting given to digits depending on their right position */
47 private static final int[] POSITION_WEIGHT = new int[] {1, 3, 1, 7, 3, 9, 1};
48
49 /**
50 * Construct a modulus 11 Check Digit routine for ISBN-10.
51 */
52 public SedolCheckDigit() {
53 super(10);
54 }
55
56 /**
57 * Calculate the modulus for an SEDOL code.
58 *
59 * @param code The code to calculate the modulus for.
60 * @param includesCheckDigit Whether the code includes the Check Digit or not.
61 * @return The modulus value
62 * @throws CheckDigitException if an error occurs calculating the modulus
63 * for the specified code
64 */
65 protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException {
66 if (code.length() > 7) {
67 throw new CheckDigitException("Invalid Code Length = " + code.length());
68 }
69 return super.calculateModulus(code, includesCheckDigit);
70 }
71
72 /**
73 * Calculates the <i>weighted</i> value of a charcter in the
74 * code at a specified position.
75 *
76 * @param charValue The numeric value of the character.
77 * @param leftPos The position of the character in the code, counting from left to right
78 * @param rightPos The positionof the character in the code, counting from right to left
79 * @return The weighted value of the character.
80 */
81 protected int weightedValue(int charValue, int leftPos, int rightPos) {
82 return (charValue * POSITION_WEIGHT[leftPos - 1]);
83 }
84
85 /**
86 * Convert a character at a specified position to an integer value.
87 *
88 * @param character The character to convert
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 integer value of the character
92 * @throws CheckDigitException if character is not alphanumeric
93 */
94 protected int toInt(char character, int leftPos, int rightPos)
95 throws CheckDigitException {
96 int charValue = Character.getNumericValue(character);
97 if (charValue < 0 || charValue > 35) {
98 throw new CheckDigitException("Invalid Character[" +
99 leftPos + "] = '" + charValue + "'");
100 }
101 return charValue;
102 }
103
104 }