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 */ 017 package org.apache.commons.validator.routines.checkdigit; 018 019 import 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} or the 027 * {@link ISBNCheckDigit#ISBN13} 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 * @version $Revision: 493905 $ $Date: 2007-01-08 03:11:38 +0100 (Mo, 08. Jan 2007) $ 035 * @since Validator 1.4 036 */ 037 public final class ISBNCheckDigit implements CheckDigit, Serializable { 038 039 /** Singleton ISBN-10 Check Digit instance */ 040 public static final CheckDigit ISBN10 = ISBN10CheckDigit.INSTANCE; 041 042 /** Singleton ISBN-13 Check Digit instance */ 043 public static final CheckDigit ISBN13 = EAN13CheckDigit.INSTANCE; 044 045 /** Singleton combined ISBN-10 / ISBN-13 Check Digit instance */ 046 public static final CheckDigit ISBN = new ISBNCheckDigit(); 047 048 /** 049 * Calculate an ISBN-10 or ISBN-13 check digit, depending 050 * on the length of the code. 051 * <p> 052 * If the length of the code is 9, it is treated as an ISBN-10 053 * code or if the length of the code is 12, it is treated as an ISBN-13 054 * code. 055 * 056 * @param code The ISBN code to validate (should have a length of 057 * 9 or 12) 058 * @return The ISBN-10 check digit if the length is 9 or an ISBN-13 059 * check digit if the length is 12. 060 * @throws CheckDigitException if the code is missing, or an invalid 061 * length (i.e. not 9 or 12) or if there is an error calculating the 062 * check digit. 063 */ 064 public String calculate(String code) throws CheckDigitException { 065 if (code == null || code.length() == 0) { 066 throw new CheckDigitException("ISBN Code is missing"); 067 } else if (code.length() == 9) { 068 return ISBN10.calculate(code); 069 } else if (code.length() == 12) { 070 return ISBN13.calculate(code); 071 } else { 072 throw new CheckDigitException("Invalid ISBN Length = " + code.length()); 073 } 074 } 075 076 /** 077 * <p>Validate an ISBN-10 or ISBN-13 check digit, depending 078 * on the length of the code.</p> 079 * <p> 080 * If the length of the code is 10, it is treated as an ISBN-10 081 * code or ff the length of the code is 13, it is treated as an ISBN-13 082 * code. 083 * 084 * @param code The ISBN code to validate (should have a length of 085 * 10 or 13) 086 * @return <code>true</code> if the code has a length of 10 and is 087 * a valid ISBN-10 check digit or the code has a length of 13 and is 088 * a valid ISBN-13 check digit - otherwise <code>false</code>. 089 */ 090 public boolean isValid(String code) { 091 if (code == null) { 092 return false; 093 } else if (code.length() == 10) { 094 return ISBN10.isValid(code); 095 } else if (code.length() == 13) { 096 return ISBN13.isValid(code); 097 } else { 098 return false; 099 } 100 } 101 102 }