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