View Javadoc
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      private static final long serialVersionUID = 1L;
52  
53      /** Singleton ISSN Check Digit instance */
54      public static final CheckDigit ISSN_CHECK_DIGIT = new ISSNCheckDigit();
55  
56      /**
57       * Creates the instance using a checkdigit modulus of 11.
58       */
59      public ISSNCheckDigit() {
60          super(MODULUS_11);
61      }
62  
63      @Override
64      protected String toCheckDigit(final int charValue) throws CheckDigitException {
65          if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber
66              return "X";
67          }
68          return super.toCheckDigit(charValue);
69      }
70  
71      @Override
72      protected int toInt(final char character, final int leftPos, final int rightPos)
73              throws CheckDigitException {
74          if (rightPos == 1 && character == 'X') {
75              return 10; // CHECKSTYLE IGNORE MagicNumber
76          }
77          return super.toInt(character, leftPos, rightPos);
78      }
79  
80      @Override
81      protected int weightedValue(final int charValue, final int leftPos, final int rightPos) throws CheckDigitException {
82          return charValue * (9 - leftPos); // CHECKSTYLE IGNORE MagicNumber
83      }
84  }