    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
    019import java.util.Arrays;
    020
    021import org.apache.commons.validator.GenericValidator;
    022import org.apache.commons.validator.routines.CodeValidator;
    023
    024/**
    025 * General Modulus 10 Check Digit calculation/validation.
    026 *
    027 * <h2>How it Works</h2>
    028 * <p>
    029 * This implementation calculates/validates the check digit in the following
    030 * way:
    031 * <ul>
    032 * <li>Converting each character to an integer value using
    033 * {@code Character.getNumericValue(char)} - negative integer values from
    034 * that method are invalid.</li>
    035 * <li>Calculating a <em>weighted value</em> by multiplying the character's
    036 * integer value by a <em>weighting factor</em>. The <em>weighting factor</em> is
    037 * selected from the configured {@code positionWeight} array based on its
    038 * position. The {@code postitionWeight} values are used either
    039 * left-to-right (when {@code useRightPos=false}) or right-to-left (when
    040 * {@code useRightPos=true}).</li>
    041 * <li>If {@code sumWeightedDigits=true}, the <em>weighted value</em> is
    042 * re-calculated by summing its digits.</li>
    043 * <li>The <em>weighted values</em> of each character are totalled.</li>
    044 * <li>The total modulo 10 will be zero for a code with a valid Check Digit.</li>
    045 * </ul>
    046 * <h2>Limitations</h2>
    047 * <p>
    048 * This implementation has the following limitations:
    049 * <ul>
    050 * <li>It assumes the last character in the code is the Check Digit and
    051 * validates that it is a numeric character.</li>
    052 * <li>The only limitation on valid characters are those that
    053 * {@code Character.getNumericValue(char)} returns a positive value. If,
    054 * for example, the code should only contain numbers, this implementation does
    055 * not check that.</li>
    056 * <li>There are no checks on code length.</li>
    057 * </ul>
    058 * <p>
    059 * <strong>Note:</strong> This implementation can be combined with the
    060 * {@link CodeValidator} in order to ensure the length and characters are valid.
    061 *
    062 * <h2>Example Usage</h2>
    063 * <p>
    064 * This implementation was added after a number of Modulus 10 routines and these
    065 * are shown re-implemented using this routine below:
    066 *
    067 * <p>
    068 * <strong>ABA Number</strong> Check Digit Routine (equivalent of
    069 * {@link ABANumberCheckDigit}). Weighting factors are {@code [1, 7, 3]}
    070 * applied from right to left.
    071 *
    072 * <pre>
    073 * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 7, 3 }, true);
    074 * </pre>
    075 *
    076 * <p>
    077 * <strong>CUSIP</strong> Check Digit Routine (equivalent of {@link CUSIPCheckDigit}).
    078 * Weighting factors are {@code [1, 2]} applied from right to left and the
    079 * digits of the <em>weighted value</em> are summed.
    080 *
    081 * <pre>
    082 * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true);
    083 * </pre>
    084 *
    085 * <p>
    086 * <strong>EAN-13 / UPC</strong> Check Digit Routine (equivalent of
    087 * {@link EAN13CheckDigit}). Weighting factors are {@code [1, 3]} applied
    088 * from right to left.
    089 *
    090 * <pre>
    091 * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3 }, true);
    092 * </pre>
    093 *
    094 * <p>
    095 * <strong>Luhn</strong> Check Digit Routine (equivalent of {@link LuhnCheckDigit}).
    096 * Weighting factors are {@code [1, 2]} applied from right to left and the
    097 * digits of the <em>weighted value</em> are summed.
    098 *
    099 * <pre>
    100 * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true);
    101 * </pre>
    102 *
    103 * <p>
    104 * <strong>SEDOL</strong> Check Digit Routine (equivalent of {@link SedolCheckDigit}).
    105 * Weighting factors are {@code [1, 3, 1, 7, 3, 9, 1]} applied from left to
    106 * right.
    107 *
    108 * <pre>
    109 * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3, 1, 7, 3, 9, 1 });
    110 * </pre>
    111 *
    112 * @since 1.6
    113 */
    114public final class ModulusTenCheckDigit extends ModulusCheckDigit {
    115
    116    private static final long serialVersionUID = -3752929983453368497L;
    117
    118    /**
    119     * The weighted values to apply based on the character position
    120     */
    121    private final int[] positionWeight;
    122
    123    /**
    124     * {@code true} if use positionWeights from right to left
    125     */
    126    private final boolean useRightPos;
    127
    128    /**
    129     * {@code true} if sum the digits of the weighted value
    130     */
    131    private final boolean sumWeightedDigits;
    132
    133    /**
    134     * Constructs a modulus 10 Check Digit routine with the specified weighting
    135     * from left to right.
    136     *
    137     * @param positionWeight the weighted values to apply based on the
    138     *            character position
    139     */
    140    public ModulusTenCheckDigit(final int[] positionWeight) {
    141        this(positionWeight, false, false);
    142    }
    143
    144    /**
    145     * Constructs a modulus 10 Check Digit routine with the specified weighting,
    146     * indicating whether its from the left or right.
    147     *
    148     * @param positionWeight the weighted values to apply based on the
    149     *            character position
    150     * @param useRightPos {@code true} if use positionWeights from right to
    151     *            left
    152     */
    153    public ModulusTenCheckDigit(final int[] positionWeight, final boolean useRightPos) {
    154        this(positionWeight, useRightPos, false);
    155    }
    156
    157    /**
    158     * Constructs a modulus 10 Check Digit routine with the specified weighting,
    159     * indicating whether its from the left or right and whether the weighted
    160     * digits should be summed.
    161     *
    162     * @param positionWeight the weighted values to apply based on the
    163     *            character position
    164     * @param useRightPos {@code true} if use positionWeights from right to
    165     *            left
    166     * @param sumWeightedDigits {@code true} if sum the digits of the
    167     *            weighted value
    168     */
    169    public ModulusTenCheckDigit(final int[] positionWeight, final boolean useRightPos, final boolean sumWeightedDigits) {
    170        this.positionWeight = Arrays.copyOf(positionWeight, positionWeight.length);
    171        this.useRightPos = useRightPos;
    172        this.sumWeightedDigits = sumWeightedDigits;
    173    }
    174
    175    /**
    176     * Validate a modulus check digit for a code.
    177     * <p>
    178     * Note: assumes last digit is the check digit
    179     *
    180     * @param code The code to validate
    181     * @return {@code true} if the check digit is valid, otherwise
    182     *         {@code false}
    183     */
    184    @Override
    185    public boolean isValid(final String code) {
    186        if (GenericValidator.isBlankOrNull(code) || !Character.isDigit(code.charAt(code.length() - 1))) {
    187            return false;
    188        }
    189        return super.isValid(code);
    190    }
    191
    192    /**
    193     * Convert a character at a specified position to an integer value.
    194     * <p>
    195     * <strong>Note:</strong> this implementation only handlers values that
    196     * Character.getNumericValue(char) returns a non-negative number.
    197     *
    198     * @param character The character to convert
    199     * @param leftPos The position of the character in the code, counting from
    200     *            left to right (for identifying the position in the string)
    201     * @param rightPos The position of the character in the code, counting from
    202     *            right to left (not used here)
    203     * @return The integer value of the character
    204     * @throws CheckDigitException if Character.getNumericValue(char) returns a
    205     *             negative number
    206     */
    207    @Override
    208    protected int toInt(final char character, final int leftPos, final int rightPos) throws CheckDigitException {
    209        final int num = Character.getNumericValue(character);
    210        if (num < 0) {
    211            throw new CheckDigitException("Invalid Character[" + leftPos + "] = '" + character + "'");
    212        }
    213        return num;
    214    }
    215
    216    /**
    217     * Return a string representation of this implementation.
    218     *
    219     * @return a string representation
    220     */
    221    @Override
    222    public String toString() {
    223        return getClass().getSimpleName() + "[positionWeight=" + Arrays.toString(positionWeight) + ", useRightPos="
    224                + useRightPos + ", sumWeightedDigits=" + sumWeightedDigits + "]";
    225    }
    226
    227    /**
    228     * Calculates the <em>weighted</em> value of a character in the code at a
    229     * specified position.
    230     *
    231     * @param charValue The numeric value of the character.
    232     * @param leftPos The position of the character in the code, counting from
    233     *            left to right
    234     * @param rightPos The position of the character in the code, counting from
    235     *            right to left
    236     * @return The weighted value of the character.
    237     */
    238    @Override
    239    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    240        final int pos = useRightPos ? rightPos : leftPos;
    241        final int weight = positionWeight[(pos - 1) % positionWeight.length];
    242        int weightedValue = charValue * weight;
    243        if (sumWeightedDigits) {
    244            weightedValue = sumDigits(weightedValue);
    245        }
    246        return weightedValue;
    247    }
    248
    249}