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