| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ModulusTenCheckDigit |
|
| 2.2857142857142856;2.286 |
| 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 | import java.util.Arrays; | |
| 20 | ||
| 21 | import org.apache.commons.validator.routines.CodeValidator; | |
| 22 | ||
| 23 | /** | |
| 24 | * General Modulus 10 Check Digit calculation/validation. | |
| 25 | * | |
| 26 | * <h3>How if Works</h3> | |
| 27 | * <p> | |
| 28 | * This implementation calculates/validates the check digit in the following | |
| 29 | * way: | |
| 30 | * <ul> | |
| 31 | * <li>Converting each character to an integer value using | |
| 32 | * <code>Character.getNumericValue(char)</code> - negative integer values from | |
| 33 | * that method are invalid.</li> | |
| 34 | * <li>Calculating a <i>weighted value</i> by multiplying the character's | |
| 35 | * integer value by a <i>weighting factor</i>. The <i>weighting factor</i> is | |
| 36 | * selected from the configured <code>postitionWeight</code> array based on its | |
| 37 | * position. The <code>postitionWeight</code> values are used either | |
| 38 | * left-to-right (when <code>useRightPos=false</code>) or right-to-left (when | |
| 39 | * <code>useRightPos=true</code>).</li> | |
| 40 | * <li>If <code>sumWeightedDigits=true</code>, the <i>weighted value</i> is | |
| 41 | * re-calculated by summing its digits.</li> | |
| 42 | * <li>The <i>weighted values</i> of each character are totalled.</li> | |
| 43 | * <li>The total modulo 10 will be zero for a code with a valid Check Digit.</li> | |
| 44 | * </ul> | |
| 45 | * <h3>Limitations</h3> | |
| 46 | * <p> | |
| 47 | * This implementation has the following limitations: | |
| 48 | * <ul> | |
| 49 | * <li>It assumes the last character in the code is the Check Digit and | |
| 50 | * validates that it is a numeric character.</li> | |
| 51 | * <li>The only limitation on valid characters are those that | |
| 52 | * <code>Character.getNumericValue(char)</code> returns a positive value. If, | |
| 53 | * for example, the code should only contain numbers, this implementation does | |
| 54 | * not check that.</li> | |
| 55 | * <li>There are no checks on code length.</li> | |
| 56 | * </ul> | |
| 57 | * <p> | |
| 58 | * <b>Note:</b> This implementation can be combined with the | |
| 59 | * {@link CodeValidator} in order to ensure the length and characters are valid. | |
| 60 | * | |
| 61 | * <h3>Example Usage</h3> | |
| 62 | * <p> | |
| 63 | * This implementation was added after a number of Modulus 10 routines and these | |
| 64 | * are shown re-implemented using this routine below: | |
| 65 | * | |
| 66 | * <p> | |
| 67 | * <b>ABA Number</b> Check Digit Routine (equivalent of | |
| 68 | * {@link ABANumberCheckDigit}). Weighting factors are <code>[1, 7, 3]</code> | |
| 69 | * applied from right to left. | |
| 70 | * | |
| 71 | * <pre> | |
| 72 | * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 7, 3 }, true); | |
| 73 | * </pre> | |
| 74 | * | |
| 75 | * <p> | |
| 76 | * <b>CUSIP</b> Check Digit Routine (equivalent of {@link CUSIPCheckDigit}). | |
| 77 | * Weighting factors are <code>[1, 2]</code> applied from right to left and the | |
| 78 | * digits of the <i>weighted value</i> are summed. | |
| 79 | * | |
| 80 | * <pre> | |
| 81 | * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true); | |
| 82 | * </pre> | |
| 83 | * | |
| 84 | * <p> | |
| 85 | * <b>EAN-13 / UPC</b> Check Digit Routine (equivalent of | |
| 86 | * {@link EAN13CheckDigit}). Weighting factors are <code>[1, 3]</code> applied | |
| 87 | * from right to left. | |
| 88 | * | |
| 89 | * <pre> | |
| 90 | * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3 }, true); | |
| 91 | * </pre> | |
| 92 | * | |
| 93 | * <p> | |
| 94 | * <b>Luhn</b> Check Digit Routine (equivalent of {@link LuhnCheckDigit}). | |
| 95 | * Weighting factors are <code>[1, 2]</code> applied from right to left and the | |
| 96 | * digits of the <i>weighted value</i> are summed. | |
| 97 | * | |
| 98 | * <pre> | |
| 99 | * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 2 }, true, true); | |
| 100 | * </pre> | |
| 101 | * | |
| 102 | * <p> | |
| 103 | * <b>SEDOL</b> Check Digit Routine (equivalent of {@link SedolCheckDigit}). | |
| 104 | * Weighting factors are <code>[1, 3, 1, 7, 3, 9, 1]</code> applied from left to | |
| 105 | * right. | |
| 106 | * | |
| 107 | * <pre> | |
| 108 | * CheckDigit routine = new ModulusTenCheckDigit(new int[] { 1, 3, 1, 7, 3, 9, 1 }); | |
| 109 | * </pre> | |
| 110 | * | |
| 111 | * @since Validator 1.6 | |
| 112 | * @version $Revision: 1739356 $ | |
| 113 | */ | |
| 114 | public final class ModulusTenCheckDigit extends ModulusCheckDigit { | |
| 115 | ||
| 116 | private static final long serialVersionUID = -3752929983453368497L; | |
| 117 | ||
| 118 | private final int[] postitionWeight; | |
| 119 | private final boolean useRightPos; | |
| 120 | private final boolean sumWeightedDigits; | |
| 121 | ||
| 122 | /** | |
| 123 | * Construct a modulus 10 Check Digit routine with the specified weighting | |
| 124 | * from left to right. | |
| 125 | * | |
| 126 | * @param postitionWeight the weighted values to apply based on the | |
| 127 | * character position | |
| 128 | */ | |
| 129 | public ModulusTenCheckDigit(int[] postitionWeight) { | |
| 130 | 8 | this(postitionWeight, false, false); |
| 131 | 8 | } |
| 132 | ||
| 133 | /** | |
| 134 | * Construct a modulus 10 Check Digit routine with the specified weighting, | |
| 135 | * indicating whether its from the left or right. | |
| 136 | * | |
| 137 | * @param postitionWeight the weighted values to apply based on the | |
| 138 | * character position | |
| 139 | * @param useRightPos <code>true</code> if use positionWeights from right to | |
| 140 | * left | |
| 141 | */ | |
| 142 | public ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos) { | |
| 143 | 14 | this(postitionWeight, useRightPos, false); |
| 144 | 14 | } |
| 145 | ||
| 146 | /** | |
| 147 | * Construct a modulus 10 Check Digit routine with the specified weighting, | |
| 148 | * indicating whether its from the left or right and whether the weighted | |
| 149 | * digits should be summed. | |
| 150 | * | |
| 151 | * @param postitionWeight the weighted values to apply based on the | |
| 152 | * character position | |
| 153 | * @param useRightPos <code>true</code> if use positionWeights from right to | |
| 154 | * left | |
| 155 | * @param sumWeightedDigits <code>true</code> if sum the digits of the | |
| 156 | * weighted value | |
| 157 | */ | |
| 158 | public ModulusTenCheckDigit(int[] postitionWeight, boolean useRightPos, boolean sumWeightedDigits) { | |
| 159 | 38 | super(10); // CHECKSTYLE IGNORE MagicNumber |
| 160 | 38 | this.postitionWeight = Arrays.copyOf(postitionWeight, postitionWeight.length); |
| 161 | 38 | this.useRightPos = useRightPos; |
| 162 | 38 | this.sumWeightedDigits = sumWeightedDigits; |
| 163 | 38 | } |
| 164 | ||
| 165 | /** | |
| 166 | * Validate a modulus check digit for a code. | |
| 167 | * <p> | |
| 168 | * Note: assumes last digit is the check digit | |
| 169 | * | |
| 170 | * @param code The code to validate | |
| 171 | * @return <code>true</code> if the check digit is valid, otherwise | |
| 172 | * <code>false</code> | |
| 173 | */ | |
| 174 | @Override | |
| 175 | public boolean isValid(String code) { | |
| 176 | 2209 | if (code == null || code.length() == 0) { |
| 177 | 10 | return false; |
| 178 | } | |
| 179 | 2199 | if (!Character.isDigit(code.charAt(code.length() - 1))) { |
| 180 | 1896 | return false; |
| 181 | } | |
| 182 | ||
| 183 | 303 | return super.isValid(code); |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Convert a character at a specified position to an integer value. | |
| 188 | * <p> | |
| 189 | * <b>Note:</b> this implementation only handlers values that | |
| 190 | * Character.getNumericValue(char) returns a non-negative number. | |
| 191 | * | |
| 192 | * @param character The character to convert | |
| 193 | * @param leftPos The position of the character in the code, counting from | |
| 194 | * left to right (for identifying the position in the string) | |
| 195 | * @param rightPos The position of the character in the code, counting from | |
| 196 | * right to left (not used here) | |
| 197 | * @return The integer value of the character | |
| 198 | * @throws CheckDigitException if Character.getNumericValue(char) returns a | |
| 199 | * negative number | |
| 200 | */ | |
| 201 | @Override | |
| 202 | protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { | |
| 203 | 3454 | int num = Character.getNumericValue(character); |
| 204 | 3454 | if (num < 0) { |
| 205 | 4 | throw new CheckDigitException("Invalid Character[" + leftPos + "] = '" + character + "'"); |
| 206 | } | |
| 207 | 3450 | return num; |
| 208 | } | |
| 209 | ||
| 210 | /** | |
| 211 | * Calculates the <i>weighted</i> value of a character in the code at a | |
| 212 | * specified position. | |
| 213 | * | |
| 214 | * @param charValue The numeric value of the character. | |
| 215 | * @param leftPos The position of the character in the code, counting from | |
| 216 | * left to right | |
| 217 | * @param rightPos The position of the character in the code, counting from | |
| 218 | * right to left | |
| 219 | * @return The weighted value of the character. | |
| 220 | */ | |
| 221 | @Override | |
| 222 | protected int weightedValue(int charValue, int leftPos, int rightPos) { | |
| 223 | 3450 | int pos = useRightPos ? rightPos : leftPos; |
| 224 | 3450 | int weight = postitionWeight[(pos - 1) % postitionWeight.length]; |
| 225 | 3450 | int weightedValue = charValue * weight; |
| 226 | 3450 | if (sumWeightedDigits) { |
| 227 | 1835 | weightedValue = ModulusCheckDigit.sumDigits(weightedValue); |
| 228 | } | |
| 229 | 3450 | return weightedValue; |
| 230 | } | |
| 231 | ||
| 232 | /** | |
| 233 | * Return a string representation of this implementation. | |
| 234 | * | |
| 235 | * @return a string representation | |
| 236 | */ | |
| 237 | @Override | |
| 238 | public String toString() { | |
| 239 | 0 | return getClass().getSimpleName() + "[postitionWeight=" + Arrays.toString(postitionWeight) + ", useRightPos=" |
| 240 | + useRightPos + ", sumWeightedDigits=" + sumWeightedDigits + "]"; | |
| 241 | } | |
| 242 | ||
| 243 | } |