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>Luhn</b> Check Digit calculation/validation.
021 * <p>
022 * Luhn check digits are used, for example, by:
023 * <ul>
024 *    <li><a href="http://en.wikipedia.org/wiki/Credit_card">Credit Card Numbers</a></li>
025 *    <li><a href="http://en.wikipedia.org/wiki/IMEI">IMEI Numbers</a> - International
026 *        Mobile Equipment Identity Numbers</li>
027 * </ul>
028 * Check digit calculation is based on <i>modulus 10</i> with digits in
029 * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i>
030 * position digits being weighted 2 (weighted values greater than 9 have 9 subtracted).
031 * <p>
032 * See <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia</a>
033 * for more details.
034 *
035 * @version $Revision: 1227719 $ $Date: 2012-01-05 18:45:51 +0100 (Do, 05 Jan 2012) $
036 * @since Validator 1.4
037 */
038public final class LuhnCheckDigit extends ModulusCheckDigit {
039
040    private static final long serialVersionUID = -2976900113942875999L;
041
042    /** Singleton Luhn Check Digit instance */
043    public static final CheckDigit LUHN_CHECK_DIGIT = new LuhnCheckDigit();
044
045    /** weighting given to digits depending on their right position */
046    private static final int[] POSITION_WEIGHT = new int[] {2, 1};
047
048    /**
049     * Construct a modulus 10 Luhn Check Digit routine.
050     */
051    public LuhnCheckDigit() {
052        super(10);
053    }
054
055    /**
056     * <p>Calculates the <i>weighted</i> value of a charcter in the
057     * code at a specified position.</p>
058     *
059     * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
060     * with a factor of <b>one</b> and <b>even</b> digits with a factor
061     * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
062     *
063     * @param charValue The numeric value of the character.
064     * @param leftPos The position of the character in the code, counting from left to right
065     * @param rightPos The positionof the character in the code, counting from right to left
066     * @return The weighted value of the character.
067     */
068    protected int weightedValue(int charValue, int leftPos, int rightPos) {
069        int weight = POSITION_WEIGHT[rightPos % 2];
070        int weightedValue = (charValue * weight);
071        return (weightedValue > 9 ? (weightedValue - 9) : weightedValue);
072    }
073}