commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bspeak...@apache.org
Subject svn commit: r591519 - /commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
Date Sat, 03 Nov 2007 00:19:53 GMT
Author: bspeakmon
Date: Fri Nov  2 17:19:53 2007
New Revision: 591519

URL: http://svn.apache.org/viewvc?rev=591519&view=rev
Log:
- VALIDATOR-247: use routines.checkdigit.LuhnCheckDigit

Modified:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java?rev=591519&r1=591518&r2=591519&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
(original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CreditCardValidator.java
Fri Nov  2 17:19:53 2007
@@ -16,12 +16,14 @@
  */
 package org.apache.commons.validator.routines;
 
+import org.apache.commons.validator.routines.checkdigit.CheckDigit;
+import org.apache.commons.validator.routines.checkdigit.LuhnCheckDigit;
+import org.apache.commons.validator.util.Flags;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 
-import org.apache.commons.validator.util.Flags;
-
 /**
  * <p>Perform credit card validations.</p>
  * <p>
@@ -53,7 +55,6 @@
      * v.addAllowedCardType(customType);
      * v.isValid(aCardNumber);
      * </pre>
-     * @since Validator 1.1.2
      */
     public static final int NONE = 0;
 
@@ -83,6 +84,11 @@
     private Collection cardTypes = new ArrayList();
 
     /**
+     * Luhn checkdigit validator for the card numbers.
+     */
+    private static final CheckDigit LUHN_VALIDATOR = LuhnCheckDigit.INSTANCE;
+
+    /**
      * Create a new CreditCardValidator with default options.
      */
     public CreditCardValidator() {
@@ -126,7 +132,7 @@
             return false;
         }
 
-        if (!this.luhnCheck(card)) {
+        if (!LUHN_VALIDATOR.isValid(card)) {
             return false;
         }
         
@@ -149,36 +155,6 @@
      */
     public void addAllowedCardType(CreditCardType type){
         this.cardTypes.add(type);
-    }
-
-    /**
-     * Checks for a valid credit card number.
-     * @param cardNumber Credit Card Number.
-     * @return Whether the card number passes the luhnCheck.
-     */
-    protected boolean luhnCheck(String cardNumber) {
-        // number must be validated as 0..9 numeric first!!
-        int digits = cardNumber.length();
-        int oddOrEven = digits & 1;
-        long sum = 0;
-        for (int count = 0; count < digits; count++) {
-            int digit = 0;
-            try {
-                digit = Integer.parseInt(cardNumber.charAt(count) + "");
-            } catch(NumberFormatException e) {
-                return false;
-            }
-
-            if (((count & 1) ^ oddOrEven) == 0) { // not
-                digit *= 2;
-                if (digit > 9) {
-                    digit -= 9;
-                }
-            }
-            sum += digit;
-        }
-
-        return (sum == 0) ? false : (sum % 10 == 0);
     }
     
     /**



Mime
View raw message