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   * Modulus 10 <b>ISIN</b> (International Securities Identifying Number) Check Digit calculation/validation.
21   *
22   * <p>
23   * ISIN Numbers are 12 character alphanumeric codes used
24   * to identify Securities.
25   * </p>
26   *
27   * <p>
28   * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
29   * with every second digit being weighted by 2. Alphabetic characters are
30   * converted to numbers by their position in the alphabet starting with A being 10.
31   * Weighted numbers greater than ten are treated as two separate numbers.
32   * </p>
33   *
34   * <p>
35   * See <a href="http://en.wikipedia.org/wiki/ISIN">Wikipedia - ISIN</a>
36   * for more details.
37   * </p>
38   *
39   * @version $Revision: 1649191 $
40   * @since Validator 1.4
41   */
42  public final class ISINCheckDigit extends ModulusCheckDigit {
43  
44      private static final long serialVersionUID = -1239211208101323599L;
45  
46      /** Singleton ISIN Check Digit instance */
47      public static final CheckDigit ISIN_CHECK_DIGIT = new ISINCheckDigit();
48  
49      /** weighting given to digits depending on their right position */
50      private static final int[] POSITION_WEIGHT = new int[] {2, 1};
51  
52      /**
53       * Construct an ISIN Indetifier Check Digit routine.
54       */
55      public ISINCheckDigit() {
56          super(10);
57      }
58  
59      /**
60       * Calculate the modulus for an ISIN code.
61       *
62       * @param code The code to calculate the modulus for.
63       * @param includesCheckDigit Whether the code includes the Check Digit or not.
64       * @return The modulus value
65       * @throws CheckDigitException if an error occurs calculating the modulus
66       * for the specified code
67       */
68      protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException {
69          StringBuffer transformed = new  StringBuffer(code.length() * 2);
70          if (includesCheckDigit) {
71              char checkDigit = code.charAt(code.length()-1); // fetch the last character
72              if (!Character.isDigit(checkDigit)){
73                  throw new CheckDigitException("Invalid checkdigit["+ checkDigit+ "] in " + code);
74              }
75          }
76          for (int i = 0; i < code.length(); i++) {
77              int charValue = Character.getNumericValue(code.charAt(i));
78              if (charValue < 0 || charValue > 35) {
79                  throw new CheckDigitException("Invalid Character[" +
80                          (i + 1) + "] = '" + charValue + "'");
81              }
82               // this converts alphanumerics to two digits
83               // so there is no need to overload toInt()
84              transformed.append(charValue);
85          }
86          return super.calculateModulus(transformed.toString(), includesCheckDigit);
87      }
88  
89      /**
90       * <p>Calculates the <i>weighted</i> value of a charcter in the
91       * code at a specified position.</p>
92       *
93       * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
94       * with a factor of <b>one</b> and <b>even</b> digits with a factor
95       * of <b>two</b>. Weighted values &gt; 9, have 9 subtracted</p>
96       *
97       * @param charValue The numeric value of the character.
98       * @param leftPos The position of the character in the code, counting from left to right
99       * @param rightPos The positionof the character in the code, counting from right to left
100      * @return The weighted value of the character.
101      */
102     protected int weightedValue(int charValue, int leftPos, int rightPos) {
103         int weight = POSITION_WEIGHT[rightPos % 2];
104         int weightedValue = (charValue * weight);
105         return ModulusCheckDigit.sumDigits(weightedValue);
106     }
107 }