ISBN10CheckDigit.java

  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.  *      http://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.  * Modulus 11 <b>ISBN-10</b> Check Digit calculation/validation.
  20.  * <p>
  21.  * ISBN-10 Numbers are a numeric code except for the last (check) digit
  22.  * which can have a value of "X".
  23.  * <p>
  24.  * Check digit calculation is based on <i>modulus 11</i> with digits being weighted
  25.  * based by their position, from right to left  with the first digit being weighted
  26.  * 1, the second 2 and so on. If the check digit is calculated as "10" it is converted
  27.  * to "X".
  28.  * <p>
  29.  * <b>N.B.</b> From 1st January 2007 the book industry will start to use a new 13 digit
  30.  * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC
  31.  * (see {@link EAN13CheckDigit}) standard.
  32.  * <p>
  33.  * For further information see:
  34.  * <ul>
  35.  *   <li><a href="https://en.wikipedia.org/wiki/ISBN">Wikipedia - International
  36.  *       Standard Book Number (ISBN)</a>.</li>
  37.  *   <li><a href="http://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13
  38.  *       Transition details</a>.</li>
  39.  * </ul>
  40.  *
  41.  * @since 1.4
  42.  */
  43. public final class ISBN10CheckDigit extends ModulusCheckDigit {

  44.     private static final long serialVersionUID = 8000855044504864964L;

  45.     /** Singleton ISBN-10 Check Digit instance */
  46.     public static final CheckDigit ISBN10_CHECK_DIGIT = new ISBN10CheckDigit();

  47.     /**
  48.      * Constructs a modulus 11 Check Digit routine for ISBN-10.
  49.      */
  50.     public ISBN10CheckDigit() {
  51.         super(MODULUS_11);
  52.     }

  53.     /**
  54.      * <p>Convert an integer value to a character at a specified position.</p>
  55.      *
  56.      * <p>Value '10' for position 1 (check digit) converted to 'X'.</p>
  57.      *
  58.      * @param charValue The integer value of the character.
  59.      * @return The converted character.
  60.      * @throws CheckDigitException if an error occurs.
  61.      */
  62.     @Override
  63.     protected String toCheckDigit(final int charValue)
  64.             throws CheckDigitException {
  65.         if (charValue == 10) {  // CHECKSTYLE IGNORE MagicNumber
  66.             return "X";
  67.         }
  68.         return super.toCheckDigit(charValue);
  69.     }

  70.     /**
  71.      * <p>Convert a character at a specified position to an
  72.      * integer value.</p>
  73.      *
  74.      * <p>Character 'X' check digit converted to 10.</p>
  75.      *
  76.      * @param character The character to convert.
  77.      * @param leftPos The position of the character in the code, counting from left to right
  78.      * @param rightPos The position of the character in the code, counting from right to left
  79.      * @return The integer value of the character.
  80.      * @throws CheckDigitException if an error occurs.
  81.      */
  82.     @Override
  83.     protected int toInt(final char character, final int leftPos, final int rightPos)
  84.             throws CheckDigitException {
  85.         if (rightPos == 1 && character == 'X') {
  86.             return 10;  // CHECKSTYLE IGNORE MagicNumber
  87.         }
  88.         return super.toInt(character, leftPos, rightPos);
  89.     }

  90.     /**
  91.      * Calculates the <i>weighted</i> value of a character in the
  92.      * code at a specified position.
  93.      *
  94.      * <p>For ISBN-10 (from right to left) digits are weighted
  95.      * by their position.</p>
  96.      *
  97.      * @param charValue The numeric value of the character.
  98.      * @param leftPos The position of the character in the code, counting from left to right
  99.      * @param rightPos The positionof the character in the code, counting from right to left
  100.      * @return The weighted value of the character.
  101.      */
  102.     @Override
  103.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
  104.         return charValue * rightPos;
  105.     }

  106. }