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 * 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 * <b>Note:</b> This class expects the input to be numeric only,
041 * with all formatting removed.
042 * For example:
043 * <pre>
044 * 03178471
045 * 1050124X
046 * </pre>
047 * @since 1.5.0
048 */
049public final class ISSNCheckDigit extends ModulusCheckDigit {
050
051
052    private static final long serialVersionUID = 1L;
053
054    /** Singleton ISSN Check Digit instance */
055    public static final CheckDigit ISSN_CHECK_DIGIT = new ISSNCheckDigit();
056
057    /**
058     * Creates the instance using a checkdigit modulus of 11
059     */
060    public ISSNCheckDigit() {
061        super(11); // CHECKSTYLE IGNORE MagicNumber
062    }
063
064    @Override
065    protected int weightedValue(int charValue, int leftPos, int rightPos) throws CheckDigitException {
066        return charValue * (9 - leftPos); // CHECKSTYLE IGNORE MagicNumber
067    }
068
069    @Override
070    protected String toCheckDigit(int charValue) throws CheckDigitException {
071        if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber
072            return "X";
073        }
074        return super.toCheckDigit(charValue);
075    }
076
077    @Override
078    protected int toInt(char character, int leftPos, int rightPos)
079            throws CheckDigitException {
080        if (rightPos == 1 && character == 'X') {
081            return 10; // CHECKSTYLE IGNORE MagicNumber
082        }
083        return super.toInt(character, leftPos, rightPos);
084    }
085}