ABANumberCheckDigit.java

  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.  * Modulus 10 <b>ABA Number</b> (or <b>Routing Transit Number</b> (RTN)) Check Digit
  20.  * calculation/validation.
  21.  *
  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.  *
  28.  * Check digit calculation is based on <i>modulus 10</i> with digits being weighted
  29.  * based on their position (from right to left) as follows:
  30.  *
  31.  * <ul>
  32.  *   <li>Digits 1, 4 and &amp; 7 are weighted 1</li>
  33.  *   <li>Digits 2, 5 and &amp; 8 are weighted 7</li>
  34.  *   <li>Digits 3, 6 and &amp; 9 are weighted 3</li>
  35.  * </ul>
  36.  *
  37.  * <p>
  38.  * For further information see
  39.  *  <a href="https://en.wikipedia.org/wiki/Routing_transit_number">Wikipedia -
  40.  *  Routing transit number</a>.
  41.  * </p>
  42.  *
  43.  * @since 1.4
  44.  */
  45. public final class ABANumberCheckDigit extends ModulusCheckDigit {

  46.     private static final long serialVersionUID = -8255937433810380145L;

  47.     /** Singleton Routing Transit Number Check Digit instance */
  48.     public static final CheckDigit ABAN_CHECK_DIGIT = new ABANumberCheckDigit();

  49.     /** Weighting given to digits depending on their right position */
  50.     private static final int[] POSITION_WEIGHT = {3, 1, 7};

  51.     /**
  52.      * Constructs a modulus 10 Check Digit routine for ABA Numbers.
  53.      */
  54.     public ABANumberCheckDigit() {
  55.     }

  56.     /**
  57.      * Calculates the <i>weighted</i> value of a character in the
  58.      * code at a specified position.
  59.      * <p>
  60.      * ABA Routing numbers are weighted in the following manner:
  61.      * <pre><code>
  62.      *     left position: 1  2  3  4  5  6  7  8  9
  63.      *            weight: 3  7  1  3  7  1  3  7  1
  64.      * </code></pre>
  65.      *
  66.      * @param charValue The numeric value of the character.
  67.      * @param leftPos The position of the character in the code, counting from left to right
  68.      * @param rightPos The positionof the character in the code, counting from right to left
  69.      * @return The weighted value of the character.
  70.      */
  71.     @Override
  72.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
  73.         final int weight = POSITION_WEIGHT[rightPos % 3]; // CHECKSTYLE IGNORE MagicNumber
  74.         return charValue * weight;
  75.     }

  76. }