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