ISSNCheckDigit.java

  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.  * International Standard Serial Number (ISSN)
  20.  * is an eight-digit serial number used to
  21.  * uniquely identify a serial publication.
  22.  * <pre>
  23.  * The format is:
  24.  *
  25.  * ISSN dddd-dddC
  26.  * where:
  27.  * d = decimal digit (0-9)
  28.  * C = checksum (0-9 or X)
  29.  *
  30.  * The checksum is formed by adding the first 7 digits multiplied by
  31.  * the position in the entire number (counting from the right).
  32.  * For example, abcd-efg would be 8a + 7b + 6c + 5d + 4e +3f +2g.
  33.  * The check digit is modulus 11, where the value 10 is represented by 'X'
  34.  * For example:
  35.  * ISSN 0317-8471
  36.  * ISSN 1050-124X
  37.  * </pre>
  38.  * <p>
  39.  * <b>Note:</b> This class expects the input to be numeric only,
  40.  * with all formatting removed.
  41.  * For example:
  42.  * <pre>
  43.  * 03178471
  44.  * 1050124X
  45.  * </pre>
  46.  * @since 1.5.0
  47.  */
  48. public final class ISSNCheckDigit extends ModulusCheckDigit {

  49.     private static final long serialVersionUID = 1L;

  50.     /** Singleton ISSN Check Digit instance */
  51.     public static final CheckDigit ISSN_CHECK_DIGIT = new ISSNCheckDigit();

  52.     /**
  53.      * Creates the instance using a checkdigit modulus of 11.
  54.      */
  55.     public ISSNCheckDigit() {
  56.         super(MODULUS_11);
  57.     }

  58.     @Override
  59.     protected String toCheckDigit(final int charValue) throws CheckDigitException {
  60.         if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber
  61.             return "X";
  62.         }
  63.         return super.toCheckDigit(charValue);
  64.     }

  65.     @Override
  66.     protected int toInt(final char character, final int leftPos, final int rightPos)
  67.             throws CheckDigitException {
  68.         if (rightPos == 1 && character == 'X') {
  69.             return 10; // CHECKSTYLE IGNORE MagicNumber
  70.         }
  71.         return super.toInt(character, leftPos, rightPos);
  72.     }

  73.     @Override
  74.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) throws CheckDigitException {
  75.         return charValue * (9 - leftPos); // CHECKSTYLE IGNORE MagicNumber
  76.     }
  77. }