Author: britter
Date: Mon Jul  7 20:21:21 2014
New Revision: 1608599

URL: http://svn.apache.org/r1608599
Log:
VALIDATOR-266: DomainValidator uses an O(n) method where an O(1) would be more 
appropriate. Thanks to Bruce Collie

Modified:
    commons/proper/validator/trunk/src/changes/changes.xml
    
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java

Modified: commons/proper/validator/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/changes/changes.xml?rev=1608599&r1=1608598&r2=1608599&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/changes/changes.xml (original)
+++ commons/proper/validator/trunk/src/changes/changes.xml Mon Jul  7 20:21:21 
2014
@@ -43,6 +43,9 @@ The <action> type attribute can be add,u
   <body>
 
   <release version="1.4.1" date="TBA" description="TBA">
+    <action dev="britter" type="fix" issue="VALIDATOR-266" due-to="Bruce 
Collie" >
+      DomainValidator uses an O(n) method where an O(1) would be more 
appropriate
+    </action>
     <action dev="britter" type="fix" issue="VALIDATOR-273" due-to="Chris Lee" >
       EmailValidator does not support mailboxes at TLDs
     </action>

Modified: 
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
URL: 
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java?rev=1608599&r1=1608598&r2=1608599&view=diff
==============================================================================
--- 
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
 (original)
+++ 
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/DomainValidator.java
 Mon Jul  7 20:21:21 2014
@@ -165,7 +165,7 @@ public class DomainValidator implements 
      * @return true if the parameter is an infrastructure TLD
      */
     public boolean isValidInfrastructureTld(String iTld) {
-        return 
INFRASTRUCTURE_TLD_LIST.contains(chompLeadingDot(iTld.toLowerCase()));
+        return Arrays.binarySearch(INFRASTRUCTURE_TLDS, 
(chompLeadingDot(iTld.toLowerCase()))) >= 0;
     }
 
     /**
@@ -176,7 +176,7 @@ public class DomainValidator implements 
      * @return true if the parameter is a generic TLD
      */
     public boolean isValidGenericTld(String gTld) {
-        return GENERIC_TLD_LIST.contains(chompLeadingDot(gTld.toLowerCase()));
+        return Arrays.binarySearch(GENERIC_TLDS, 
chompLeadingDot(gTld.toLowerCase())) >= 0;
     }
 
     /**
@@ -187,7 +187,7 @@ public class DomainValidator implements 
      * @return true if the parameter is a country code TLD
      */
     public boolean isValidCountryCodeTld(String ccTld) {
-        return 
COUNTRY_CODE_TLD_LIST.contains(chompLeadingDot(ccTld.toLowerCase()));
+        return Arrays.binarySearch(COUNTRY_CODE_TLDS, 
chompLeadingDot(ccTld.toLowerCase())) >= 0;
     }
 
     /**
@@ -198,7 +198,7 @@ public class DomainValidator implements 
      * @return true if the parameter is an local TLD
      */
     public boolean isValidLocalTld(String iTld) {
-        return LOCAL_TLD_LIST.contains(chompLeadingDot(iTld.toLowerCase()));
+        return Arrays.binarySearch(LOCAL_TLDS, 
chompLeadingDot(iTld.toLowerCase())) >= 0;
     }
 
     private String chompLeadingDot(String str) {
@@ -680,8 +680,10 @@ public class DomainValidator implements 
        "localdomain"          // Also widely used as localhost.localdomain
    };
 
-    private static final List INFRASTRUCTURE_TLD_LIST = 
Arrays.asList(INFRASTRUCTURE_TLDS);
-    private static final List GENERIC_TLD_LIST = Arrays.asList(GENERIC_TLDS);
-    private static final List COUNTRY_CODE_TLD_LIST = 
Arrays.asList(COUNTRY_CODE_TLDS);
-    private static final List LOCAL_TLD_LIST = Arrays.asList(LOCAL_TLDS);
+    static {
+        Arrays.sort(INFRASTRUCTURE_TLDS);
+        Arrays.sort(COUNTRY_CODE_TLDS);
+        Arrays.sort(GENERIC_TLDS);
+        Arrays.sort(LOCAL_TLDS);
+    }
 }


Reply via email to