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="http://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 * @version $Revision$ 043 * @since Validator 1.4 044 */ 045public final class ISBN10CheckDigit extends ModulusCheckDigit { 046 047 private static final long serialVersionUID = 8000855044504864964L; 048 049 /** Singleton ISBN-10 Check Digit instance */ 050 public static final CheckDigit ISBN10_CHECK_DIGIT = new ISBN10CheckDigit(); 051 052 /** 053 * Construct a modulus 11 Check Digit routine for ISBN-10. 054 */ 055 public ISBN10CheckDigit() { 056 super(11); // CHECKSTYLE IGNORE MagicNumber 057 } 058 059 /** 060 * Calculates the <i>weighted</i> value of a charcter in the 061 * code at a specified position. 062 * 063 * <p>For ISBN-10 (from right to left) digits are weighted 064 * by their position.</p> 065 * 066 * @param charValue The numeric value of the character. 067 * @param leftPos The position of the character in the code, counting from left to right 068 * @param rightPos The positionof the character in the code, counting from right to left 069 * @return The weighted value of the character. 070 */ 071 @Override 072 protected int weightedValue(int charValue, int leftPos, int rightPos) { 073 return charValue * rightPos; 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(char character, int leftPos, 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 * <p>Convert an integer value to a character at a specified position.</p> 099 * 100 * <p>Value '10' for position 1 (check digit) converted to 'X'.</p> 101 * 102 * @param charValue The integer value of the character. 103 * @return The converted character. 104 * @throws CheckDigitException if an error occurs. 105 */ 106 @Override 107 protected String toCheckDigit(int charValue) 108 throws CheckDigitException { 109 if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber 110 return "X"; 111 } 112 return super.toCheckDigit(charValue); 113 } 114 115}