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 * https://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 * International Standard Serial Number (ISSN) 021 * is an eight-digit serial number used to 022 * uniquely identify a serial publication. 023 * <pre> 024 * The format is: 025 * 026 * ISSN dddd-dddC 027 * where: 028 * d = decimal digit (0-9) 029 * C = checksum (0-9 or X) 030 * 031 * The checksum is formed by adding the first 7 digits multiplied by 032 * the position in the entire number (counting from the right). 033 * For example, abcd-efg would be 8a + 7b + 6c + 5d + 4e +3f +2g. 034 * The check digit is modulus 11, where the value 10 is represented by 'X' 035 * For example: 036 * ISSN 0317-8471 037 * ISSN 1050-124X 038 * </pre> 039 * <p> 040 * <strong>Note:</strong> This class expects the input to be numeric only, 041 * with all formatting removed. 042 * For example: 043 * </p> 044 * <pre> 045 * 03178471 046 * 1050124X 047 * </pre> 048 * 049 * @since 1.5.0 050 */ 051public final class ISSNCheckDigit extends ModulusCheckDigit { 052 053 private static final long serialVersionUID = 1L; 054 055 /** An ISSN is exactly eight characters: seven digits plus the check digit. */ 056 private static final int ISSN_LEN = 8; 057 058 /** 059 * Singleton ISSN Check Digit instance. 060 */ 061 public static final CheckDigit ISSN_CHECK_DIGIT = new ISSNCheckDigit(); 062 063 /** 064 * Creates the instance using a checkdigit modulus of 11. 065 */ 066 public ISSNCheckDigit() { 067 super(MODULUS_11); 068 } 069 070 /** 071 * {@inheritDoc} 072 * 073 * <p> 074 * The {@code 9 - leftPos} weighting gives a ninth character a weight of zero, so an over-length code left the 075 * trailing characters unweighted and still passed the modulus test (for example every {@code 03178471} + digit 076 * validated). The length is checked here so only a genuine eight-character ISSN reaches the check digit test. 077 * </p> 078 */ 079 @Override 080 public boolean isValid(final String code) { 081 return isLength(code, ISSN_LEN) && super.isValid(code); 082 } 083 084 @Override 085 protected String toCheckDigit(final int charValue) throws CheckDigitException { 086 if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber 087 return "X"; 088 } 089 return super.toCheckDigit(charValue); 090 } 091 092 @Override 093 protected int toInt(final char character, final int leftPos, final int rightPos) 094 throws CheckDigitException { 095 if (rightPos == 1 && character == 'X') { 096 return 10; // CHECKSTYLE IGNORE MagicNumber 097 } 098 return super.toInt(character, leftPos, rightPos); 099 } 100 101 @Override 102 protected int weightedValue(final int charValue, final int leftPos, final int rightPos) throws CheckDigitException { 103 return charValue * (9 - leftPos); // CHECKSTYLE IGNORE MagicNumber 104 } 105}