Author: niallp Date: Tue Dec 19 07:10:09 2006 New Revision: 488702 URL: http://svn.apache.org/viewvc?view=rev&rev=488702 Log: Add Verhoeff Check Digit implementation
Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java (with props) jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java (with props) Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java Added: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java?view=auto&rev=488702 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java (added) +++ jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java Tue Dec 19 07:10:09 2006 @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.validator.routines.checkdigit; + +/** + * <b>Verhoeff</b> (Dihedral) Check Digit calculation/validation. + * <p> + * Check digit calculation for numeric codes using a + * <a href="http://en.wikipedia.org/wiki/Dihedral_group">Dihedral Group</a> + * of order 10. + * <p> + * See <a href="http://en.wikipedia.org/wiki/Verhoeff_algorithm">Wikipedia + * - Verhoeff algorithm</a> for more details. + * + * @version $Revision$ $Date$ + * @since Validator 1.4 + */ +public class VerhoeffCheckDigit implements CheckDigit { + + /** Static Verhoeff check digit instance */ + public static final CheckDigit INSTANCE = new VerhoeffCheckDigit(); + + /** D - multiplication table */ + private static final int[][] D_TABLE = new int[][] { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 2, 3, 4, 0, 6, 7, 8, 9, 5}, + {2, 3, 4, 0, 1, 7, 8, 9, 5, 6}, + {3, 4, 0, 1, 2, 8, 9, 5, 6, 7}, + {4, 0, 1, 2, 3, 9, 5, 6, 7, 8}, + {5, 9, 8, 7, 6, 0, 4, 3, 2, 1}, + {6, 5, 9, 8, 7, 1, 0, 4, 3, 2}, + {7, 6, 5, 9, 8, 2, 1, 0, 4, 3}, + {8, 7, 6, 5, 9, 3, 2, 1, 0, 4}, + {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}}; + + /** P - permutation table */ + private static final int[][] P_TABLE = new int[][] { + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + {1, 5, 7, 6, 2, 8, 3, 0, 9, 4}, + {5, 8, 0, 3, 7, 9, 6, 1, 4, 2}, + {8, 9, 1, 6, 0, 4, 3, 5, 2, 7}, + {9, 4, 5, 3, 1, 2, 6, 8, 7, 0}, + {4, 2, 8, 6, 5, 7, 3, 9, 0, 1}, + {2, 7, 9, 3, 8, 0, 6, 4, 1, 5}, + {7, 0, 4, 6, 9, 1, 3, 2, 5, 8}}; + + /** inv: inverse table */ + private static final int[] INV_TABLE = new int[] + {0, 4, 3, 2, 1, 5, 6, 7, 8, 9}; + + + /** + * Validate the Verhoeff <i>Check Digit</i> for a code. + * + * @param code The code to validate + * @return <code>true</code> if the check digit is valid, + * otherwise <code>false</code> + */ + public boolean isValid(String code) { + if (code == null || code.length() == 0) { + return false; + } + try { + return (calculateChecksum(code, true) == 0); + } catch (CheckDigitException e) { + return false; + } + } + + /** + * Calculate a Verhoeff <i>Check Digit</i> for a code. + * + * @param code The code to calculate the Check Digit for + * @return The calculated Check Digit + * @throws CheckDigitException if an error occurs calculating + * the check digit for the specified code + */ + public String calculate(String code) throws CheckDigitException { + if (code == null || code.length() == 0) { + throw new CheckDigitException("Code is missing"); + } + int checksum = calculateChecksum(code, false); + return Integer.toString(INV_TABLE[checksum]); + } + + /** + * Calculate the checksum. + * + * @param code The code to calculate the checksum for. + * @param includesCheckDigit Whether the code includes the Check Digit or not. + * @return The checksum value + * @throws CheckDigitException if the code contains an invalid character (i.e. not numeric) + */ + private int calculateChecksum(String code, boolean includesCheckDigit) throws CheckDigitException { + int checksum = 0; + for (int i = 0; i < code.length(); i++) { + int idx = code.length() - (i + 1); + int num = Character.getNumericValue(code.charAt(idx)); + if (num < 0 || num > 9) { + throw new CheckDigitException("Invalid Character[" + + i + "] = '" + ((int)code.charAt(idx)) + "'"); + } + int pos = includesCheckDigit ? i : i + 1; + checksum = D_TABLE[checksum][P_TABLE[pos % 8][num]]; + } + return checksum; + } + +} Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/validator/trunk/src/share/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigit.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java?view=diff&rev=488702&r1=488701&r2=488702 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java (original) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/CheckDigitTestSuite.java Tue Dec 19 07:10:09 2006 @@ -48,6 +48,7 @@ suite.addTestSuite(ISBN10CheckDigitTest.class); suite.addTestSuite(ISBNCheckDigitTest.class); suite.addTestSuite(LuhnCheckDigitTest.class); + suite.addTestSuite(VerhoeffCheckDigitTest.class); return suite; } Added: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java?view=auto&rev=488702 ============================================================================== --- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java (added) +++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java Tue Dec 19 07:10:09 2006 @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.validator.routines.checkdigit; + +/** + * Verhoeff Check Digit Test. + * + * @version $Revision$ + * @since Validator 1.4 + */ +public class VerhoeffCheckDigitTest extends CheckDigitTestBase { + + /** + * Construct a new test. + * @param name test name + */ + public VerhoeffCheckDigitTest(String name) { + super(name); + } + + /** + * Set up routine & valid codes. + */ + protected void setUp() throws Exception { + super.setUp(); + routine = VerhoeffCheckDigit.INSTANCE; + valid = new String[] { + "15", + "1428570", + "12345678902" + }; + } + + /** + * Test zero sum + */ + public void testZeroSum() { + // ignore, don't run this test + } +} Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/VerhoeffCheckDigitTest.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]