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