You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2006/12/19 16:07:45 UTC

svn commit: r488698 - in /jakarta/commons/proper/validator/trunk/src: share/org/apache/commons/validator/routines/checkdigit/ test/org/apache/commons/validator/routines/checkdigit/

Author: niallp
Date: Tue Dec 19 07:07:44 2006
New Revision: 488698

URL: http://svn.apache.org/viewvc?view=rev&rev=488698
Log:
Various check digit changes - minor reformatting, javadocs and test case improvements

Modified:
    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
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java
    jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java

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=488698&r1=488697&r2=488698
==============================================================================
--- 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 Tue Dec 19 07:07:44 2006
@@ -19,16 +19,12 @@
 import java.io.Serializable;
 
 /**
- * Modulus 10 <b>EAN-13</b>/<b>UPC</b>/<b>ISBN-13</b> Check Digit
+ * Modulus 10 <b>EAN-13</b> / <b>UPC</b> / <b>ISBN-13</b> Check Digit
  * calculation/validation.
  * <p>
- * The calculation based on the following criteria:
- *
- * <ul>
- *   <li>Modulus 10.</li>
- *   <li>Odd digits weighted by one (right to left)</li>
- *   <li>Even digits weighted by three (right to left)</li>
- * </ul>
+ * Check digit calculation is based on <i>modulus 10</i> with digits in
+ * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i>
+ * position digits being weighted 3.
  * <p>
  * For further information see:
  * <ul>
@@ -51,11 +47,8 @@
     /** Static EAN-13 Check Digit instance */
     public static final CheckDigit INSTANCE = new EAN13CheckDigit();
 
-    /** weighting given to the 'odd' digits in EAN-13 check digit calculation */
-    private static final int ODD_WEIGHT  = 1;
-
-    /** weighting given to the 'even' digits in EAN-13 check digit calculation */
-    private static final int EVEN_WEIGHT = 3;
+    /** weighting given to digits depending on their right position */
+    private static final int[] POSITION_WEIGHT = new int[] {3, 1};
 
     /**
      * Construct a modulus 10 Check Digit routine for EAN/UPC.
@@ -78,8 +71,7 @@
      * @return The weighted value of the character.
      */
     protected int weightedValue(int charValue, int leftPos, int rightPos) {
-        boolean oddPosition = (rightPos % 2 == 1);
-        int weight = (oddPosition  ? ODD_WEIGHT : EVEN_WEIGHT);
+        int weight = POSITION_WEIGHT[rightPos % 2];
         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=488698&r1=488697&r2=488698
==============================================================================
--- 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 Tue Dec 19 07:07:44 2006
@@ -21,19 +21,17 @@
 /**
  * Modulus 11 <b>ISBN-10</b> Check Digit calculation/validation.
  * <p>
- * Calculation of the <b>ISBN-10</b> Check Digit is based on the
- * following criteria:
+ * ISBN-10 Numbers are a numeric code except for the last (check) digit
+ * which can have a value of "X".
  * <p>
- * <ul>
- *   <li>Modulus 11.</li>
- *   <li>Digits weight by their position (right to left)</li>
- *   <li>If checkdigit value is 10 --> Character 'X'</li>
- * </ul>
- *
+ * Check digit calculation is based on <i>modulus 11</i> with digits being weighted
+ * based by their position, from right to left  with the first digit being weighted
+ * 1, the second 2 and so on. If the check digit is calculated as "10" it is converted
+ * to "X".
  * <p>
  * <b>N.B.</b> From 1st January 2007 the book industry will start to use a new 13 digit
- * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13
- * (see {@link EAN13CheckDigit}) standard. For more information see:
+ * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC
+ * (see {@link EAN13CheckDigit}) standard.
  * <p>
  * For further information see:
  * <ul>

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=488698&r1=488697&r2=488698
==============================================================================
--- 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 Tue Dec 19 07:07:44 2006
@@ -19,14 +19,18 @@
 import java.io.Serializable;
 
 /**
- * Combined <b>ISBN-10</b>/<b>ISBN-13</b> Check Digit calculation/validation.
+ * Combined <b>ISBN-10</b> / <b>ISBN-13</b> Check Digit calculation/validation.
  * <p>
  * This implementation validates/calculates ISBN check digits
  * based on the length of the code passed to it - delegating
  * either to the {@link ISBNCheckDigit#ISBN10} or the
  * {@link ISBNCheckDigit#ISBN13} routines to perform the actual
  * validation/calculation.
- * 
+ * <p>
+ * <b>N.B.</b> From 1st January 2007 the book industry will start to use a new 13 digit
+ * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC
+ * standard.
+ *
  * @version $Revision$ $Date$
  * @since Validator 1.4
  */

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=488698&r1=488697&r2=488698
==============================================================================
--- 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 Tue Dec 19 07:07:44 2006
@@ -22,20 +22,15 @@
 /**
  * Modulus 10 <b>Luhn</b> Check Digit calculation/validation.
  * <p>
- * Luhn check digits are used, for example, by credit card numbers. See
- * <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia - Luhn
- * algorithm</a> for details.
- * <p>
- * Calculation based on the following criteria:
- * <p>
- *
+ * Luhn check digits are used, for example, by:
  * <ul>
- *   <li>Modulus 10.</li>
- *   <li>Odd digits weighted by one (right to left)</li>
- *   <li>Even digits weighted by two (right to left)</li>
- *   <li>Weighted values > 9 have 9 subtracted.</li>
+ *    <li><a href="http://en.wikipedia.org/wiki/Credit_card">Credit Card Numbers</a></li>
+ *    <li><a href="http://en.wikipedia.org/wiki/IMEI">IMEI Numbers</a> - International
+ *        Mobile Equipment Identity Numbers</li>
  * </ul>
- *
+ * Check digit calculation is based on <i>modulus 10</i> with digits in
+ * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i>
+ * position digits being weighted 2 (weighted values greater than 9 have 9 subtracted).
  * <p>
  * See <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia</a>
  * for more details.
@@ -48,8 +43,8 @@
     /** Static Luhn Check Digit instance */
     public static final CheckDigit INSTANCE = new LuhnCheckDigit();
 
-    private static final int ODD_WEIGHT  = 1;
-    private static final int EVEN_WEIGHT = 2;
+    /** weighting given to digits depending on their right position */
+    private static final int[] POSITION_WEIGHT = new int[] {2, 1};
 
     /**
      * Construct a modulus 10 Luhn Check Digit routine.
@@ -72,8 +67,7 @@
      * @return The weighted value of the character.
      */
     protected int weightedValue(int charValue, int leftPos, int rightPos) {
-        boolean oddPosition = (rightPos % 2 == 1);
-        int weight = (oddPosition  ? ODD_WEIGHT : EVEN_WEIGHT);
+        int weight = POSITION_WEIGHT[rightPos % 2];
         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=488698&r1=488697&r2=488698
==============================================================================
--- 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 Tue Dec 19 07:07:44 2006
@@ -27,7 +27,7 @@
  * will need to implement/override the <code>toInt()</code> and
  * <code>toChar()</code> methods.
  * <p>
- *    
+ *
  * @version $Revision$ $Date$
  * @since Validator 1.4
  */
@@ -47,7 +47,6 @@
     /**
      * 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() {
@@ -66,7 +65,7 @@
             return false;
         }
         try {       
-            int remainder = mod(code, true);
+            int remainder = calculateModulus(code, true);
             return (remainder == 0);
         } catch (CheckDigitException  ex) {
             return false;
@@ -85,8 +84,8 @@
         if (code == null || code.length() == 0) {
             throw new CheckDigitException("Code is missing");
         }
-        int remainder = mod(code, false);
-        int charValue = (remainder == 0) ? 0 : (modulus - remainder);
+        int modulusResult = calculateModulus(code, false);
+        int charValue = (modulus - modulusResult) % modulus;
         return toCheckDigit(charValue);
     }
 
@@ -99,7 +98,7 @@
      * @throws CheckDigitException if an error occurs calculating the modulus
      * for the specified code
      */
-    protected int mod(String code, boolean includesCheckDigit) throws CheckDigitException {
+    protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException {
         int total = 0;
         for (int i = 0; i < code.length(); i++) {
             int lth = code.length() + (includesCheckDigit ? 0 : 1);
@@ -178,6 +177,22 @@
             throw new CheckDigitException("Invalid Check Digit Value =" + 
                     + charValue);
         }
+    }
+
+    /**
+     * Add together the individual digits in a number.
+     *
+     * @param number The number whose digits are to be added
+     * @return The sum of the digits
+     */
+    public static int sumDigits(int number) {
+        int total = 0;
+        int todo = number;
+        while (todo > 0) {
+            total += todo % 10;
+            todo  = todo / 10;
+        }
+        return total;
     }
 
 }

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=488698&r1=488697&r2=488698
==============================================================================
--- 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 Tue Dec 19 07:07:44 2006
@@ -51,7 +51,7 @@
     protected String zeroSum = "0000000000";
 
     /** Prefix for error messages */
-    protected String msgPrefix = "";
+    protected String missingMessage = "Code is missing";
 
     /**
      * Constructor
@@ -111,18 +111,6 @@
             }
             assertFalse("invalid check digit[" + i +"]: " + invalidCheckDigits[i], routine.isValid(invalidCheckDigits[i]));
         }
-
-        // test null
-        assertFalse("Test Null", routine.isValid(null));
-
-        // test zero length
-        assertFalse("Test Zero Length", routine.isValid(""));
-        
-        // test zero sum
-        if (zeroSum != null) {
-            assertFalse("Test Zero Sum", routine.isValid(zeroSum));
-        }
-
     }
 
     /**
@@ -158,22 +146,6 @@
             log.debug("testCalculateInvalid() for " + routine.getClass().getName());
         }
 
-        // test null
-        try {
-            routine.calculate(null);
-            fail("Null - expected exception");
-        } catch (Exception e) {
-            assertEquals("Null Test", msgPrefix +"Code is missing", e.getMessage());
-        }
-
-        // test zero length
-        try {
-            routine.calculate("");
-            fail("Zero Length - expected exception");
-        } catch (Exception e) {
-            assertEquals("Zero Length",  msgPrefix +"Code is missing", e.getMessage());
-        }
-
         // test invalid code values
         for (int i = 0; i < invalid.length; i++) {
             try {
@@ -183,18 +155,51 @@
                 routine.calculate(invalid[i]);
                 fail("Invalid Characters[" + i + "]=" +  invalid[i] + " - expected exception");
             } catch (Exception e) {
-                assertTrue("Invalid Character[" +i +"]", e.getMessage().startsWith("Invalid Character["));
+                assertTrue("Invalid Character[" +i +"]=" +  e.getMessage(), e.getMessage().startsWith("Invalid Character["));
             }
         }
+    }
 
-        // test zero sum
-        if (zeroSum != null) {
-            try {
-                routine.calculate(zeroSum);
-                fail("Zero Sum - expected exception");
-            } catch (Exception e) {
-                assertEquals("Zero Length",  "Invalid code, sum is zero", e.getMessage());
-            }
+    /**
+     * Test missing code
+     */
+    public void testMissingCode() {
+
+        // isValid() null
+        assertFalse("isValid() Null", routine.isValid(null));
+
+        // isValid() zero length
+        assertFalse("isValid() Zero Length", routine.isValid(""));
+
+        // calculate() null
+        try {
+            routine.calculate(null);
+            fail("calculate() Null - expected exception");
+        } catch (Exception e) {
+            assertEquals("calculate() Null", missingMessage, e.getMessage());
+        }
+
+        // calculate() zero length
+        try {
+            routine.calculate("");
+            fail("calculate() Zero Length - expected exception");
+        } catch (Exception e) {
+            assertEquals("calculate() Zero Length",  missingMessage, e.getMessage());
+        }
+    }
+
+    /**
+     * Test zero sum
+     */
+    public void testZeroSum() {
+        
+        assertFalse("isValid() Zero Sum", routine.isValid(zeroSum));
+
+        try {
+            routine.calculate(zeroSum);
+            fail("Zero Sum - expected exception");
+        } catch (Exception e) {
+            assertEquals("isValid() Zero Sum",  "Invalid code, sum is zero", e.getMessage());
         }
 
     }

Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java?view=diff&rev=488698&r1=488697&r2=488698
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java (original)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/EAN13CheckDigitTest.java Tue Dec 19 07:07:44 2006
@@ -39,7 +39,11 @@
     protected void setUp() throws Exception {
         super.setUp();
         routine = EAN13CheckDigit.INSTANCE;
-        valid = new String[] {"9780072129519", "9780764558313"};
+        valid = new String[] {
+                "9780072129519",
+                "9780764558313",
+                "4025515373438",
+                "0095673400332"};
     }
 
 }

Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java?view=diff&rev=488698&r1=488697&r2=488698
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java (original)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigitTest.java Tue Dec 19 07:07:44 2006
@@ -39,7 +39,12 @@
     protected void setUp() throws Exception {
         super.setUp();
         routine = ISBN10CheckDigit.INSTANCE;
-        valid = new String[] {"1930110995", "020163385X"};
+        valid = new String[] {
+                "1930110995",
+                "020163385X",
+                "1932394354",
+                "1590596277"
+                };
     }
 
 }

Modified: jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java?view=diff&rev=488698&r1=488697&r2=488698
==============================================================================
--- jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java (original)
+++ jakarta/commons/proper/validator/trunk/src/test/org/apache/commons/validator/routines/checkdigit/ISBNCheckDigitTest.java Tue Dec 19 07:07:44 2006
@@ -39,8 +39,15 @@
     protected void setUp() throws Exception {
         super.setUp();
         routine = ISBNCheckDigit.ISBN;
-        valid = new String[] {"9780072129519", "9780764558313", "1930110995", "020163385X"};
-        msgPrefix = "ISBN ";
+        valid = new String[] {
+                "9780072129519",
+                "9780764558313",
+                "1930110995",
+                "020163385X",
+                "1590596277",    // ISBN-10 Ubuntu Book
+                "9781590596272"  // ISBN-13 Ubuntu Book
+                };
+        missingMessage = "ISBN Code is missing";
         zeroSum = "000000000000";
     }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org