    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 *      https://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 <strong>SEDOL</strong> (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 <em>modulus 10</em> 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 * }</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    /**
    046     * Singleton SEDOL check digit instance.
    047     */
    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 for the specified code.
    066     */
    067    @Override
    068    protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException {
    069        if (code.length() > POSITION_WEIGHT.length) {
    070            throw new CheckDigitException("Invalid Code Length = %d", code.length());
    071        }
    072        return super.calculateModulus(code, includesCheckDigit);
    073    }
    074
    075    private boolean isVowel(final char character) {
    076        return "AEIOU".indexOf(Character.toUpperCase(character)) >= 0;
    077    }
    078
    079    /**
    080     * Convert a character at a specified position to an integer value.
    081     *
    082     * @param character The character to convert.
    083     * @param leftPos   The position of the character in the code, counting from left to right.
    084     * @param rightPos  The position of the character in the code, counting from right to left.
    085     * @return The integer value of the character.
    086     * @throws CheckDigitException if character is not alphanumeric or a vowel.
    087     */
    088    @Override
    089    protected int toInt(final char character, final int leftPos, final int rightPos) throws CheckDigitException {
    090        final int charValue = Character.getNumericValue(character);
    091        // the check digit is only allowed to reach 9
    092        final int charValueMax = rightPos == 1 ? 9 : MAX_ALPHANUMERIC_VALUE; // CHECKSTYLE IGNORE MagicNumber
    093        // The SEDOL alphabet excludes the vowels A, E, I, O and U, and treats Y as a consonant.
    094        if (charValue > charValueMax || !isAsciiAlphaNum(character) || isVowel(character)) {
    095            throw new CheckDigitException("Invalid Character[%d,%d] = '%d' out of range 0 to %d, exclusing vowels.", leftPos, rightPos, charValue, charValueMax);
    096        }
    097        return charValue;
    098    }
    099
    100    /**
    101     * Calculates the <em>weighted</em> value of a character in the 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 position of 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}