commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator CreditCardValidator.java GenericValidator.java
Date Sat, 07 Jun 2003 19:13:22 GMT
dgraham     2003/06/07 12:13:21

  Modified:    validator/src/share/org/apache/commons/validator
                        CreditCardValidator.java GenericValidator.java
  Log:
  Added validation options to CreditCardValidator to allow only
  certain card types to pass validation.  This required
  CreditCardValidator to not be a Singleton.
  
  Currently the options are passed into the constructor (like UrlValidator) but we may
  need to consider passing the options into the isValid() method
  so that one CreditCardValidator instance can be used to
  validate with different options.
  
  This still needs a unit test.
  
  PR# 20557
  
  Revision  Changes    Path
  1.6       +62 -20    jakarta-commons/validator/src/share/org/apache/commons/validator/CreditCardValidator.java
  
  Index: CreditCardValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/CreditCardValidator.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- CreditCardValidator.java	7 Jun 2003 18:31:18 -0000	1.5
  +++ CreditCardValidator.java	7 Jun 2003 19:13:21 -0000	1.6
  @@ -61,13 +61,18 @@
   
   package org.apache.commons.validator;
   
  +import org.apache.commons.validator.util.Flags;
  +
   /**
    * <p>Perform credit card validations.</p>
    * <p>
  - * This class is a Singleton; you can retrieve the instance via the getInstance() method.
  + * By default, all supported card types are allowed.  You can specify which cards 
  + * should pass validation by configuring the validation options.  For example,<br/>
  + * <code>CreditCardValidator ccv = new CreditCardValidator(CreditCardValidator.AMEX
+ CreditCardValidator.VISA);</code>
  + * configures the validator to only pass American Express and Visa cards. 
    * </p>
  - * Reference Sean M. Burke's script at
  - * http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
  + * Reference Sean M. Burke's 
  + * <a href="http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl">script</a>.
    *
    * @author David Winterfeldt
    * @author James Turner
  @@ -83,23 +88,52 @@
   	private static final String DISCOVER_PREFIX = "6011";
   
   	/**
  -	 * Singleton instance of this class.
  +	 * Option specifying that American Express cards are allowed.
  +	 */
  +	public static final int AMEX = 1;
  +
  +	/**
  +	 * Option specifying that Visa cards are allowed.
  +	 */
  +	public static final int VISA = 2;
  +
  +	/**
  +	 * Option specifying that Mastercard cards are allowed.
  +	 */
  +	public static final int MASTERCARD = 4;
  +
  +	/**
  +	 * Option specifying that Discover cards are allowed.
  +	 */
  +	public static final int DISCOVER = 8;
  +
  +	/**
  +	 * The default validation options allow all supported card types.
  +	 */
  +	private static final Flags defaultOptions =
  +		new Flags(AMEX + VISA + MASTERCARD + DISCOVER);
  +
  +	/**
  +	 * The current set of validation options.
   	 */
  -	private static final CreditCardValidator instance =
  -		new CreditCardValidator();
  +	private Flags options = null;
   
   	/**
  -	 * Returns the Singleton instance of this validator.
  +	 * Create a new CreditCardValidator with default options.
   	 */
  -	public static CreditCardValidator getInstance() {
  -		return instance;
  +	public CreditCardValidator() {
  +		super();
  +		this.options = defaultOptions;
   	}
   
   	/**
  -	 * Protected constructor for subclasses to use.
  +	 * Create a new CreditCardValidator with the specified options.  Pass in 
  +	 * CreditCardValidator.VISA + CreditCardValidator.AMEX to specify that those are the
  +	 * only valid card types. 
   	 */
  -	protected CreditCardValidator() {
  +	public CreditCardValidator(int options) {
   		super();
  +		this.options = new Flags(options);
   	}
   
   	/**
  @@ -115,12 +149,20 @@
   			return false;
   		}
   
  -		if (this.isVisa(card)
  -			|| this.isAmex(card)
  -			|| this.isMastercard(card)
  -			|| this.isDiscover(card)) {
  +		if (this.isVisa(card)) {
  +			return this.options.isOn(VISA);
  +		}
  +
  +		if (this.isAmex(card)) {
  +			return this.options.isOn(AMEX);
  +		}
  +
  +		if (this.isMastercard(card)) {
  +			return this.options.isOn(MASTERCARD);
  +		}
   
  -			return true;
  +		if (this.isDiscover(card)) {
  +			return this.options.isOn(DISCOVER);
   		}
   
   		return false;
  
  
  
  1.24      +17 -17    jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java
  
  Index: GenericValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/GenericValidator.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- GenericValidator.java	2 May 2003 23:39:31 -0000	1.23
  +++ GenericValidator.java	7 Jun 2003 19:13:21 -0000	1.24
  @@ -85,9 +85,15 @@
      public final static String REGEXP_DELIM = ValidatorUtil.REGEXP_DELIMITER;
   
      /**
  -    * UrlValidator used in wrapper method, lazy initialization.
  +    * UrlValidator used in wrapper method.
       */
  -   private static UrlValidator urlValidator = null;
  +   private static final UrlValidator urlValidator = new UrlValidator();
  +   
  +   /**
  +    * CreditCardValidator used in wrapper method.
  +    */
  +    private static final CreditCardValidator creditCardValidator =
  +    	new CreditCardValidator();
      
      /**
       * <p>Checks if the field isn't null and length of the field is greater than zero
not
  @@ -246,33 +252,30 @@
   
      /**
       * Checks if the field is a valid credit card number.
  -    *
       * @param value The value validation is being performed on.
       */
      public static boolean isCreditCard(String value) {
  -      return CreditCardValidator.getInstance().isValid(value);
  +      return creditCardValidator.isValid(value);
      }
   
      /**
       * Checks for a valid credit card number.
       *
       * @param cardNumber Credit Card Number.
  -    * @deprecated Use CreditCardValidator.validateCreditCardLuhnCheck() instead.
  +    * @deprecated This functionality has moved to CreditCardValidator.
       */
      protected static boolean validateCreditCardLuhnCheck(String cardNumber) {
  -      return CreditCardValidator.getInstance().validateCreditCardLuhnCheck(
  -            cardNumber);
  +        return (new CreditCardValidator()).luhnCheck(cardNumber);
      }
   
      /**
       * Checks for a valid credit card number.
       *
       * @param cardNumber Credit Card Number.
  -    * @deprecated Use CreditCardValidator.validateCreditCardPrefixCheck() instead.
  +    * @deprecated This functionality has move to CreditCardValidator.
       */
      protected boolean validateCreditCardPrefixCheck(String cardNumber) {
  -      return CreditCardValidator.getInstance().validateCreditCardPrefixCheck(
  -            cardNumber);
  +	   return (new CreditCardValidator()).isValidPrefix(cardNumber);
      }
   
      /**
  @@ -292,9 +295,6 @@
       * @param value The value validation is being performed on.
       */
      public static boolean isUrl(String value) {
  -      if (urlValidator == null) {
  -         urlValidator = new UrlValidator();
  -      }
         return urlValidator.isValid(value);
      }
   
  
  
  

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


Mime
View raw message