    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.validator.routines.checkdigit;
    018
    019/**
    020 * Modulus 10 <b>ABA Number</b> (or <b>Routing Transit Number</b> (RTN)) Check Digit
    021 * calculation/validation.
    022 *
    023 * <p>
    024 * ABA Numbers (or Routing Transit Numbers) are a nine digit numeric code used
    025 * to identify American financial institutions for things such as checks or deposits
    026 * (ABA stands for the American Bankers Association).
    027 * </p>
    028 *
    029 * Check digit calculation is based on <i>modulus 10</i> with digits being weighted
    030 * based on their position (from right to left) as follows:
    031 *
    032 * <ul>
    033 *   <li>Digits 1, 4 and &amp; 7 are weighted 1</li>
    034 *   <li>Digits 2, 5 and &amp; 8 are weighted 7</li>
    035 *   <li>Digits 3, 6 and &amp; 9 are weighted 3</li>
    036 * </ul>
    037 *
    038 * <p>
    039 * For further information see
    040 *  <a href="http://en.wikipedia.org/wiki/Routing_transit_number">Wikipedia -
    041 *  Routing transit number</a>.
    042 * </p>
    043 *
    044 * @version $Revision$
    045 * @since Validator 1.4
    046 */
    047public final class ABANumberCheckDigit extends ModulusCheckDigit {
    048
    049    private static final long serialVersionUID = -8255937433810380145L;
    050
    051    /** Singleton Routing Transit Number Check Digit instance */
    052    public static final CheckDigit ABAN_CHECK_DIGIT = new ABANumberCheckDigit();
    053
    054    /** weighting given to digits depending on their right position */
    055    private static final int[] POSITION_WEIGHT = new int[] {3, 1, 7};
    056
    057    /**
    058     * Construct a modulus 10 Check Digit routine for ABA Numbers.
    059     */
    060    public ABANumberCheckDigit() {
    061        super(10); // CHECKSTYLE IGNORE MagicNumber
    062    }
    063
    064    /**
    065     * Calculates the <i>weighted</i> value of a character in the
    066     * code at a specified position.
    067     * <p>
    068     * ABA Routing numbers are weighted in the following manner:
    069     * <pre><code>
    070     *     left position: 1  2  3  4  5  6  7  8  9
    071     *            weight: 3  7  1  3  7  1  3  7  1
    072     * </code></pre>
    073     *
    074     * @param charValue The numeric value of the character.
    075     * @param leftPos The position of the character in the code, counting from left to right
    076     * @param rightPos The positionof the character in the code, counting from right to left
    077     * @return The weighted value of the character.
    078     */
    079    @Override
    080    protected int weightedValue(int charValue, int leftPos, int rightPos) {
    081        int weight = POSITION_WEIGHT[rightPos % 3]; // CHECKSTYLE IGNORE MagicNumber
    082        return charValue * weight;
    083    }
    084
    085}