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 <garydgreg...@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) {