| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ISBN10CheckDigit |
|
| 2.25;2.25 |
| 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 | /** | |
| 20 | * Modulus 11 <b>ISBN-10</b> Check Digit calculation/validation. | |
| 21 | * <p> | |
| 22 | * ISBN-10 Numbers are a numeric code except for the last (check) digit | |
| 23 | * which can have a value of "X". | |
| 24 | * <p> | |
| 25 | * Check digit calculation is based on <i>modulus 11</i> with digits being weighted | |
| 26 | * based by their position, from right to left with the first digit being weighted | |
| 27 | * 1, the second 2 and so on. If the check digit is calculated as "10" it is converted | |
| 28 | * to "X". | |
| 29 | * <p> | |
| 30 | * <b>N.B.</b> From 1st January 2007 the book industry will start to use a new 13 digit | |
| 31 | * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC | |
| 32 | * (see {@link EAN13CheckDigit}) standard. | |
| 33 | * <p> | |
| 34 | * For further information see: | |
| 35 | * <ul> | |
| 36 | * <li><a href="http://en.wikipedia.org/wiki/ISBN">Wikipedia - International | |
| 37 | * Standard Book Number (ISBN)</a>.</li> | |
| 38 | * <li><a href="http://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13 | |
| 39 | * Transition details</a>.</li> | |
| 40 | * </ul> | |
| 41 | * | |
| 42 | * @version $Revision: 1739356 $ | |
| 43 | * @since Validator 1.4 | |
| 44 | */ | |
| 45 | public final class ISBN10CheckDigit extends ModulusCheckDigit { | |
| 46 | ||
| 47 | private static final long serialVersionUID = 8000855044504864964L; | |
| 48 | ||
| 49 | /** Singleton ISBN-10 Check Digit instance */ | |
| 50 | 1 | public static final CheckDigit ISBN10_CHECK_DIGIT = new ISBN10CheckDigit(); |
| 51 | ||
| 52 | /** | |
| 53 | * Construct a modulus 11 Check Digit routine for ISBN-10. | |
| 54 | */ | |
| 55 | public ISBN10CheckDigit() { | |
| 56 | 1 | super(11); // CHECKSTYLE IGNORE MagicNumber |
| 57 | 1 | } |
| 58 | ||
| 59 | /** | |
| 60 | * Calculates the <i>weighted</i> value of a charcter in the | |
| 61 | * code at a specified position. | |
| 62 | * | |
| 63 | * <p>For ISBN-10 (from right to left) digits are weighted | |
| 64 | * by their position.</p> | |
| 65 | * | |
| 66 | * @param charValue The numeric value of the character. | |
| 67 | * @param leftPos The position of the character in the code, counting from left to right | |
| 68 | * @param rightPos The positionof the character in the code, counting from right to left | |
| 69 | * @return The weighted value of the character. | |
| 70 | */ | |
| 71 | @Override | |
| 72 | protected int weightedValue(int charValue, int leftPos, int rightPos) { | |
| 73 | 5352 | return charValue * rightPos; |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * <p>Convert a character at a specified position to an | |
| 78 | * integer value.</p> | |
| 79 | * | |
| 80 | * <p>Character 'X' check digit converted to 10.</p> | |
| 81 | * | |
| 82 | * @param character The character to convert. | |
| 83 | * @param leftPos The position of the character in the code, counting from left to right | |
| 84 | * @param rightPos The position of the character in the code, counting from right to left | |
| 85 | * @return The integer value of the character. | |
| 86 | * @throws CheckDigitException if an error occurs. | |
| 87 | */ | |
| 88 | @Override | |
| 89 | protected int toInt(char character, int leftPos, int rightPos) | |
| 90 | throws CheckDigitException { | |
| 91 | 5801 | if (rightPos == 1 && character == 'X') { |
| 92 | 24 | return 10; // CHECKSTYLE IGNORE MagicNumber |
| 93 | } | |
| 94 | 5777 | return super.toInt(character, leftPos, rightPos); |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * <p>Convert an integer value to a character at a specified position.</p> | |
| 99 | * | |
| 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 | 8 | if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber |
| 110 | 2 | return "X"; |
| 111 | } | |
| 112 | 6 | return super.toCheckDigit(charValue); |
| 113 | } | |
| 114 | ||
| 115 | } |