EAN13CheckDigit.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.validator.routines.checkdigit;

  18. /**
  19.  * Modulus 10 <b>EAN-13</b> / <b>UPC</b> / <b>ISBN-13</b> Check Digit
  20.  * calculation/validation.
  21.  * <p>
  22.  * Check digit calculation is based on <i>modulus 10</i> with digits in
  23.  * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i>
  24.  * position digits being weighted 3.
  25.  * <p>
  26.  * For further information see:
  27.  * <ul>
  28.  *   <li>EAN-13 - see
  29.  *       <a href="https://en.wikipedia.org/wiki/European_Article_Number">Wikipedia -
  30.  *       European Article Number</a>.</li>
  31.  *   <li>UPC - see
  32.  *       <a href="https://en.wikipedia.org/wiki/Universal_Product_Code">Wikipedia -
  33.  *       Universal Product Code</a>.</li>
  34.  *   <li>ISBN-13 - see
  35.  *       <a href="https://en.wikipedia.org/wiki/ISBN">Wikipedia - International
  36.  *       Standard Book Number (ISBN)</a>.</li>
  37.  * </ul>
  38.  *
  39.  * @since 1.4
  40.  */
  41. public final class EAN13CheckDigit extends ModulusCheckDigit {

  42.     private static final long serialVersionUID = 1726347093230424107L;

  43.     /** Singleton EAN-13 Check Digit instance */
  44.     public static final CheckDigit EAN13_CHECK_DIGIT = new EAN13CheckDigit();

  45.     /** Weighting given to digits depending on their right position */
  46.     private static final int[] POSITION_WEIGHT = {3, 1};

  47.     /**
  48.      * Constructs a modulus 10 Check Digit routine for EAN/UPC.
  49.      */
  50.     public EAN13CheckDigit() {
  51.     }

  52.     /**
  53.      * <p>Calculates the <i>weighted</i> value of a character in the
  54.      * code at a specified position.</p>
  55.      *
  56.      * <p>For EAN-13 (from right to left) <b>odd</b> digits are weighted
  57.      * with a factor of <b>one</b> and <b>even</b> digits with a factor
  58.      * of <b>three</b>.</p>
  59.      *
  60.      * @param charValue The numeric value of the character.
  61.      * @param leftPos The position of the character in the code, counting from left to right
  62.      * @param rightPos The positionof the character in the code, counting from right to left
  63.      * @return The weighted value of the character.
  64.      */
  65.     @Override
  66.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
  67.         return charValue * POSITION_WEIGHT[rightPos % 2];
  68.     }
  69. }