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 * @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        super();
058    }
059
060    /**
061     * Calculate the modulus for an SEDOL code.
062     *
063     * @param code The code to calculate the modulus for.
064     * @param includesCheckDigit Whether the code includes the Check Digit or not.
065     * @return The modulus value
066     * @throws CheckDigitException if an error occurs calculating the modulus
067     * for the specified code
068     */
069    @Override
070    protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException {
071        if (code.length() > POSITION_WEIGHT.length) {
072            throw new CheckDigitException("Invalid Code Length = " + code.length());
073        }
074        return super.calculateModulus(code, includesCheckDigit);
075    }
076
077    /**
078     * Convert a character at a specified position to an integer value.
079     *
080     * @param character The character to convert
081     * @param leftPos The position of the character in the code, counting from left to right
082     * @param rightPos The positionof the character in the code, counting from right to left
083     * @return The integer value of the character
084     * @throws CheckDigitException if character is not alphanumeric
085     */
086    @Override
087    protected int toInt(final char character, final int leftPos, final int rightPos)
088            throws CheckDigitException {
089        final int charValue = Character.getNumericValue(character);
090        // the check digit is only allowed to reach 9
091        final int charMax = rightPos == 1 ? 9 : MAX_ALPHANUMERIC_VALUE; // CHECKSTYLE IGNORE MagicNumber
092        if (charValue < 0 || charValue > charMax) {
093            throw new CheckDigitException("Invalid Character[" +
094                    leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
095        }
096        return charValue;
097    }
098
099    /**
100     * Calculates the <i>weighted</i> value of a character in the
101     * code at a specified position.
102     *
103     * @param charValue The numeric value of the character.
104     * @param leftPos The position of the character in the code, counting from left to right
105     * @param rightPos The positionof the character in the code, counting from right to left
106     * @return The weighted value of the character.
107     */
108    @Override
109    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
110        return charValue * POSITION_WEIGHT[leftPos - 1];
111    }
112
113}