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  
18  package org.apache.commons.validator.routines.checkdigit;
19  
20  /**
21   * Modulus 11 <strong>ISBN-10</strong> Check Digit calculation/validation.
22   * <p>
23   * ISBN-10 Numbers are a numeric code except for the last (check) digit which can have a value of "X".
24   * </p>
25   * <p>
26   * 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
27   * weighted 1, the second 2 and so on. If the check digit is calculated as "10" it is converted to "X".
28   * </p>
29   * <p>
30   * <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
31   * the EAN-13 / UPC (see {@link EAN13CheckDigit}) standard.
32   * </p>
33   * <p>
34   * For further information see:
35   * </p>
36   * <ul>
37   * <li><a href="https://en.wikipedia.org/wiki/ISBN">Wikipedia - International Standard Book Number (ISBN)</a>.</li>
38   * <li><a href="https://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13 Transition details</a>.</li>
39   * </ul>
40   *
41   * @since 1.4
42   */
43  public final class ISBN10CheckDigit extends ModulusCheckDigit {
44  
45      private static final long serialVersionUID = 8000855044504864964L;
46  
47      /**
48       * Singleton ISBN-10 Check Digit instance.
49       */
50      public static final CheckDigit ISBN10_CHECK_DIGIT = new ISBN10CheckDigit();
51  
52      /** An ISBN-10 is exactly ten characters, the last being the check digit. */
53      private static final int ISBN10_LEN = 10;
54  
55      /**
56       * Constructs a modulus 11 Check Digit routine for ISBN-10.
57       */
58      public ISBN10CheckDigit() {
59          super(MODULUS_11);
60      }
61  
62      /**
63       * {@inheritDoc}
64       *
65       * <p>
66       * The weight is {@code rightPos}, which does not change when a character is prepended, and modulus 11 makes the
67       * leading position weight a multiple of the modulus, so {@code ModulusCheckDigit} would accept an over-length code
68       * with any prepended digit (for example {@code 51930110995}). The ten-character length is checked here before the
69       * check digit test.
70       * </p>
71       */
72      @Override
73      public boolean isValid(final String code) {
74          return isLength(code, ISBN10_LEN) && super.isValid(code);
75      }
76  
77      /**
78       * Convert an integer value to a character at a specified position.
79       * <p>
80       * Value '10' for position 1 (check digit) converted to 'X'.
81       * </p>
82       *
83       * @param charValue The integer value of the character.
84       * @return The converted character.
85       * @throws CheckDigitException if an error occurs.
86       */
87      @Override
88      protected String toCheckDigit(final int charValue) throws CheckDigitException {
89          if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber
90              return "X";
91          }
92          return super.toCheckDigit(charValue);
93      }
94  
95      /**
96       * Convert a character at a specified position to an integer value.
97       * <p>
98       * Character 'X' check digit converted to 10.
99       * </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 }