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 * https://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 <strong>ABA Number</strong> (or <strong>Routing Transit Number</strong> (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 <em>modulus 10</em> with digits being weighted
30 * based on their position (from right to left) as follows:
31 *
32 * <ul>
33 * <li>Digits 1, 4 and & 7 are weighted 1</li>
34 * <li>Digits 2, 5 and & 8 are weighted 7</li>
35 * <li>Digits 3, 6 and & 9 are weighted 3</li>
36 * </ul>
37 *
38 * <p>
39 * For further information see
40 * <a href="https://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 }
61
62 /**
63 * Calculates the <em>weighted</em> value of a character in the
64 * code at a specified position.
65 * <p>
66 * ABA Routing numbers are weighted in the following manner:
67 * <pre>{@code
68 * left position: 1 2 3 4 5 6 7 8 9
69 * weight: 3 7 1 3 7 1 3 7 1
70 * }</pre>
71 *
72 * @param charValue The numeric value of the character.
73 * @param leftPos The position of the character in the code, counting from left to right
74 * @param rightPos The position of the character in the code, counting from right to left
75 * @return The weighted value of the character.
76 */
77 @Override
78 protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
79 final int weight = POSITION_WEIGHT[rightPos % 3]; // CHECKSTYLE IGNORE MagicNumber
80 return charValue * weight;
81 }
82
83 }