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