    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 */
    017
    018package org.apache.commons.validator.routines.checkdigit;
    019
    020/**
    021 * Modulus 10 <strong>Luhn</strong> Check Digit calculation/validation.
    022 *
    023 * <p>
    024 * Luhn check digits are used, for example, by:
    025 * </p>
    026 * <ul>
    027 * <li><a href="https://en.wikipedia.org/wiki/Credit_card">Credit Card Numbers</a></li>
    028 * <li><a href="https://en.wikipedia.org/wiki/IMEI">IMEI Numbers</a> - International Mobile Equipment Identity Numbers</li>
    029 * </ul>
    030 * <p>
    031 * Check digit calculation is based on <em>modulus 10</em> with digits in an <em>odd</em> position (from right to left) being weighted 1 and <em>even</em>
    032 * position digits being weighted 2 (weighted values greater than 9 have 9 subtracted).
    033 * </p>
    034 * <p>
    035 * See <a href="https://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia</a> for more details.
    036 * </p>
    037 *
    038 * @since 1.4
    039 */
    040public final class LuhnCheckDigit extends ModulusCheckDigit {
    041
    042    private static final long serialVersionUID = -2976900113942875999L;
    043
    044    /**
    045     * Singleton Luhn Check Digit instance.
    046     */
    047    public static final CheckDigit LUHN_CHECK_DIGIT = new LuhnCheckDigit();
    048
    049    /** Weighting given to digits depending on their right position */
    050    private static final int[] POSITION_WEIGHT = { 2, 1 };
    051
    052    /**
    053     * Constructs a modulus 10 Luhn Check Digit routine.
    054     */
    055    public LuhnCheckDigit() {
    056    }
    057
    058    /**
    059     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
    060     * <p>
    061     * For Luhn (from right to left) <strong>odd</strong> digits are weighted with a factor of <strong>one</strong> and <strong>even</strong> digits with a
    062     * factor of <strong>two</strong>. Weighted values &gt; 9, have 9 subtracted
    063     * </p>
    064     *
    065     * @param charValue The numeric value of the character.
    066     * @param leftPos   The position of the character in the code, counting from left to right.
    067     * @param rightPos  The position of the character in the code, counting from right to left.
    068     * @return The weighted value of the character.
    069     */
    070    @Override
    071    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    072        final int weight = POSITION_WEIGHT[rightPos % 2]; // CHECKSTYLE IGNORE MagicNumber
    073        final int weightedValue = charValue * weight;
    074        return weightedValue > 9 ? weightedValue - 9 : weightedValue; // CHECKSTYLE IGNORE MagicNumber
    075    }
    076}