001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator.routines.checkdigit;
018
019/**
020 * Modulus 10 <b>SEDOL</b> (UK Securities) Check Digit calculation/validation.
021 *
022 * <p>
023 * SEDOL Numbers are 7 character alphanumeric codes used
024 * to identify UK Securities (SEDOL stands for Stock Exchange Daily Official List).
025 * </p>
026 * <p>
027 * Check digit calculation is based on <i>modulus 10</i> with digits being weighted
028 * based on their position, from left to right, as follows:
029 * </p>
030 * <pre><code>
031 *      position:  1  2  3  4  5  6  7
032 *     weighting:  1  3  1  7  3  9  1
033 * </code></pre>
034 * <p>
035 * See <a href="https://en.wikipedia.org/wiki/SEDOL">Wikipedia - SEDOL</a>
036 * for more details.
037 * </p>
038 *
039 * @since 1.4
040 */
041public final class SedolCheckDigit extends ModulusCheckDigit {
042
043    private static final long serialVersionUID = -8976881621148878443L;
044
045    private static final int MAX_ALPHANUMERIC_VALUE = 35; // Character.getNumericValue('Z')
046
047    /** Singleton SEDOL check digit instance */
048    public static final CheckDigit SEDOL_CHECK_DIGIT = new SedolCheckDigit();
049
050    /** Weighting given to digits depending on their right position */
051    private static final int[] POSITION_WEIGHT = {1, 3, 1, 7, 3, 9, 1};
052
053    /**
054     * Constructs a modulus 10 Check Digit routine for ISBN-10.
055     */
056    public SedolCheckDigit() {
057    }
058
059    /**
060     * Calculate the modulus for an SEDOL code.
061     *
062     * @param code The code to calculate the modulus for.
063     * @param includesCheckDigit Whether the code includes the Check Digit or not.
064     * @return The modulus value
065     * @throws CheckDigitException if an error occurs calculating the modulus
066     * for the specified code
067     */
068    @Override
069    protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException {
070        if (code.length() > POSITION_WEIGHT.length) {
071            throw new CheckDigitException("Invalid Code Length = " + code.length());
072        }
073        return super.calculateModulus(code, includesCheckDigit);
074    }
075
076    /**
077     * Convert a character at a specified position to an integer value.
078     *
079     * @param character The character to convert
080     * @param leftPos The position of the character in the code, counting from left to right
081     * @param rightPos The positionof the character in the code, counting from right to left
082     * @return The integer value of the character
083     * @throws CheckDigitException if character is not alphanumeric
084     */
085    @Override
086    protected int toInt(final char character, final int leftPos, final int rightPos) throws CheckDigitException {
087        final int charValue = Character.getNumericValue(character);
088        // the check digit is only allowed to reach 9
089        final int charMax = rightPos == 1 ? 9 : MAX_ALPHANUMERIC_VALUE; // CHECKSTYLE IGNORE MagicNumber
090        if (charValue < 0 || charValue > charMax) {
091            throw new CheckDigitException("Invalid Character[" + leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
092        }
093        return charValue;
094    }
095
096    /**
097     * Calculates the <i>weighted</i> value of a character in the
098     * code at a specified position.
099     *
100     * @param charValue The numeric value of the character.
101     * @param leftPos The position of the character in the code, counting from left to right
102     * @param rightPos The positionof the character in the code, counting from right to left
103     * @return The weighted value of the character.
104     */
105    @Override
106    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
107        return charValue * POSITION_WEIGHT[leftPos - 1];
108    }
109
110}