    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>EAN-13</strong> / <strong>UPC</strong> / <strong>ISBN-13</strong> Check Digit
    021 * calculation/validation.
    022 * <p>
    023 * Check digit calculation is based on <em>modulus 10</em> with digits in
    024 * an <em>odd</em> position (from right to left) being weighted 1 and <em>even</em>
    025 * position digits being weighted 3.
    026 * </p>
    027 * <p>
    028 * For further information see:
    029 * </p>
    030 * <ul>
    031 *   <li>EAN-13 - see
    032 *       <a href="https://en.wikipedia.org/wiki/European_Article_Number">Wikipedia -
    033 *       European Article Number</a>.</li>
    034 *   <li>UPC - see
    035 *       <a href="https://en.wikipedia.org/wiki/Universal_Product_Code">Wikipedia -
    036 *       Universal Product Code</a>.</li>
    037 *   <li>ISBN-13 - see
    038 *       <a href="https://en.wikipedia.org/wiki/ISBN">Wikipedia - International
    039 *       Standard Book Number (ISBN)</a>.</li>
    040 * </ul>
    041 *
    042 * @since 1.4
    043 */
    044public final class EAN13CheckDigit extends ModulusCheckDigit {
    045
    046    private static final long serialVersionUID = 1726347093230424107L;
    047
    048    /**
    049     * Singleton EAN-13 Check Digit instance.
    050     */
    051    public static final CheckDigit EAN13_CHECK_DIGIT = new EAN13CheckDigit();
    052
    053    /** Weighting given to digits depending on their right position */
    054    private static final int[] POSITION_WEIGHT = {3, 1};
    055
    056    /** An EAN-13 (and ISBN-13) code is exactly thirteen digits, the last being the check digit. */
    057    private static final int EAN13_LEN = 13;
    058
    059    /**
    060     * Constructs a modulus 10 Check Digit routine for EAN/UPC.
    061     */
    062    public EAN13CheckDigit() {
    063    }
    064
    065    /**
    066     * {@inheritDoc}
    067     *
    068     * <p>
    069     * The weight is taken from {@code rightPos}, which does not change when a character is prepended, so
    070     * {@code ModulusCheckDigit} would accept an over-length code whose leading digit lands on a no-op weight (for
    071     * example a valid code with a {@code 0} prepended). The thirteen-character length is checked here before the check
    072     * digit test.
    073     * </p>
    074     */
    075    @Override
    076    public boolean isValid(final String code) {
    077        return isLength(code, EAN13_LEN) && super.isValid(code);
    078    }
    079
    080    /**
    081     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
    082     * <p>
    083     * For EAN-13 (from right to left) <strong>odd</strong> digits are weighted with a factor of <strong>one</strong> and <strong>even</strong> digits with a
    084     * factor of <strong>three</strong>.
    085     * </p>
    086     *
    087     * @param charValue The numeric value of the character.
    088     * @param leftPos   The position of the character in the code, counting from left to right.
    089     * @param rightPos  The position of the character in the code, counting from right to left.
    090     * @return The weighted value of the character.
    091     */
    092    @Override
    093    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    094        return charValue * POSITION_WEIGHT[rightPos % 2];
    095    }
    096}