You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/09/19 12:22:52 UTC
(commons-validator) branch master updated (68124abe -> 4955aa62)
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git
from 68124abe Merge pull request #260 from apache/dependabot/github_actions/github/codeql-action-3.26.7
new 564360aa Javadoc
new 31f972fa Javadoc
new 2549a1de Internal refactoring
new 4955aa62 InetAddressValidator does not need its instance variable, so uses a touch less memory
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 3 +-
.../validator/routines/InetAddressValidator.java | 19 +--
.../commons/validator/routines/RegexValidator.java | 147 ++++++++++-----------
.../routines/InetAddressValidatorTest.java | 2 +-
4 files changed, 86 insertions(+), 85 deletions(-)
(commons-validator) 01/04: Javadoc
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git
commit 564360aaa047519524735300f5e32e3712c0feb3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 19 08:07:09 2024 -0400
Javadoc
Use longer lines
---
.../commons/validator/routines/InetAddressValidator.java | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java b/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
index d3a6153a..b0a22da6 100644
--- a/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
+++ b/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
@@ -48,8 +48,7 @@ public class InetAddressValidator implements Serializable {
private static final long serialVersionUID = -919201640201914789L;
- private static final String IPV4_REGEX =
- "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
+ private static final String IPV4_REGEX = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$";
// Max number of hex groups (separated by :) in an IPV6 address
private static final int IPV6_MAX_HEX_GROUPS = 8;
@@ -65,8 +64,10 @@ public class InetAddressValidator implements Serializable {
private static final Pattern DIGITS_PATTERN = Pattern.compile("\\d{1,3}");
private static final Pattern ID_CHECK_PATTERN = Pattern.compile("[^\\s/%]+");
+
/**
* Returns the singleton instance of this validator.
+ *
* @return the singleton instance of this validator
*/
public static InetAddressValidator getInstance() {
@@ -78,6 +79,7 @@ public class InetAddressValidator implements Serializable {
/**
* Checks if the specified string is a valid IPv4 or IPv6 address.
+ *
* @param inetAddress the string to validate
* @return true if the string validates as an IP address
*/
@@ -87,6 +89,7 @@ public class InetAddressValidator implements Serializable {
/**
* Validates an IPv4 address. Returns true if valid.
+ *
* @param inet4Address the IPv4 address to validate
* @return true if the argument contains a valid IPv4 address
*/
@@ -119,6 +122,7 @@ public class InetAddressValidator implements Serializable {
/**
* Validates an IPv6 address. Returns true if valid.
+ *
* @param inet6Address the IPv6 address to validate
* @return true if the argument contains a valid IPv6 address
*
@@ -155,8 +159,7 @@ public class InetAddressValidator implements Serializable {
if (containsCompressedZeroes && inet6Address.indexOf("::") != inet6Address.lastIndexOf("::")) {
return false;
}
- if (inet6Address.startsWith(":") && !inet6Address.startsWith("::")
- || inet6Address.endsWith(":") && !inet6Address.endsWith("::")) {
+ if (inet6Address.startsWith(":") && !inet6Address.startsWith("::") || inet6Address.endsWith(":") && !inet6Address.endsWith("::")) {
return false;
}
String[] octets = inet6Address.split(":");
(commons-validator) 03/04: Internal refactoring
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git
commit 2549a1def7207431377b250624e0c3e65dea4452
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 19 08:19:31 2024 -0400
Internal refactoring
- Use vargars
- Use longer lines
---
.../commons/validator/routines/RegexValidator.java | 147 ++++++++++-----------
1 file changed, 72 insertions(+), 75 deletions(-)
diff --git a/src/main/java/org/apache/commons/validator/routines/RegexValidator.java b/src/main/java/org/apache/commons/validator/routines/RegexValidator.java
index de0580c4..414a319f 100644
--- a/src/main/java/org/apache/commons/validator/routines/RegexValidator.java
+++ b/src/main/java/org/apache/commons/validator/routines/RegexValidator.java
@@ -24,12 +24,11 @@ import java.util.regex.Pattern;
/**
* <b>Regular Expression</b> validation (using the JRE's regular expression support).
* <p>
- * Constructs the validator either for a single regular expression or a set (array) of
- * regular expressions. By default validation is <em>case sensitive</em> but constructors
- * are provided to allow <em>case in-sensitive</em> validation. For example to create
- * a validator which does <em>case in-sensitive</em> validation for a set of regular
- * expressions:
+ * Constructs the validator either for a single regular expression or a set (array) of regular expressions. By default validation is <em>case sensitive</em> but
+ * constructors are provided to allow <em>case in-sensitive</em> validation. For example to create a validator which does <em>case in-sensitive</em> validation
+ * for a set of regular expressions:
* </p>
+ *
* <pre>
* <code>
* String[] regexs = new String[] {...};
@@ -38,31 +37,30 @@ import java.util.regex.Pattern;
* </pre>
*
* <ul>
- * <li>Validate {@code true} or {@code false}:</li>
- * <li>
- * <ul>
- * <li>{@code boolean valid = validator.isValid(value);}</li>
- * </ul>
- * </li>
- * <li>Validate returning an aggregated String of the matched groups:</li>
- * <li>
- * <ul>
- * <li>{@code String result = validator.validate(value);}</li>
- * </ul>
- * </li>
- * <li>Validate returning the matched groups:</li>
- * <li>
- * <ul>
- * <li>{@code String[] result = validator.match(value);}</li>
- * </ul>
- * </li>
+ * <li>Validate {@code true} or {@code false}:</li>
+ * <li>
+ * <ul>
+ * <li>{@code boolean valid = validator.isValid(value);}</li>
+ * </ul>
+ * </li>
+ * <li>Validate returning an aggregated String of the matched groups:</li>
+ * <li>
+ * <ul>
+ * <li>{@code String result = validator.validate(value);}</li>
+ * </ul>
+ * </li>
+ * <li>Validate returning the matched groups:</li>
+ * <li>
+ * <ul>
+ * <li>{@code String[] result = validator.match(value);}</li>
+ * </ul>
+ * </li>
* </ul>
*
* <b>Note that patterns are matched against the entire input.</b>
*
* <p>
- * Cached instances pre-compile and re-use {@link Pattern}(s) - which according
- * to the {@link Pattern} API are safe to use in a multi-threaded environment.
+ * Cached instances pre-compile and re-use {@link Pattern}(s) - which according to the {@link Pattern} API are safe to use in a multi-threaded environment.
* </p>
*
* @since 1.4
@@ -71,79 +69,82 @@ public class RegexValidator implements Serializable {
private static final long serialVersionUID = -8832409930574867162L;
+ private static final int CASE_SENSITIVE = 0;
+
+ private static int toCompileFlags(final boolean caseSensitive) {
+ return caseSensitive ? CASE_SENSITIVE : Pattern.CASE_INSENSITIVE;
+ }
+
/**
* Compiled RE patterns from constructors.
*/
private final Pattern[] patterns;
/**
- * Constructs a <em>case sensitive</em> validator that matches any one
- * in the list of regular expressions.
+ * Constructs a new instance that matches any one of the set of regular expressions with the specified case sensitivity.
+ *
+ * @param regexs The set of regular expressions this validator will validate against
+ * @param flags See {@link Pattern#compile(String, int)}. sensitive</i>, otherwise matching is <em>case in-sensitive</em>
+ */
+ private RegexValidator(final int flags, final String... regexs) {
+ if (regexs == null || regexs.length == 0) {
+ throw new IllegalArgumentException("Regular expressions are missing");
+ }
+ patterns = new Pattern[regexs.length];
+ for (int i = 0; i < regexs.length; i++) {
+ final String regex = regexs[i];
+ if (regex == null || regex.isEmpty()) {
+ throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
+ }
+ patterns[i] = Pattern.compile(regex, flags);
+ }
+ }
+
+ /**
+ * Constructs a new <em>case sensitive</em> instance that matches any one in the list of regular expressions.
*
- * @param regexs The set of regular expressions this validator will
- * validate against
+ * @param regexs The set of regular expressions this validator will validate against
*/
RegexValidator(final List<String> regexs) {
- this(regexs.toArray(new String[] {}), true);
+ this(CASE_SENSITIVE, regexs.toArray(new String[] {}));
}
/**
- * Constructs a <em>case sensitive</em> validator for a single
- * regular expression.
+ * Constructs a new <em>case sensitive</em> instance for a single regular expression.
*
- * @param regex The regular expression this validator will
- * validate against
+ * @param regex The regular expression this validator will validate against
*/
public RegexValidator(final String regex) {
- this(regex, true);
+ this(CASE_SENSITIVE, regex);
}
/**
- * Constructs a <em>case sensitive</em> validator that matches any one
- * in the array of regular expressions.
+ * Constructs a new <em>case sensitive</em> instance that matches any one in the array of regular expressions.
*
- * @param regexs The set of regular expressions this validator will
- * validate against
+ * @param regexs The set of regular expressions this validator will validate against
*/
public RegexValidator(final String... regexs) {
- this(regexs, true);
+ this(CASE_SENSITIVE, regexs);
}
/**
- * Constructs a validator for a single regular expression
- * with the specified case sensitivity.
+ * Constructs a new instance for a single regular expression with the specified case sensitivity.
*
- * @param regex The regular expression this validator will
- * validate against
- * @param caseSensitive when {@code true} matching is <i>case
- * sensitive</i>, otherwise matching is <em>case in-sensitive</em>
+ * @param regex The regular expression this validator will validate against
+ * @param caseSensitive when {@code true} matching is <i>case sensitive</i>, otherwise matching is <em>case in-sensitive</em>
*/
public RegexValidator(final String regex, final boolean caseSensitive) {
- this(new String[] { regex }, caseSensitive);
+ this(toCompileFlags(caseSensitive), regex);
}
/**
- * Constructs a validator that matches any one of the set of regular
- * expressions with the specified case sensitivity.
+ * Constructs a new instance that matches any one of the set of regular expressions with the specified case sensitivity.
*
- * @param regexs The set of regular expressions this validator will
- * validate against
- * @param caseSensitive when {@code true} matching is <i>case
- * sensitive</i>, otherwise matching is <em>case in-sensitive</em>
+ * @param regexs The set of regular expressions this validator will validate against
+ * @param caseSensitive when {@code true} matching is <i>case sensitive</i>, otherwise matching is <em>case in-sensitive</em>
*/
public RegexValidator(final String[] regexs, final boolean caseSensitive) {
- if (regexs == null || regexs.length == 0) {
- throw new IllegalArgumentException("Regular expressions are missing");
- }
- patterns = new Pattern[regexs.length];
- final int flags = caseSensitive ? 0 : Pattern.CASE_INSENSITIVE;
- for (int i = 0; i < regexs.length; i++) {
- final String regex = regexs[i];
- if (regex == null || regex.isEmpty()) {
- throw new IllegalArgumentException("Regular expression[" + i + "] is missing");
- }
- patterns[i] = Pattern.compile(regex, flags);
- }
+ this(toCompileFlags(caseSensitive), regexs);
}
/**
@@ -160,8 +161,7 @@ public class RegexValidator implements Serializable {
* Validates a value against the set of regular expressions.
*
* @param value The value to validate.
- * @return {@code true} if the value is valid
- * otherwise {@code false}.
+ * @return {@code true} if the value is valid otherwise {@code false}.
*/
public boolean isValid(final String value) {
if (value == null) {
@@ -176,12 +176,10 @@ public class RegexValidator implements Serializable {
}
/**
- * Validates a value against the set of regular expressions
- * returning the array of matched groups.
+ * Validates a value against the set of regular expressions returning the array of matched groups.
*
* @param value The value to validate.
- * @return String array of the <em>groups</em> matched if
- * valid or {@code null} if invalid
+ * @return String array of the <em>groups</em> matched if valid or {@code null} if invalid
*/
public String[] match(final String value) {
if (value == null) {
@@ -203,6 +201,7 @@ public class RegexValidator implements Serializable {
/**
* Provides a String representation of this validator.
+ *
* @return A String representation of this validator.
*/
@Override
@@ -220,12 +219,10 @@ public class RegexValidator implements Serializable {
}
/**
- * Validates a value against the set of regular expressions
- * returning a String value of the aggregated groups.
+ * Validates a value against the set of regular expressions returning a String value of the aggregated groups.
*
* @param value The value to validate.
- * @return Aggregated String value comprised of the
- * <em>groups</em> matched if valid or {@code null} if invalid
+ * @return Aggregated String value comprised of the <em>groups</em> matched if valid or {@code null} if invalid
*/
public String validate(final String value) {
if (value == null) {
(commons-validator) 04/04: InetAddressValidator does not need its instance variable, so uses a touch less memory
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git
commit 4955aa62ddb6b0f65607edd2e50ee57c40d71247
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 19 08:22:45 2024 -0400
InetAddressValidator does not need its instance variable, so uses a
touch less memory
---
src/changes/changes.xml | 3 ++-
.../apache/commons/validator/routines/InetAddressValidator.java | 8 ++++----
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index a5bd00ec..50e201fd 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -64,7 +64,8 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.10.0" date="YYYY-MM-DD" description="This is a maintenance and bug fix release; requires Java 8.">
<!-- FIX -->
- <action type="update" dev="ggregory" due-to="Johannes Weberhofer">Fix order of actual and expected parameters in assertEquals() #246.</action>
+ <action type="update" dev="ggregory" due-to="Johannes Weberhofer">Fix order of actual and expected parameters in assertEquals() #246.</action>
+ <action type="update" dev="ggregory" due-to="Johannes Weberhofer">InetAddressValidator does not need its instance variable, so uses a touch less memory.</action>
<!-- ADD -->
<action type="update" dev="ggregory" due-to="Leonard Wicke, Gary Gregory">Add IBANValidator.Validator.getIbanLength().</action>
<!-- UPDATE -->
diff --git a/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java b/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
index b0a22da6..07d90038 100644
--- a/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
+++ b/src/main/java/org/apache/commons/validator/routines/InetAddressValidator.java
@@ -65,6 +65,9 @@ public class InetAddressValidator implements Serializable {
private static final Pattern ID_CHECK_PATTERN = Pattern.compile("[^\\s/%]+");
+ /** IPv4 RegexValidator */
+ private final static RegexValidator IPV4_VALIDATOR = new RegexValidator(IPV4_REGEX);
+
/**
* Returns the singleton instance of this validator.
*
@@ -74,9 +77,6 @@ public class InetAddressValidator implements Serializable {
return VALIDATOR;
}
- /** IPv4 RegexValidator */
- private final RegexValidator ipv4Validator = new RegexValidator(IPV4_REGEX);
-
/**
* Checks if the specified string is a valid IPv4 or IPv6 address.
*
@@ -95,7 +95,7 @@ public class InetAddressValidator implements Serializable {
*/
public boolean isValidInet4Address(final String inet4Address) {
// verify that address conforms to generic IPv4 format
- final String[] groups = ipv4Validator.match(inet4Address);
+ final String[] groups = IPV4_VALIDATOR.match(inet4Address);
if (groups == null) {
return false;
}
(commons-validator) 02/04: Javadoc
Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-validator.git
commit 31f972faa2732ed995960cb516b29b94a80869d0
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 19 08:07:43 2024 -0400
Javadoc
---
.../org/apache/commons/validator/routines/InetAddressValidatorTest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java b/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
index 2ea7b33e..49971f2e 100644
--- a/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
+++ b/src/test/java/org/apache/commons/validator/routines/InetAddressValidatorTest.java
@@ -657,7 +657,7 @@ public class InetAddressValidatorTest {
}
/**
- * Inet6Address may also contain a scope id
+ * Inet6Address may also contain a scope id.
*/
@Test
public void testValidator445() {