Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LuhnCheckDigit |
|
| 1.5;1.5 |
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 | /** | |
20 | * Modulus 10 <b>Luhn</b> Check Digit calculation/validation. | |
21 | * | |
22 | * Luhn check digits are used, for example, by: | |
23 | * <ul> | |
24 | * <li><a href="http://en.wikipedia.org/wiki/Credit_card">Credit Card Numbers</a></li> | |
25 | * <li><a href="http://en.wikipedia.org/wiki/IMEI">IMEI Numbers</a> - International | |
26 | * Mobile Equipment Identity Numbers</li> | |
27 | * </ul> | |
28 | * Check digit calculation is based on <i>modulus 10</i> with digits in | |
29 | * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i> | |
30 | * position digits being weighted 2 (weighted values greater than 9 have 9 subtracted). | |
31 | * | |
32 | * <p> | |
33 | * See <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia</a> | |
34 | * for more details. | |
35 | * </p> | |
36 | * | |
37 | * @version $Revision: 1739356 $ | |
38 | * @since Validator 1.4 | |
39 | */ | |
40 | public final class LuhnCheckDigit extends ModulusCheckDigit { | |
41 | ||
42 | private static final long serialVersionUID = -2976900113942875999L; | |
43 | ||
44 | /** Singleton Luhn Check Digit instance */ | |
45 | 1 | public static final CheckDigit LUHN_CHECK_DIGIT = new LuhnCheckDigit(); |
46 | ||
47 | /** weighting given to digits depending on their right position */ | |
48 | 1 | private static final int[] POSITION_WEIGHT = new int[] {2, 1}; |
49 | ||
50 | /** | |
51 | * Construct a modulus 10 Luhn Check Digit routine. | |
52 | */ | |
53 | public LuhnCheckDigit() { | |
54 | 1 | super(10); // CHECKSTYLE IGNORE MagicNumber |
55 | 1 | } |
56 | ||
57 | /** | |
58 | * <p>Calculates the <i>weighted</i> value of a charcter in the | |
59 | * code at a specified position.</p> | |
60 | * | |
61 | * <p>For Luhn (from right to left) <b>odd</b> digits are weighted | |
62 | * with a factor of <b>one</b> and <b>even</b> digits with a factor | |
63 | * of <b>two</b>. Weighted values > 9, have 9 subtracted</p> | |
64 | * | |
65 | * @param charValue The numeric value of the character. | |
66 | * @param leftPos The position of the character in the code, counting from left to right | |
67 | * @param rightPos The positionof the character in the code, counting from right to left | |
68 | * @return The weighted value of the character. | |
69 | */ | |
70 | @Override | |
71 | protected int weightedValue(int charValue, int leftPos, int rightPos) { | |
72 | 8666 | int weight = POSITION_WEIGHT[rightPos % 2]; // CHECKSTYLE IGNORE MagicNumber |
73 | 8666 | int weightedValue = charValue * weight; |
74 | 8666 | return weightedValue > 9 ? (weightedValue - 9) : weightedValue; // CHECKSTYLE IGNORE MagicNumber |
75 | } | |
76 | } |