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