ModulusTenCheckDigit.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. import java.util.Arrays;

  19. import org.apache.commons.validator.GenericValidator;
  20. import org.apache.commons.validator.routines.CodeValidator;

  21. /**
  22.  * General Modulus 10 Check Digit calculation/validation.
  23.  *
  24.  * <h2>How it Works</h2>
  25.  * <p>
  26.  * This implementation calculates/validates the check digit in the following
  27.  * way:
  28.  * <ul>
  29.  * <li>Converting each character to an integer value using
  30.  * <code>Character.getNumericValue(char)</code> - negative integer values from
  31.  * that method are invalid.</li>
  32.  * <li>Calculating a <i>weighted value</i> by multiplying the character's
  33.  * integer value by a <i>weighting factor</i>. The <i>weighting factor</i> is
  34.  * selected from the configured <code>postitionWeight</code> array based on its
  35.  * position. The <code>postitionWeight</code> values are used either
  36.  * left-to-right (when <code>useRightPos=false</code>) or right-to-left (when
  37.  * <code>useRightPos=true</code>).</li>
  38.  * <li>If <code>sumWeightedDigits=true</code>, the <i>weighted value</i> is
  39.  * re-calculated by summing its digits.</li>
  40.  * <li>The <i>weighted values</i> of each character are totalled.</li>
  41.  * <li>The total modulo 10 will be zero for a code with a valid Check Digit.</li>
  42.  * </ul>
  43.  * <h2>Limitations</h2>
  44.  * <p>
  45.  * This implementation has the following limitations:
  46.  * <ul>
  47.  * <li>It assumes the last character in the code is the Check Digit and
  48.  * validates that it is a numeric character.</li>
  49.  * <li>The only limitation on valid characters are those that
  50.  * <code>Character.getNumericValue(char)</code> returns a positive value. If,
  51.  * for example, the code should only contain numbers, this implementation does
  52.  * not check that.</li>
  53.  * <li>There are no checks on code length.</li>
  54.  * </ul>
  55.  * <p>
  56.  * <b>Note:</b> This implementation can be combined with the
  57.  * {@link CodeValidator} in order to ensure the length and characters are valid.
  58.  *
  59.  * <h2>Example Usage</h2>
  60.  * <p>
  61.  * This implementation was added after a number of Modulus 10 routines and these
  62.  * are shown re-implemented using this routine below:
  63.  *
  64.  * <p>
  65.  * <b>ABA Number</b> Check Digit Routine (equivalent of
  66.  * {@link ABANumberCheckDigit}). Weighting factors are <code>[1, 7, 3]</code>
  67.  * applied from right to left.
  68.  *
  69.  * <pre>
  70.  * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 7, 3 }, true);
  71.  * </pre>
  72.  *
  73.  * <p>
  74.  * <b>CUSIP</b> Check Digit Routine (equivalent of {@link CUSIPCheckDigit}).
  75.  * Weighting factors are <code>[1, 2]</code> applied from right to left and the
  76.  * digits of the <i>weighted value</i> are summed.
  77.  *
  78.  * <pre>
  79.  * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true);
  80.  * </pre>
  81.  *
  82.  * <p>
  83.  * <b>EAN-13 / UPC</b> Check Digit Routine (equivalent of
  84.  * {@link EAN13CheckDigit}). Weighting factors are <code>[1, 3]</code> applied
  85.  * from right to left.
  86.  *
  87.  * <pre>
  88.  * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3 }, true);
  89.  * </pre>
  90.  *
  91.  * <p>
  92.  * <b>Luhn</b> Check Digit Routine (equivalent of {@link LuhnCheckDigit}).
  93.  * Weighting factors are <code>[1, 2]</code> applied from right to left and the
  94.  * digits of the <i>weighted value</i> are summed.
  95.  *
  96.  * <pre>
  97.  * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true);
  98.  * </pre>
  99.  *
  100.  * <p>
  101.  * <b>SEDOL</b> Check Digit Routine (equivalent of {@link SedolCheckDigit}).
  102.  * Weighting factors are <code>[1, 3, 1, 7, 3, 9, 1]</code> applied from left to
  103.  * right.
  104.  *
  105.  * <pre>
  106.  * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3, 1, 7, 3, 9, 1 });
  107.  * </pre>
  108.  *
  109.  * @since 1.6
  110.  */
  111. public final class ModulusTenCheckDigit extends ModulusCheckDigit {

  112.     private static final long serialVersionUID = -3752929983453368497L;

  113.     /**
  114.      * The weighted values to apply based on the character position
  115.      */
  116.     private final int[] postitionWeight;

  117.     /**
  118.      * {@code true} if use positionWeights from right to left
  119.      */
  120.     private final boolean useRightPos;

  121.     /**
  122.      * {@code true} if sum the digits of the weighted value
  123.      */
  124.     private final boolean sumWeightedDigits;

  125.     /**
  126.      * Constructs a modulus 10 Check Digit routine with the specified weighting
  127.      * from left to right.
  128.      *
  129.      * @param postitionWeight the weighted values to apply based on the
  130.      *            character position
  131.      */
  132.     public ModulusTenCheckDigit(final int[] postitionWeight) {
  133.         this(postitionWeight, false, false);
  134.     }

  135.     /**
  136.      * Constructs a modulus 10 Check Digit routine with the specified weighting,
  137.      * indicating whether its from the left or right.
  138.      *
  139.      * @param postitionWeight the weighted values to apply based on the
  140.      *            character position
  141.      * @param useRightPos {@code true} if use positionWeights from right to
  142.      *            left
  143.      */
  144.     public ModulusTenCheckDigit(final int[] postitionWeight, final boolean useRightPos) {
  145.         this(postitionWeight, useRightPos, false);
  146.     }

  147.     /**
  148.      * Constructs a modulus 10 Check Digit routine with the specified weighting,
  149.      * indicating whether its from the left or right and whether the weighted
  150.      * digits should be summed.
  151.      *
  152.      * @param postitionWeight the weighted values to apply based on the
  153.      *            character position
  154.      * @param useRightPos {@code true} if use positionWeights from right to
  155.      *            left
  156.      * @param sumWeightedDigits {@code true} if sum the digits of the
  157.      *            weighted value
  158.      */
  159.     public ModulusTenCheckDigit(final int[] postitionWeight, final boolean useRightPos, final boolean sumWeightedDigits) {
  160.         this.postitionWeight = Arrays.copyOf(postitionWeight, postitionWeight.length);
  161.         this.useRightPos = useRightPos;
  162.         this.sumWeightedDigits = sumWeightedDigits;
  163.     }

  164.     /**
  165.      * Validate a modulus check digit for a code.
  166.      * <p>
  167.      * Note: assumes last digit is the check digit
  168.      *
  169.      * @param code The code to validate
  170.      * @return {@code true} if the check digit is valid, otherwise
  171.      *         {@code false}
  172.      */
  173.     @Override
  174.     public boolean isValid(final String code) {
  175.         if (GenericValidator.isBlankOrNull(code)) {
  176.             return false;
  177.         }
  178.         if (!Character.isDigit(code.charAt(code.length() - 1))) {
  179.             return false;
  180.         }
  181.         return super.isValid(code);
  182.     }

  183.     /**
  184.      * Convert a character at a specified position to an integer value.
  185.      * <p>
  186.      * <b>Note:</b> this implementation only handlers values that
  187.      * Character.getNumericValue(char) returns a non-negative number.
  188.      *
  189.      * @param character The character to convert
  190.      * @param leftPos The position of the character in the code, counting from
  191.      *            left to right (for identifying the position in the string)
  192.      * @param rightPos The position of the character in the code, counting from
  193.      *            right to left (not used here)
  194.      * @return The integer value of the character
  195.      * @throws CheckDigitException if Character.getNumericValue(char) returns a
  196.      *             negative number
  197.      */
  198.     @Override
  199.     protected int toInt(final char character, final int leftPos, final int rightPos) throws CheckDigitException {
  200.         final int num = Character.getNumericValue(character);
  201.         if (num < 0) {
  202.             throw new CheckDigitException("Invalid Character[" + leftPos + "] = '" + character + "'");
  203.         }
  204.         return num;
  205.     }

  206.     /**
  207.      * Return a string representation of this implementation.
  208.      *
  209.      * @return a string representation
  210.      */
  211.     @Override
  212.     public String toString() {
  213.         return getClass().getSimpleName() + "[postitionWeight=" + Arrays.toString(postitionWeight) + ", useRightPos="
  214.                 + useRightPos + ", sumWeightedDigits=" + sumWeightedDigits + "]";
  215.     }

  216.     /**
  217.      * Calculates the <i>weighted</i> value of a character in the code at a
  218.      * specified position.
  219.      *
  220.      * @param charValue The numeric value of the character.
  221.      * @param leftPos The position of the character in the code, counting from
  222.      *            left to right
  223.      * @param rightPos The position of the character in the code, counting from
  224.      *            right to left
  225.      * @return The weighted value of the character.
  226.      */
  227.     @Override
  228.     protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
  229.         final int pos = useRightPos ? rightPos : leftPos;
  230.         final int weight = postitionWeight[(pos - 1) % postitionWeight.length];
  231.         int weightedValue = charValue * weight;
  232.         if (sumWeightedDigits) {
  233.             weightedValue = sumDigits(weightedValue);
  234.         }
  235.         return weightedValue;
  236.     }

  237. }