View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.validator.routines.checkdigit;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.validator.GenericValidator;
22  
23  /**
24   * Combined <strong>ISBN-10</strong> / <strong>ISBN-13</strong> Check Digit calculation/validation.
25   * <p>
26   * This implementation validates/calculates ISBN check digits
27   * based on the length of the code passed to it - delegating
28   * either to the {@link ISBNCheckDigit#ISBN10_CHECK_DIGIT} or the
29   * {@link ISBNCheckDigit#ISBN13_CHECK_DIGIT} routines to perform the actual
30   * validation/calculation.
31   * <p>
32   * <strong>N.B.</strong> From 1st January 2007 the book industry will start to use a new 13 digit
33   * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC
34   * standard.
35   *
36   * @since 1.4
37   */
38  public final class ISBNCheckDigit extends AbstractCheckDigit implements Serializable {
39  
40      private static final long serialVersionUID = 1391849166205184558L;
41  
42      /** Singleton ISBN-10 Check Digit instance */
43      public static final CheckDigit ISBN10_CHECK_DIGIT = ISBN10CheckDigit.ISBN10_CHECK_DIGIT;
44  
45      /** Singleton ISBN-13 Check Digit instance */
46      public static final CheckDigit ISBN13_CHECK_DIGIT = EAN13CheckDigit.EAN13_CHECK_DIGIT;
47  
48      /** Singleton combined ISBN-10 / ISBN-13 Check Digit instance */
49      public static final CheckDigit ISBN_CHECK_DIGIT = new ISBNCheckDigit();
50  
51      /**
52       * Constructs a new instance.
53       */
54      public ISBNCheckDigit() {
55          // empty
56      }
57  
58      /**
59       * Calculate an ISBN-10 or ISBN-13 check digit, depending
60       * on the length of the code.
61       * <p>
62       * If the length of the code is 9, it is treated as an ISBN-10
63       * code or if the length of the code is 12, it is treated as an ISBN-13
64       * code.
65       *
66       * @param code The ISBN code to validate (should have a length of
67       * 9 or 12)
68       * @return The ISBN-10 check digit if the length is 9 or an ISBN-13
69       * check digit if the length is 12.
70       * @throws CheckDigitException if the code is missing, or an invalid
71       * length (that is, not 9 or 12) or if there is an error calculating the
72       * check digit.
73       */
74      @Override
75      public String calculate(final String code) throws CheckDigitException {
76          if (GenericValidator.isBlankOrNull(code)) {
77              throw new CheckDigitException("ISBN Code is missing");
78          }
79          if (code.length() == 9) { // CHECKSTYLE IGNORE MagicNumber
80              return ISBN10_CHECK_DIGIT.calculate(code);
81          }
82          if (code.length() == 12) { // CHECKSTYLE IGNORE MagicNumber
83              return ISBN13_CHECK_DIGIT.calculate(code);
84          }
85          throw new CheckDigitException("Invalid ISBN Length = " + code.length());
86      }
87  
88      /**
89       * <p>Validate an ISBN-10 or ISBN-13 check digit, depending
90       * on the length of the code.</p>
91       * <p>
92       * If the length of the code is 10, it is treated as an ISBN-10
93       * code or ff the length of the code is 13, it is treated as an ISBN-13
94       * code.
95       *
96       * @param code The ISBN code to validate (should have a length of
97       * 10 or 13)
98       * @return {@code true} if the code has a length of 10 and is
99       * a valid ISBN-10 check digit or the code has a length of 13 and is
100      * a valid ISBN-13 check digit - otherwise {@code false}.
101      */
102     @Override
103     public boolean isValid(final String code) {
104         if (code == null) {
105             return false;
106         }
107         if (code.length() == 10) { // CHECKSTYLE IGNORE MagicNumber
108             return ISBN10_CHECK_DIGIT.isValid(code);
109         }
110         if (code.length() == 13) { // CHECKSTYLE IGNORE MagicNumber
111             return ISBN13_CHECK_DIGIT.isValid(code);
112         }
113         return false;
114     }
115 
116 }