    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="http://en.wikipedia.org/wiki/SEDOL">Wikipedia - SEDOL</a>
    036 * for more details.
    037 * </p>
    038 *
    039 * @version $Revision$
    040 * @since Validator 1.4
    041 */
    042public final class SedolCheckDigit extends ModulusCheckDigit {
    043
    044    private static final long serialVersionUID = -8976881621148878443L;
    045
    046    private static final int MAX_ALPHANUMERIC_VALUE = 35; // Character.getNumericValue('Z')
    047
    048    /** Singleton SEDOL check digit instance */
    049    public static final CheckDigit SEDOL_CHECK_DIGIT = new SedolCheckDigit();
    050
    051    /** weighting given to digits depending on their right position */
    052    private static final int[] POSITION_WEIGHT = new int[] {1, 3, 1, 7, 3, 9, 1};
    053
    054    /**
    055     * Construct a modulus 11 Check Digit routine for ISBN-10.
    056     */
    057    public SedolCheckDigit() {
    058        super(10); // CHECKSTYLE IGNORE MagicNumber
    059    }
    060
    061    /**
    062     * Calculate the modulus for an SEDOL code.
    063     *
    064     * @param code The code to calculate the modulus for.
    065     * @param includesCheckDigit Whether the code includes the Check Digit or not.
    066     * @return The modulus value
    067     * @throws CheckDigitException if an error occurs calculating the modulus
    068     * for the specified code
    069     */
    070    @Override
    071    protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException {
    072        if (code.length() > POSITION_WEIGHT.length) {
    073            throw new CheckDigitException("Invalid Code Length = " + code.length());
    074        }
    075        return super.calculateModulus(code, includesCheckDigit);
    076    }
    077
    078    /**
    079     * Calculates the <i>weighted</i> value of a charcter in the
    080     * code at a specified position.
    081     *
    082     * @param charValue The numeric value of the character.
    083     * @param leftPos The position of the character in the code, counting from left to right
    084     * @param rightPos The positionof the character in the code, counting from right to left
    085     * @return The weighted value of the character.
    086     */
    087    @Override
    088    protected int weightedValue(int charValue, int leftPos, int rightPos) {
    089        return charValue * POSITION_WEIGHT[leftPos - 1];
    090    }
    091
    092    /**
    093     * Convert a character at a specified position to an integer value.
    094     *
    095     * @param character The character to convert
    096     * @param leftPos The position of the character in the code, counting from left to right
    097     * @param rightPos The positionof the character in the code, counting from right to left
    098     * @return The integer value of the character
    099     * @throws CheckDigitException if character is not alphanumeric
    100     */
    101    @Override
    102    protected int toInt(char character, int leftPos, int rightPos)
    103            throws CheckDigitException {
    104        int charValue = Character.getNumericValue(character);
    105        // the check digit is only allowed to reach 9
    106        final int charMax = rightPos == 1 ? 9 : MAX_ALPHANUMERIC_VALUE; // CHECKSTYLE IGNORE MagicNumber
    107        if (charValue < 0 || charValue > charMax) {
    108            throw new CheckDigitException("Invalid Character[" +
    109                    leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
    110        }
    111        return charValue;
    112    }
    113
    114}