| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ISSNCheckDigit |
|
| 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 | * International Standard Serial Number (ISSN) | |
| 21 | * is an eight-digit serial number used to | |
| 22 | * uniquely identify a serial publication. | |
| 23 | * <pre> | |
| 24 | * The format is: | |
| 25 | * | |
| 26 | * ISSN dddd-dddC | |
| 27 | * where: | |
| 28 | * d = decimal digit (0-9) | |
| 29 | * C = checksum (0-9 or X) | |
| 30 | * | |
| 31 | * The checksum is formed by adding the first 7 digits multiplied by | |
| 32 | * the position in the entire number (counting from the right). | |
| 33 | * For example, abcd-efg would be 8a + 7b + 6c + 5d + 4e +3f +2g. | |
| 34 | * The check digit is modulus 11, where the value 10 is represented by 'X' | |
| 35 | * For example: | |
| 36 | * ISSN 0317-8471 | |
| 37 | * ISSN 1050-124X | |
| 38 | * </pre> | |
| 39 | * <p> | |
| 40 | * <b>Note:</b> This class expects the input to be numeric only, | |
| 41 | * with all formatting removed. | |
| 42 | * For example: | |
| 43 | * <pre> | |
| 44 | * 03178471 | |
| 45 | * 1050124X | |
| 46 | * </pre> | |
| 47 | * @since 1.5.0 | |
| 48 | */ | |
| 49 | public final class ISSNCheckDigit extends ModulusCheckDigit { | |
| 50 | ||
| 51 | ||
| 52 | private static final long serialVersionUID = 1L; | |
| 53 | ||
| 54 | /** Singleton ISSN Check Digit instance */ | |
| 55 | 1 | public static final CheckDigit ISSN_CHECK_DIGIT = new ISSNCheckDigit(); |
| 56 | ||
| 57 | /** | |
| 58 | * Creates the instance using a checkdigit modulus of 11 | |
| 59 | */ | |
| 60 | public ISSNCheckDigit() { | |
| 61 | 1 | super(11); // CHECKSTYLE IGNORE MagicNumber |
| 62 | 1 | } |
| 63 | ||
| 64 | @Override | |
| 65 | protected int weightedValue(int charValue, int leftPos, int rightPos) throws CheckDigitException { | |
| 66 | 6826 | return charValue * (9 - leftPos); // CHECKSTYLE IGNORE MagicNumber |
| 67 | } | |
| 68 | ||
| 69 | @Override | |
| 70 | protected String toCheckDigit(int charValue) throws CheckDigitException { | |
| 71 | 13 | if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber |
| 72 | 2 | return "X"; |
| 73 | } | |
| 74 | 11 | return super.toCheckDigit(charValue); |
| 75 | } | |
| 76 | ||
| 77 | @Override | |
| 78 | protected int toInt(char character, int leftPos, int rightPos) | |
| 79 | throws CheckDigitException { | |
| 80 | 7602 | if (rightPos == 1 && character == 'X') { |
| 81 | 19 | return 10; // CHECKSTYLE IGNORE MagicNumber |
| 82 | } | |
| 83 | 7583 | return super.toInt(character, leftPos, rightPos); |
| 84 | } | |
| 85 | } |