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