    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 */
    017
    018package org.apache.commons.validator.routines.checkdigit;
    019
    020/**
    021 * Modulus 11 <strong>ISBN-10</strong> Check Digit calculation/validation.
    022 * <p>
    023 * ISBN-10 Numbers are a numeric code except for the last (check) digit which can have a value of "X".
    024 * </p>
    025 * <p>
    026 * Check digit calculation is based on <em>modulus 11</em> with digits being weighted based by their position, from right to left with the first digit being
    027 * weighted 1, the second 2 and so on. If the check digit is calculated as "10" it is converted to "X".
    028 * </p>
    029 * <p>
    030 * <strong>N.B.</strong> From 1st January 2007 the book industry will start to use a new 13 digit ISBN number (rather than this 10 digit ISBN number) which uses
    031 * the EAN-13 / UPC (see {@link EAN13CheckDigit}) standard.
    032 * </p>
    033 * <p>
    034 * For further information see:
    035 * </p>
    036 * <ul>
    037 * <li><a href="https://en.wikipedia.org/wiki/ISBN">Wikipedia - International Standard Book Number (ISBN)</a>.</li>
    038 * <li><a href="https://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13 Transition details</a>.</li>
    039 * </ul>
    040 *
    041 * @since 1.4
    042 */
    043public final class ISBN10CheckDigit extends ModulusCheckDigit {
    044
    045    private static final long serialVersionUID = 8000855044504864964L;
    046
    047    /**
    048     * Singleton ISBN-10 Check Digit instance.
    049     */
    050    public static final CheckDigit ISBN10_CHECK_DIGIT = new ISBN10CheckDigit();
    051
    052    /** An ISBN-10 is exactly ten characters, the last being the check digit. */
    053    private static final int ISBN10_LEN = 10;
    054
    055    /**
    056     * Constructs a modulus 11 Check Digit routine for ISBN-10.
    057     */
    058    public ISBN10CheckDigit() {
    059        super(MODULUS_11);
    060    }
    061
    062    /**
    063     * {@inheritDoc}
    064     *
    065     * <p>
    066     * The weight is {@code rightPos}, which does not change when a character is prepended, and modulus 11 makes the
    067     * leading position weight a multiple of the modulus, so {@code ModulusCheckDigit} would accept an over-length code
    068     * with any prepended digit (for example {@code 51930110995}). The ten-character length is checked here before the
    069     * check digit test.
    070     * </p>
    071     */
    072    @Override
    073    public boolean isValid(final String code) {
    074        return isLength(code, ISBN10_LEN) && super.isValid(code);
    075    }
    076
    077    /**
    078     * Convert an integer value to a character at a specified position.
    079     * <p>
    080     * Value '10' for position 1 (check digit) converted to 'X'.
    081     * </p>
    082     *
    083     * @param charValue The integer value of the character.
    084     * @return The converted character.
    085     * @throws CheckDigitException if an error occurs.
    086     */
    087    @Override
    088    protected String toCheckDigit(final int charValue) throws CheckDigitException {
    089        if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber
    090            return "X";
    091        }
    092        return super.toCheckDigit(charValue);
    093    }
    094
    095    /**
    096     * Convert a character at a specified position to an integer value.
    097     * <p>
    098     * Character 'X' check digit converted to 10.
    099     * </p>
    100     *
    101     * @param character The character to convert.
    102     * @param leftPos   The position of the character in the code, counting from left to right.
    103     * @param rightPos  The position of the character in the code, counting from right to left.
    104     * @return The integer value of the character.
    105     * @throws CheckDigitException if an error occurs.
    106     */
    107    @Override
    108    protected int toInt(final char character, final int leftPos, final int rightPos) throws CheckDigitException {
    109        if (rightPos == 1 && character == 'X') {
    110            return 10; // CHECKSTYLE IGNORE MagicNumber
    111        }
    112        return super.toInt(character, leftPos, rightPos);
    113    }
    114
    115    /**
    116     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
    117     *
    118     * <p>
    119     * For ISBN-10 (from right to left) digits are weighted by their position.
    120     * </p>
    121     *
    122     * @param charValue The numeric value of the character.
    123     * @param leftPos   The position of the character in the code, counting from left to right.
    124     * @param rightPos  The position of the character in the code, counting from right to left.
    125     * @return The weighted value of the character.
    126     */
    127    @Override
    128    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    129        return charValue * rightPos;
    130    }
    131}