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>ABA Number</b> (or <b>Routing Transit Number</b> (RTN)) Check Digit
21   * calculation/validation.
22   * <p>
23   * ABA Numbers (or Routing Transit Numbers) are a nine digit numeric code used
24   * to identify American financial institutions for things such as checks or deposits
25   * (ABA stands for the American Bankers Association).
26   * <p>
27   * Check digit calculation is based on <i>modulus 10</i> with digits being weighted
28   * based on their position (from right to left) as follows:
29   * <ul>
30   *     <li>Digits 1, 4 and & 7 are weighted 1
31   *     <li>Digits 2, 5 and & 8 are weighted 7
32   *     <li>Digits 3, 6 and & 9 are weighted 3
33   * </ul>
34   * <p>
35   * For further information see
36   *  <a href="http://en.wikipedia.org/wiki/Routing_transit_number">Wikipedia -
37   *  Routing transit number</a>.
38   *
39   * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Do, 05 Jan 2012) $
40   * @since Validator 1.4
41   */
42  public final class ABANumberCheckDigit extends ModulusCheckDigit {
43  
44      private static final long serialVersionUID = -8255937433810380145L;
45  
46      /** Singleton Routing Transit Number Check Digit instance */
47      public static final CheckDigit ABAN_CHECK_DIGIT = new ABANumberCheckDigit();
48  
49      /** weighting given to digits depending on their right position */
50      private static final int[] POSITION_WEIGHT = new int[] {3, 1, 7};
51  
52      /**
53       * Construct a modulus 10 Check Digit routine for ABA Numbers.
54       */
55      public ABANumberCheckDigit() {
56          super(10);
57      }
58  
59      /**
60       * Calculates the <i>weighted</i> value of a character in the
61       * code at a specified position.
62       * <p>
63       * ABA Routing numbers are weighted in the following manner:
64       * <pre><code>
65       *     left position: 1  2  3  4  5  6  7  8  9
66       *            weight: 3  7  1  3  7  1  3  7  1
67       * </code></pre>
68       *
69       * @param charValue The numeric value of the character.
70       * @param leftPos The position of the character in the code, counting from left to right
71       * @param rightPos The positionof the character in the code, counting from right to left
72       * @return The weighted value of the character.
73       */
74      protected int weightedValue(int charValue, int leftPos, int rightPos) {
75          int weight = POSITION_WEIGHT[rightPos % 3];
76          return (charValue * weight);
77      }
78  
79  }