    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 *      https://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 <strong>ABA Number</strong> (or <strong>Routing Transit Number</strong> (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 <em>modulus 10</em> 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="https://en.wikipedia.org/wiki/Routing_transit_number">Wikipedia -
    041 *  Routing transit number</a>.
    042 * </p>
    043 *
    044 * @since 1.4
    045 */
    046public final class ABANumberCheckDigit extends ModulusCheckDigit {
    047
    048    private static final long serialVersionUID = -8255937433810380145L;
    049
    050    /** Singleton Routing Transit Number Check Digit instance */
    051    public static final CheckDigit ABAN_CHECK_DIGIT = new ABANumberCheckDigit();
    052
    053    /** Weighting given to digits depending on their right position */
    054    private static final int[] POSITION_WEIGHT = {3, 1, 7};
    055
    056    /**
    057     * Constructs a modulus 10 Check Digit routine for ABA Numbers.
    058     */
    059    public ABANumberCheckDigit() {
    060    }
    061
    062    /**
    063     * Calculates the <em>weighted</em> value of a character in the
    064     * code at a specified position.
    065     * <p>
    066     * ABA Routing numbers are weighted in the following manner:
    067     * <pre>{@code
    068     *     left position: 1  2  3  4  5  6  7  8  9
    069     *            weight: 3  7  1  3  7  1  3  7  1
    070     * }</pre>
    071     *
    072     * @param charValue The numeric value of the character.
    073     * @param leftPos The position of the character in the code, counting from left to right
    074     * @param rightPos The position of the character in the code, counting from right to left
    075     * @return The weighted value of the character.
    076     */
    077    @Override
    078    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    079        final int weight = POSITION_WEIGHT[rightPos % 3]; // CHECKSTYLE IGNORE MagicNumber
    080        return charValue * weight;
    081    }
    082
    083}