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>EAN-13</b> / <b>UPC</b> / <b>ISBN-13</b> Check Digit
021 * calculation/validation.
022 * <p>
023 * Check digit calculation is based on <i>modulus 10</i> with digits in
024 * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i>
025 * position digits being weighted 3.
026 * <p>
027 * For further information see:
028 * <ul>
029 *   <li>EAN-13 - see
030 *       <a href="http://en.wikipedia.org/wiki/European_Article_Number">Wikipedia -
031 *       European Article Number</a>.</li>
032 *   <li>UPC - see
033 *       <a href="http://en.wikipedia.org/wiki/Universal_Product_Code">Wikipedia -
034 *       Universal Product Code</a>.</li>
035 *   <li>ISBN-13 - see
036 *       <a href="http://en.wikipedia.org/wiki/ISBN">Wikipedia - International
037 *       Standard Book Number (ISBN)</a>.</li>
038 * </ul>
039 *
040 * @version $Revision$
041 * @since Validator 1.4
042 */
043public final class EAN13CheckDigit extends ModulusCheckDigit {
044
045    private static final long serialVersionUID = 1726347093230424107L;
046
047    /** Singleton EAN-13 Check Digit instance */
048    public static final CheckDigit EAN13_CHECK_DIGIT = new EAN13CheckDigit();
049
050    /** weighting given to digits depending on their right position */
051    private static final int[] POSITION_WEIGHT = new int[] {3, 1};
052
053    /**
054     * Construct a modulus 10 Check Digit routine for EAN/UPC.
055     */
056    public EAN13CheckDigit() {
057        super(10);  // CHECKSTYLE IGNORE MagicNumber
058    }
059
060    /**
061     * <p>Calculates the <i>weighted</i> value of a character in the
062     * code at a specified position.</p>
063     *
064     * <p>For EAN-13 (from right to left) <b>odd</b> digits are weighted
065     * with a factor of <b>one</b> and <b>even</b> digits with a factor
066     * of <b>three</b>.</p>
067     *
068     * @param charValue The numeric value of the character.
069     * @param leftPos The position of the character in the code, counting from left to right
070     * @param rightPos The positionof the character in the code, counting from right to left
071     * @return The weighted value of the character.
072     */
073    @Override
074    protected int weightedValue(int charValue, int leftPos, int rightPos) {
075        int weight = POSITION_WEIGHT[rightPos % 2];
076        return charValue * weight;
077    }
078}