ISINCheckDigit.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.  * Modulus 10 <b>ISIN</b> (International Securities Identifying Number) Check Digit calculation/validation.
  20.  *
  21.  * <p>
  22.  * ISIN Numbers are 12 character alphanumeric codes used
  23.  * to identify Securities.
  24.  * </p>
  25.  *
  26.  * <p>
  27.  * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
  28.  * with every second digit being weighted by 2. Alphabetic characters are
  29.  * converted to numbers by their position in the alphabet starting with A being 10.
  30.  * Weighted numbers greater than ten are treated as two separate numbers.
  31.  * </p>
  32.  *
  33.  * <p>
  34.  * See <a href="https://en.wikipedia.org/wiki/ISIN">Wikipedia - ISIN</a>
  35.  * for more details.
  36.  * </p>
  37.  *
  38.  * @since 1.4
  39.  */
  40. public final class ISINCheckDigit extends ModulusCheckDigit {

  41.     private static final long serialVersionUID = -1239211208101323599L;

  42.     private static final int MAX_ALPHANUMERIC_VALUE = 35; // Character.getNumericValue('Z')

  43.     /** Singleton ISIN Check Digit instance */
  44.     public static final CheckDigit ISIN_CHECK_DIGIT = new ISINCheckDigit();

  45.     /** Weighting given to digits depending on their right position */
  46.     private static final int[] POSITION_WEIGHT = {2, 1};

  47.     /**
  48.      * Constructs an ISIN Identifier Check Digit routine.
  49.      */
  50.     public ISINCheckDigit() {
  51.     }

  52.     /**
  53.      * Calculate the modulus for an ISIN code.
  54.      *
  55.      * @param code The code to calculate the modulus for.
  56.      * @param includesCheckDigit Whether the code includes the Check Digit or not.
  57.      * @return The modulus value
  58.      * @throws CheckDigitException if an error occurs calculating the modulus
  59.      * for the specified code
  60.      */
  61.     @Override
  62.     protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException {
  63.         final StringBuilder transformed = new StringBuilder(code.length() * 2); // CHECKSTYLE IGNORE MagicNumber
  64.         if (includesCheckDigit) {
  65.             final char checkDigit = code.charAt(code.length() - 1); // fetch the last character
  66.             if (!Character.isDigit(checkDigit)) {
  67.                 throw new CheckDigitException("Invalid checkdigit[" + checkDigit + "] in " + code);
  68.             }
  69.         }
  70.         for (int i = 0; i < code.length(); i++) {
  71.             final int charValue = Character.getNumericValue(code.charAt(i));
  72.             if (charValue < 0 || charValue > MAX_ALPHANUMERIC_VALUE) {
  73.                 throw new CheckDigitException("Invalid Character[" + (i + 1) + "] = '" + charValue + "'");
  74.             }
  75.             // this converts alphanumerics to two digits
  76.             // so there is no need to overload toInt()
  77.             transformed.append(charValue);
  78.         }
  79.         return super.calculateModulus(transformed.toString(), includesCheckDigit);
  80.     }

  81.     /**
  82.      * <p>Calculates the <i>weighted</i> value of a character in the
  83.      * code at a specified position.</p>
  84.      *
  85.      * <p>For ISIN (from right to left) <b>odd</b> digits are weighted
  86.      * with a factor of <b>one</b> and <b>even</b> digits with a factor
  87.      * of <b>two</b>. Weighted values are reduced to their digital root</p>
  88.      *
  89.      * @param charValue The numeric value of the character.
  90.      * @param leftPos  The position of the character in the code, counting from left to right
  91.      * @param rightPos The position of the character in the code, counting from right to left
  92.      * @return The weighted value of the character.
  93.      */
  94.     @Override
  95.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
  96.         final int weight = POSITION_WEIGHT[rightPos % 2]; // CHECKSTYLE IGNORE MagicNumber
  97.         final int weightedValue = charValue * weight;
  98.         return sumDigits(weightedValue);
  99.     }
  100. }