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 019/** 020 * Modulus 11 <b>ISBN-10</b> Check Digit calculation/validation. 021 * <p> 022 * ISBN-10 Numbers are a numeric code except for the last (check) digit 023 * which can have a value of "X". 024 * <p> 025 * Check digit calculation is based on <i>modulus 11</i> with digits being weighted 026 * based by their position, from right to left with the first digit being weighted 027 * 1, the second 2 and so on. If the check digit is calculated as "10" it is converted 028 * to "X". 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 * (see {@link EAN13CheckDigit}) standard. 033 * <p> 034 * For further information see: 035 * <ul> 036 * <li><a href="https://en.wikipedia.org/wiki/ISBN">Wikipedia - International 037 * Standard Book Number (ISBN)</a>.</li> 038 * <li><a href="http://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13 039 * Transition details</a>.</li> 040 * </ul> 041 * 042 * @since 1.4 043 */ 044public final class ISBN10CheckDigit extends ModulusCheckDigit { 045 046 private static final long serialVersionUID = 8000855044504864964L; 047 048 /** Singleton ISBN-10 Check Digit instance */ 049 public static final CheckDigit ISBN10_CHECK_DIGIT = new ISBN10CheckDigit(); 050 051 /** 052 * Constructs a modulus 11 Check Digit routine for ISBN-10. 053 */ 054 public ISBN10CheckDigit() { 055 super(MODULUS_11); 056 } 057 058 /** 059 * <p>Convert an integer value to a character at a specified position.</p> 060 * 061 * <p>Value '10' for position 1 (check digit) converted to 'X'.</p> 062 * 063 * @param charValue The integer value of the character. 064 * @return The converted character. 065 * @throws CheckDigitException if an error occurs. 066 */ 067 @Override 068 protected String toCheckDigit(final int charValue) 069 throws CheckDigitException { 070 if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber 071 return "X"; 072 } 073 return super.toCheckDigit(charValue); 074 } 075 076 /** 077 * <p>Convert a character at a specified position to an 078 * integer value.</p> 079 * 080 * <p>Character 'X' check digit converted to 10.</p> 081 * 082 * @param character The character to convert. 083 * @param leftPos The position of the character in the code, counting from left to right 084 * @param rightPos The position of the character in the code, counting from right to left 085 * @return The integer value of the character. 086 * @throws CheckDigitException if an error occurs. 087 */ 088 @Override 089 protected int toInt(final char character, final int leftPos, final int rightPos) 090 throws CheckDigitException { 091 if (rightPos == 1 && character == 'X') { 092 return 10; // CHECKSTYLE IGNORE MagicNumber 093 } 094 return super.toInt(character, leftPos, rightPos); 095 } 096 097 /** 098 * Calculates the <i>weighted</i> value of a character in the 099 * code at a specified position. 100 * 101 * <p>For ISBN-10 (from right to left) digits are weighted 102 * by their position.</p> 103 * 104 * @param charValue The numeric value of the character. 105 * @param leftPos The position of the character in the code, counting from left to right 106 * @param rightPos The positionof the character in the code, counting from right to left 107 * @return The weighted value of the character. 108 */ 109 @Override 110 protected int weightedValue(final int charValue, final int leftPos, final int rightPos) { 111 return charValue * rightPos; 112 } 113 114}