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   * @since 1.4
45   */
46  public final class ABANumberCheckDigit extends ModulusCheckDigit {
47  
48      private static final long serialVersionUID = -8255937433810380145L;
49  
50      /** Singleton Routing Transit Number Check Digit instance */
51      public static final CheckDigit ABAN_CHECK_DIGIT = new ABANumberCheckDigit();
52  
53      /** weighting given to digits depending on their right position */
54      private static final int[] POSITION_WEIGHT = {3, 1, 7};
55  
56      /**
57       * Constructs a modulus 10 Check Digit routine for ABA Numbers.
58       */
59      public ABANumberCheckDigit() {
60          super();
61      }
62  
63      /**
64       * Calculates the <i>weighted</i> value of a character in the
65       * code at a specified position.
66       * <p>
67       * ABA Routing numbers are weighted in the following manner:
68       * <pre><code>
69       *     left position: 1  2  3  4  5  6  7  8  9
70       *            weight: 3  7  1  3  7  1  3  7  1
71       * </code></pre>
72       *
73       * @param charValue The numeric value of the character.
74       * @param leftPos The position of the character in the code, counting from left to right
75       * @param rightPos The positionof the character in the code, counting from right to left
76       * @return The weighted value of the character.
77       */
78      @Override
79      protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
80          final int weight = POSITION_WEIGHT[rightPos % 3]; // CHECKSTYLE IGNORE MagicNumber
81          return charValue * weight;
82      }
83  
84  }