commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r591497 - in /commons/proper/validator/trunk/src: main/java/org/apache/commons/validator/routines/CodeValidator.java test/java/org/apache/commons/validator/routines/CodeValidatorTest.java
Date Fri, 02 Nov 2007 22:25:16 GMT
Author: niallp
Date: Fri Nov  2 15:25:16 2007
New Revision: 591497

URL: http://svn.apache.org/viewvc?rev=591497&view=rev
Log:
VALIDATOR-215 - make CodeValidator immutable and thread safe

Modified:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CodeValidator.java
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CodeValidatorTest.java

Modified: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CodeValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CodeValidator.java?rev=591497&r1=591496&r2=591497&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CodeValidator.java
(original)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/CodeValidator.java
Fri Nov  2 15:25:16 2007
@@ -50,16 +50,10 @@
  */
 public final class CodeValidator implements Serializable {
 
-    private RegexValidator regexValidator;
-    private int minLength = -1;
-    private int maxLength = -1;
-    private CheckDigit checkdigit;
-
-    /**
-     * Default Constructor.
-     */
-    public CodeValidator() {
-    }
+    private final RegexValidator regexValidator;
+    private final int minLength;
+    private final int maxLength;
+    private final CheckDigit checkdigit;
 
     /**
      * Construct a code validator with a specified regular
@@ -96,10 +90,14 @@
      */
     public CodeValidator(String regex, int minLength, int maxLength,
             CheckDigit checkdigit) {
-        setRegex(regex);
-        setMinLength(minLength);
-        setMaxLength(maxLength);
-        setCheckDigit(checkdigit);
+        if (regex != null && regex.length() > 0) {
+            this.regexValidator = new RegexValidator(regex);
+        } else {
+            this.regexValidator = null;
+        }
+        this.minLength = minLength;
+        this.maxLength = maxLength;
+        this.checkdigit = checkdigit;
     }
 
     /**
@@ -137,10 +135,10 @@
      */
     public CodeValidator(RegexValidator regexValidator, int minLength, int maxLength,
             CheckDigit checkdigit) {
-        setRegexValidator(regexValidator);
-        setMinLength(minLength);
-        setMaxLength(maxLength);
-        setCheckDigit(checkdigit);
+        this.regexValidator = regexValidator;
+        this.minLength = minLength;
+        this.maxLength = maxLength;
+        this.checkdigit = checkdigit;
     }
 
     /**
@@ -156,29 +154,6 @@
     }
 
     /**
-     * Set the check digit validation routine.
-     * <p>
-     * <b>N.B.</b> Optional, if not set no Check Digit
-     * validation will be performed on the code.
-     *
-     * @param checkdigit The check digit validation routine
-     */
-    public void setCheckDigit(CheckDigit checkdigit) {
-        this.checkdigit = checkdigit;
-    }
-
-    /**
-     * Convenience method that sets the minimum and
-     * maximum length to the same value.
-     *
-     * @param length The length of the code
-     */
-    public void setLength(int length) {
-        setMinLength(length);
-        setMaxLength(length);
-    }
-
-    /**
      * Return the minimum length of the code.
      * <p>
      * <b>N.B.</b> Optional, if less than zero the
@@ -192,19 +167,6 @@
     }
 
     /**
-     * Set the minimum length of the code.
-     * <p>
-     * <b>N.B.</b> Optional, if less than zero the
-     * minimum length will not be checked.
-     *
-     * @param minLength The minimum length of the code or
-     * <code>-1</code> if the code has no minimum length
-     */
-    public void setMinLength(int minLength) {
-        this.minLength = minLength;
-    }
-
-    /**
      * Return the maximum length of the code.
      * <p>
      * <b>N.B.</b> Optional, if less than zero the
@@ -218,19 +180,6 @@
     }
 
     /**
-     * Set the maximum length of the code.
-     * <p>
-     * <b>N.B.</b> Optional, if less than zero the
-     * maximum length will not be checked.
-     *
-     * @param maxLength The maximum length of the code or
-     * <code>-1</code> if the code has no maximum length
-     */
-    public void setMaxLength(int maxLength) {
-        this.maxLength = maxLength;
-    }
-
-    /**
      * Return the <i>regular expression</i> validator.
      * <p>
      * <b>N.B.</b> Optional, if not set no regular
@@ -240,39 +189,6 @@
      */
     public RegexValidator getRegexValidator() {
         return regexValidator;
-    }
-
-    /**
-     * Set the <i>regular expression</i> used to validate
-     * the <i>format</i> of the code.
-     * <p>
-     * This is a convenience method which creates a
-     * {@link RegexValidator} with the specified regular
-     * expression.
-     * <p>
-     * <b>N.B.</b> Optional, if not set no regular
-     * expression validation will be performed on the code.
-     *
-     * @param regex The format regular expression.
-     */
-    public void setRegex(String regex) {
-        if (regex != null && regex.length() > 0) {
-            setRegexValidator(new RegexValidator(regex));
-        } else {
-            setRegexValidator(null);
-        }
-    }
-
-    /**
-     * Set the <i>regular expression</i> validator.
-     * <p>
-     * <b>N.B.</b> Optional, if not set no regular
-     * expression validation will be performed on the code.
-     *
-     * @param regexValidator The regular expression validator
-     */
-    public void setRegexValidator(RegexValidator regexValidator) {
-        this.regexValidator = regexValidator;
     }
 
     /**

Modified: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CodeValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CodeValidatorTest.java?rev=591497&r1=591496&r2=591497&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CodeValidatorTest.java
(original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/CodeValidatorTest.java
Fri Nov  2 15:25:16 2007
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.validator.routines;
 
+import org.apache.commons.validator.routines.checkdigit.CheckDigit;
 import org.apache.commons.validator.routines.checkdigit.EAN13CheckDigit;
 
 import junit.framework.TestCase;
@@ -54,7 +55,7 @@
      * Test Check Digit.
      */
     public void testCheckDigit() {
-        CodeValidator validator = new CodeValidator();
+        CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
         String invalidEAN = "9781930110992";
         String validEAN   = "9781930110991";
 
@@ -66,7 +67,7 @@
         assertEquals("No CheckDigit (is) valid",    true, validator.isValid(validEAN));
 
         // Use the EAN-13 check digit routine
-        validator.setCheckDigit(EAN13CheckDigit.INSTANCE);
+        validator = new CodeValidator((String)null, -1, EAN13CheckDigit.INSTANCE);
 
         assertNotNull("EAN CheckDigit", validator.getCheckDigit());
         assertEquals("EAN CheckDigit invalid",       null, validator.validate(invalidEAN));
@@ -80,7 +81,7 @@
      * Test the minimum/maximum length
      */
     public void testLength() {
-        CodeValidator validator = new CodeValidator();
+        CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
         String length_10  = "1234567890";
         String length_11  = "12345678901";
         String length_12  = "123456789012";
@@ -98,8 +99,7 @@
         assertEquals("No Length 21", length_21, validator.validate(length_21));
         assertEquals("No Length 22", length_22, validator.validate(length_22));
         
-        validator.setMinLength(11);
-
+        validator = new CodeValidator((String)null, 11, -1, (CheckDigit)null);
         assertEquals("Min 11 - min", 11, validator.getMinLength());
         assertEquals("Min 11 - max", -1, validator.getMaxLength());
         assertEquals("Min 11 - 10", null,      validator.validate(length_10));
@@ -109,8 +109,7 @@
         assertEquals("Min 11 - 21", length_21, validator.validate(length_21));
         assertEquals("Min 11 - 22", length_22, validator.validate(length_22));
         
-        validator.setMinLength(-1);
-        validator.setMaxLength(21);
+        validator = new CodeValidator((String)null, -1, 21, (CheckDigit)null);
         assertEquals("Max 21 - min", -1, validator.getMinLength());
         assertEquals("Max 21 - max", 21, validator.getMaxLength());
         assertEquals("Max 21 - 10", length_10, validator.validate(length_10));
@@ -120,7 +119,7 @@
         assertEquals("Max 21 - 21", length_21, validator.validate(length_21));
         assertEquals("Max 21 - 22", null,      validator.validate(length_22));
         
-        validator.setMinLength(11);
+        validator = new CodeValidator((String)null, 11, 21, (CheckDigit)null);
         assertEquals("Min 11 / Max 21 - min", 11, validator.getMinLength());
         assertEquals("Min 11 / Max 21 - max", 21, validator.getMaxLength());
         assertEquals("Min 11 / Max 21 - 10", null,      validator.validate(length_10));
@@ -130,7 +129,7 @@
         assertEquals("Min 11 / Max 21 - 21", length_21, validator.validate(length_21));
         assertEquals("Min 11 / Max 21 - 22", null,      validator.validate(length_22));
 
-        validator.setLength(11);
+        validator = new CodeValidator((String)null, 11, 11, (CheckDigit)null);
         assertEquals("Exact 11 - min", 11, validator.getMinLength());
         assertEquals("Exact 11 - max", 11, validator.getMaxLength());
         assertEquals("Exact 11 - 10", null,      validator.validate(length_10));
@@ -142,7 +141,7 @@
      * Test Regular Expression.
      */
     public void testRegex() {
-        CodeValidator validator = new CodeValidator();
+        CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
 
         String value2  = "12";
         String value3  = "123";
@@ -159,7 +158,8 @@
         assertEquals("No Regex invalid", invalid, validator.validate(invalid));
 
         // Regular Expression
-        validator.setRegex("^([0-9]{3,4})$");
+        String regex = "^([0-9]{3,4})$";
+        validator = new CodeValidator(regex, -1, -1, (CheckDigit)null);
         assertNotNull("No Regex", validator.getRegexValidator());
         assertEquals("Regex 2", null,   validator.validate(value2));
         assertEquals("Regex 3", value3, validator.validate(value3));
@@ -168,28 +168,27 @@
         assertEquals("Regex invalid", null, validator.validate(invalid));
 
         // Reformatted
-        validator.setRegexValidator(new RegexValidator("^([0-9]{3})(?:[-\\s])([0-9]{3})$"));
-        validator.setLength(6);
+        regex = "^([0-9]{3})(?:[-\\s])([0-9]{3})$";
+        validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null);
         assertEquals("Reformat 123-456", "123456", validator.validate("123-456"));
         assertEquals("Reformat 123 456", "123456", validator.validate("123 456"));
         assertEquals("Reformat 123456",  null,     validator.validate("123456"));
         assertEquals("Reformat 123.456", null,     validator.validate("123.456"));
 
-        String regex = "^(?:([0-9]{3})(?:[-\\s])([0-9]{3}))|([0-9]{6})$";
-        validator.setRegex(regex);
+        regex = "^(?:([0-9]{3})(?:[-\\s])([0-9]{3}))|([0-9]{6})$";
+        validator = new CodeValidator(new RegexValidator(regex), 6, (CheckDigit)null);
         assertEquals("Reformat 2 Regex",  "RegexValidator{" + regex + "}", validator.getRegexValidator().toString());
         assertEquals("Reformat 2 123-456", "123456", validator.validate("123-456"));
         assertEquals("Reformat 2 123 456", "123456", validator.validate("123 456"));
         assertEquals("Reformat 2 123456",  "123456", validator.validate("123456"));
-        validator.setRegex(null);
-        assertEquals("Reformat 2 Regex null",  null, validator.getRegexValidator());
+
     }
 
     /**
      * Test Regular Expression.
      */
     public void testNoInput() {
-        CodeValidator validator = new CodeValidator();
+        CodeValidator validator = new CodeValidator((String)null, -1, -1, (CheckDigit)null);
         assertEquals("Null",         null, validator.validate(null));
         assertEquals("Zero Length",  null, validator.validate(""));
         assertEquals("Spaces",       null, validator.validate("   "));



Mime
View raw message