    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    /**
    051     * Singleton Routing Transit Number Check Digit instance.
    052     */
    053    public static final CheckDigit ABAN_CHECK_DIGIT = new ABANumberCheckDigit();
    054
    055    /** Weighting given to digits depending on their right position */
    056    private static final int[] POSITION_WEIGHT = {3, 1, 7};
    057
    058    /** An ABA number is exactly nine digits, the last being the check digit. */
    059    private static final int ABAN_LEN = 9;
    060
    061    /**
    062     * Constructs a modulus 10 Check Digit routine for ABA Numbers.
    063     */
    064    public ABANumberCheckDigit() {
    065    }
    066
    067    /**
    068     * {@inheritDoc}
    069     *
    070     * <p>
    071     * The weight is taken from {@code rightPos}, which does not change when a character is prepended, so
    072     * {@code ModulusCheckDigit} would accept an over-length code whose leading digit lands on a no-op weight (for
    073     * example {@code 0123456780}). The nine-character length is checked here before the check digit test.
    074     * </p>
    075     */
    076    @Override
    077    public boolean isValid(final String code) {
    078        return isLength(code, ABAN_LEN) && super.isValid(code);
    079    }
    080
    081    /**
    082     * Calculates the <em>weighted</em> value of a character in the
    083     * code at a specified position.
    084     * <p>
    085     * ABA Routing numbers are weighted in the following manner:
    086     * </p>
    087     * <pre>{@code
    088     *     left position: 1  2  3  4  5  6  7  8  9
    089     *            weight: 3  7  1  3  7  1  3  7  1
    090     * }</pre>
    091     *
    092     * @param charValue The numeric value of the character.
    093     * @param leftPos The position of the character in the code, counting from left to right.
    094     * @param rightPos The position of the character in the code, counting from right to left.
    095     * @return The weighted value of the character.
    096     */
    097    @Override
    098    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    099        final int weight = POSITION_WEIGHT[rightPos % 3]; // CHECKSTYLE IGNORE MagicNumber
    100        return charValue * weight;
    101    }
    102
    103}