Source code
Description: source: package: org.apache.commons.validator.routines.checkdigit, class: ISBNCheckDigit

    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
    019import java.io.Serializable;
    020
    021import org.apache.commons.validator.GenericValidator;
    022
    023/**
    024 * Combined <strong>ISBN-10</strong> / <strong>ISBN-13</strong> Check Digit calculation/validation.
    025 * <p>
    026 * This implementation validates/calculates ISBN check digits
    027 * based on the length of the code passed to it - delegating
    028 * either to the {@link ISBNCheckDigit#ISBN10_CHECK_DIGIT} or the
    029 * {@link ISBNCheckDigit#ISBN13_CHECK_DIGIT} routines to perform the actual
    030 * validation/calculation.
    031 * </p>
    032 * <p>
    033 * <strong>N.B.</strong> From 1st January 2007 the book industry will start to use a new 13 digit
    034 * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC
    035 * standard.
    036 * </p>
    037 *
    038 * @since 1.4
    039 */
    040public final class ISBNCheckDigit extends AbstractCheckDigit implements Serializable {
    041
    042    private static final long serialVersionUID = 1391849166205184558L;
    043
    044    /**
    045     * Singleton ISBN-10 Check Digit instance.
    046     */
    047    public static final CheckDigit ISBN10_CHECK_DIGIT = ISBN10CheckDigit.ISBN10_CHECK_DIGIT;
    048
    049    /**
    050     * Singleton ISBN-13 Check Digit instance.
    051     */
    052    public static final CheckDigit ISBN13_CHECK_DIGIT = EAN13CheckDigit.EAN13_CHECK_DIGIT;
    053
    054    /**
    055     * Singleton combined ISBN-10 / ISBN-13 Check Digit instance.
    056     */
    057    public static final CheckDigit ISBN_CHECK_DIGIT = new ISBNCheckDigit();
    058
    059    /**
    060     * Constructs a new instance.
    061     */
    062    public ISBNCheckDigit() {
    063        // empty
    064    }
    065
    066    /**
    067     * Calculate an ISBN-10 or ISBN-13 check digit, depending on the length of the code.
    068     * <p>
    069     * If the length of the code is 9, it is treated as an ISBN-10 code or if the length of the code is 12, it is treated as an ISBN-13 code.
    070     * </p>
    071     *
    072     * @param code The ISBN code to validate (should have a length of 9 or 12).
    073     * @return The ISBN-10 check digit if the length is 9 or an ISBN-13 check digit if the length is 12.
    074     * @throws CheckDigitException if the code is missing, or an invalid length (that is, not 9 or 12) or if there is an error calculating the check digit.
    075     */
    076    @Override
    077    public String calculate(final String code) throws CheckDigitException {
    078        if (GenericValidator.isBlankOrNull(code)) {
    079            throw new CheckDigitException("ISBN Code is missing");
    080        }
    081        if (code.length() == 9) { // CHECKSTYLE IGNORE MagicNumber
    082            return ISBN10_CHECK_DIGIT.calculate(code);
    083        }
    084        if (code.length() == 12) { // CHECKSTYLE IGNORE MagicNumber
    085            return ISBN13_CHECK_DIGIT.calculate(code);
    086        }
    087        throw new CheckDigitException("Invalid ISBN Length = %,d", code.length());
    088    }
    089
    090    /**
    091     * Validate an ISBN-10 or ISBN-13 check digit, depending on the length of the code.
    092     * <p>
    093     * If the length of the code is 10, it is treated as an ISBN-10 code or ff the length of the code is 13, it is treated as an ISBN-13 code.
    094     * </p>
    095     *
    096     * @param code The ISBN code to validate (should have a length of 10 or 13).
    097     * @return {@code true} if the code has a length of 10 and is a valid ISBN-10 check digit or the code has a length of 13 and is a valid ISBN-13 check digit
    098     *         - otherwise {@code false}.
    099     */
    100    @Override
    101    public boolean isValid(final String code) {
    102        if (code == null) {
    103            return false;
    104        }
    105        if (code.length() == 10) { // CHECKSTYLE IGNORE MagicNumber
    106            return ISBN10_CHECK_DIGIT.isValid(code);
    107        }
    108        if (code.length() == 13) { // CHECKSTYLE IGNORE MagicNumber
    109            return ISBN13_CHECK_DIGIT.isValid(code);
    110        }
    111        return false;
    112    }
    113}