Author: niallp Date: Fri Dec 15 00:29:27 2006 New Revision: 487479 URL: http://svn.apache.org/viewvc?view=rev&rev=487479 Log: VALIDATOR-213 Re-factor Check Digits for the following: 1) allow for Check Digits of more than one character 2) pass the left hand and right position in ModulusCheckDigit methods 3) add a getModulus() method to ModulusCheckDigit
Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ISBNValidator.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ISBNValidator.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ISBNValidator.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ISBNValidator.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/ISBNValidator.java Fri Dec 15 00:29:27 2006 @@ -244,7 +244,7 @@ // Calculate the new ISBN-13 code String isbn13 = "978" + input.substring(0, 9); try { - char checkDigit = isbn13Validator.getCheckDigit().calculate(isbn13); + String checkDigit = isbn13Validator.getCheckDigit().calculate(isbn13); isbn13 += checkDigit; return isbn13; } catch (CheckDigitException e) { Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/CheckDigit.java Fri Dec 15 00:29:27 2006 @@ -63,7 +63,7 @@ * @return The calculated Check Digit * @throws CheckDigitException if an error occurs. */ - public char calculate(String code) throws CheckDigitException; + public String calculate(String code) throws CheckDigitException; /** * Validate the check digit for the code. Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigit.java Fri Dec 15 00:29:27 2006 @@ -73,11 +73,12 @@ * of <b>three</b>.</p> * * @param charValue The numeric value of the character. - * @param position The position of a character in the code. + * @param leftPos The position of the character in the code, counting from left to right + * @param rightPos The positionof the character in the code, counting from right to left * @return The weighted value of the character. */ - protected int weightedValue(int charValue, int position) { - boolean oddPosition = (position % 2 == 1); + protected int weightedValue(int charValue, int leftPos, int rightPos) { + boolean oddPosition = (rightPos % 2 == 1); int weight = (oddPosition ? ODD_WEIGHT : EVEN_WEIGHT); return (charValue * weight); } Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java Fri Dec 15 00:29:27 2006 @@ -66,11 +66,12 @@ * by their position.</p> * * @param charValue The numeric value of the character. - * @param position The position of a character in the code. + * @param leftPos The position of the character in the code, counting from left to right + * @param rightPos The positionof the character in the code, counting from right to left * @return The weighted value of the character. */ - protected int weightedValue(int charValue, int position) { - return (charValue * position); + protected int weightedValue(int charValue, int leftPos, int rightPos) { + return (charValue * rightPos); } /** @@ -80,16 +81,17 @@ * <p>Character 'X' check digit converted to 10.</p> * * @param character The character to convert. - * @param position The position of a character in the code. + * @param leftPos The position of the character in the code, counting from left to right + * @param rightPos The positionof the character in the code, counting from right to left * @return The integer value of the character. * @throws CheckDigitException if an error occurs. */ - protected int toInt(char character, int position) + protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { - if (position == 1 && character == 'X') { + if (rightPos == 1 && character == 'X') { return 10; } else { - return super.toInt(character, position); + return super.toInt(character, leftPos, rightPos); } } @@ -99,16 +101,15 @@ * <p>Value '10' for position 1 (check digit) converted to 'X'.</p> * * @param charValue The integer value of the character. - * @param position The position of a character in the code. * @return The converted character. * @throws CheckDigitException if an error occurs. */ - protected char toChar(int charValue, int position) + protected String toCheckDigit(int charValue) throws CheckDigitException { - if (position == 1 && charValue == 10) { - return 'X'; + if (charValue == 10) { + return "X"; } else { - return super.toChar(charValue, position); + return super.toCheckDigit(charValue); } } Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigit.java Fri Dec 15 00:29:27 2006 @@ -57,7 +57,7 @@ * length (i.e. not 9 or 12) or if there is an error calculating the * check digit. */ - public char calculate(String code) throws CheckDigitException { + public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("ISBN Code is missing"); } else if (code.length() == 9) { Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/LuhnCheckDigit.java Fri Dec 15 00:29:27 2006 @@ -67,11 +67,12 @@ * of <b>two</b>. Weighted values > 9, have 9 subtracted</p> * * @param charValue The numeric value of the character. - * @param position The position of a character in the code. + * @param leftPos The position of the character in the code, counting from left to right + * @param rightPos The positionof the character in the code, counting from right to left * @return The weighted value of the character. */ - protected int weightedValue(int charValue, int position) { - boolean oddPosition = (position % 2 == 1); + protected int weightedValue(int charValue, int leftPos, int rightPos) { + boolean oddPosition = (rightPos % 2 == 1); int weight = (oddPosition ? ODD_WEIGHT : EVEN_WEIGHT); int weightedValue = (charValue * weight); return (weightedValue > 9 ? (weightedValue - 9) : weightedValue); Modified: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java (original) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/ModulusCheckDigit.java Fri Dec 15 00:29:27 2006 @@ -45,6 +45,16 @@ } /** + * Return the modulus value this check digit routine is based on. + * + * @param code The code to validate + * @return The modulus value this check digit routine is based on + */ + public int getModulus() { + return modulus; + } + + /** * Validate a modulus check digit for the code. * * @param code The code to validate @@ -71,13 +81,13 @@ * @throws CheckDigitException if an error occurs calculating * the check digit for the specified code */ - public char calculate(String code) throws CheckDigitException { + public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("Code is missing"); } int remainder = mod(code, false); int charValue = (remainder == 0) ? 0 : (modulus - remainder); - return toChar(charValue, 1); + return toCheckDigit(charValue); } /** @@ -89,13 +99,14 @@ * @throws CheckDigitException if an error occurs calculating the modulus * for the specified code */ - private int mod(String code, boolean includesCheckDigit) throws CheckDigitException { + protected int mod(String code, boolean includesCheckDigit) throws CheckDigitException { int total = 0; for (int i = 0; i < code.length(); i++) { - int position = i + (includesCheckDigit ? 1 : 2); - int idx = code.length() - (i + 1); - int charValue = toInt(code.charAt(idx), position); - total += weightedValue(charValue, position); + int lth = code.length() + (includesCheckDigit ? 0 : 1); + int leftPos = i + 1; + int rightPos = lth - i; + int charValue = toInt(code.charAt(i), leftPos, rightPos); + total += weightedValue(charValue, leftPos, rightPos); } if (total == 0) { throw new CheckDigitException("Invalid code, sum is zero"); @@ -114,12 +125,13 @@ * required by overriding this method. * * @param charValue The numeric value of the character - * @param position The position of a character in the code + * @param leftPos The position of the character in the code, counting from left to right + * @param rightPos The positionof the character in the code, counting from right to left * @return The weighted value of the character * @throws CheckDigitException if an error occurs calculating * the weighted value */ - protected abstract int weightedValue(int charValue, int position) + protected abstract int weightedValue(int charValue, int leftPos, int rightPos) throws CheckDigitException; @@ -131,17 +143,18 @@ * character-->integer conversion. * * @param character The character to convert - * @param position The position of a character in the code + * @param leftPos The position of the character in the code, counting from left to right + * @param rightPos The positionof the character in the code, counting from right to left * @return The integer value of the character * @throws CheckDigitException if character is non-numeric */ - protected int toInt(char character, int position) + protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { if (Character.isDigit(character)) { return Character.getNumericValue(character); } else { throw new CheckDigitException("Invalid Character[" + - position + "] = '" + character + "'"); + leftPos + "] = '" + character + "'"); } } @@ -153,18 +166,17 @@ * integer-->character conversion. * * @param charValue The integer value of the character - * @param position The position of a character in the code * @return The converted character * @throws CheckDigitException if integer character value * doesn't represent a numeric character */ - protected char toChar(int charValue, int position) + protected String toCheckDigit(int charValue) throws CheckDigitException { if (charValue >= 0 && charValue <= 9) { - return Character.forDigit(charValue, 10); + return "" + Character.forDigit(charValue, 10); } else { - throw new CheckDigitException("Invalid Value[" + - position + "] = " + charValue); + throw new CheckDigitException("Invalid Check Digit Value =" + + + charValue); } } Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java?view=diff&rev=487479&r1=487478&r2=487479 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java (original) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestBase.java Fri Dec 15 00:29:27 2006 @@ -31,6 +31,9 @@ public class CheckDigitTestBase extends TestCase { /** Check digit routine being tested */ + protected int checkDigitLth = 1; + + /** Check digit routine being tested */ protected CheckDigit routine; /** Array of valid code values */ @@ -102,7 +105,7 @@ // test valid values for (int i = 0; i < valid.length; i++) { String code = removeCheckDigit(valid[i]); - char expected = checkDigit(valid[i]); + String expected = checkDigit(valid[i]); try { assertEquals("valid[" + i +"]: " + valid[i], expected, routine.calculate(code)); } catch (Exception e) { @@ -157,10 +160,10 @@ // create invalid check digit values for (int i = 0; i < codes.length; i++) { String code = removeCheckDigit(codes[i]); - char check = checkDigit(codes[i]); + String check = checkDigit(codes[i]); for (int j = 0; j < 10; j++) { - char curr = Character.forDigit(j, 10); - if (curr != check) { + String curr = "" + Character.forDigit(j, 10); + if (!curr.equals(check)) { list.add(code + curr); } } @@ -176,10 +179,10 @@ * @return The code without the check digit */ protected String removeCheckDigit(String code) { - if (code == null || code.length() <= 1) { + if (code == null || code.length() <= checkDigitLth) { return null; } - return code.substring(0, code.length() -1); + return code.substring(0, code.length() - checkDigitLth); } /** @@ -188,11 +191,12 @@ * @param code The code * @return The check digit */ - protected char checkDigit(String code) { - if (code == null || code.length() <= 1) { - return '?'; + protected String checkDigit(String code) { + if (code == null || code.length() <= checkDigitLth) { + return ""; } - return code.charAt(code.length() -1); + int start = code.length() - checkDigitLth; + return code.substring(start); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]