diff --git a/pom.xml b/pom.xml index 29efa4e6a..914a6ca20 100644 --- a/pom.xml +++ b/pom.xml @@ -339,6 +339,9 @@ Antony Riley + + Valentin Rocher + Scott Sanders diff --git a/src/main/java/org/apache/commons/lang3/Validate.java b/src/main/java/org/apache/commons/lang3/Validate.java index 4c867a5fc..bb0a1101a 100644 --- a/src/main/java/org/apache/commons/lang3/Validate.java +++ b/src/main/java/org/apache/commons/lang3/Validate.java @@ -19,6 +19,7 @@ package org.apache.commons.lang3; import java.util.Collection; import java.util.Iterator; import java.util.Map; +import java.util.regex.Pattern; /** *

This class assists in validating arguments. The validation methods are @@ -48,6 +49,9 @@ import java.util.Map; */ public class Validate { + private static final String DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE = "The value %s is not in the specified exclusive range of %s to %s"; + private static final String DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE = "The value %s is not in the specified inclusive range of %s to %s"; + private static final String DEFAULT_MATCHES_PATTERN_EX = "The string %s does not match the pattern %s"; private static final String DEFAULT_IS_NULL_EX_MESSAGE = "The validated object is null"; private static final String DEFAULT_IS_TRUE_EX_MESSAGE = "The validated expression is false"; private static final String DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE = "The validated array contains null element at index: %d"; @@ -781,5 +785,134 @@ public class Validate { throw new IllegalStateException(String.format(message, values)); } } - + + /** + *

Validate that the specified argument character sequence matches the specified regular + * expression pattern; otherwise throwing an exception.

+ * + *
Validate.matchesPattern("hi", "[a-z]*");
+ * + *

The syntax of the pattern is the one used in the {@link Pattern} class.

+ * + * @param input the character sequence to validate + * @param pattern regular expression pattern + * @throws IllegalArgumentException if the character sequence does not match the pattern + * @see #matchesPattern(String, String, String, Object...) + */ + public static void matchesPattern(CharSequence input, String pattern) + { + if (Pattern.matches(pattern, input) == false) + { + throw new IllegalArgumentException(String.format(DEFAULT_MATCHES_PATTERN_EX, input, pattern)); + } + } + + /** + *

Validate that the specified argument character sequence matches the specified regular + * expression pattern; otherwise throwing an exception with the specified message.

+ * + *
Validate.matchesPattern("hi", "[a-z]*", "%s does not match %s", "hi" "[a-z]*");
+ * + *

The syntax of the pattern is the one used in the {@link Pattern} class.

+ * + * @param input the character sequence to validate + * @param pattern regular expression pattern + * @param message the exception message + * @param optional values to replace in the exception message + * @throws IllegalArgumentException if the character sequence does not match the pattern + * @see #matchesPattern(String, String) + */ + public static void matchesPattern(CharSequence input, String pattern, String message, Object... values) + { + if (Pattern.matches(pattern, input) == false) + { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validate that the specified argument object fall between the two + * inclusive values specified; otherwise, throws an exception.

+ * + *
Validate.inclusiveBetween(0, 2, 1);
+ * + * @param value the object to validate + * @param start the inclusive start value + * @param end the inclusive end value + * @throws IllegalArgumentException if the value falls out of the boundaries + * @see #inclusiveBetween(Object, Object, Comparable, String, Object...) + */ + public static void inclusiveBetween(T start, T end, Comparable value) + { + if (value.compareTo(start) < 0 || value.compareTo(end) > 0) + { + throw new IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); + } + } + + /** + *

Validate that the specified argument object fall between the two + * inclusive values specified; otherwise, throws an exception with the + * specified message.

+ * + *
Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");
+ * + * @param value the object to validate + * @param start the inclusive start value + * @param end the inclusive end value + * @param message the exception message + * @param optional values to replace in the exception message + * @throws IllegalArgumentException if the value falls out of the boundaries + * @see #inclusiveBetween(Object, Object, Comparable) + */ + public static void inclusiveBetween(T start, T end, Comparable value, String message, Object... values) + { + if (value.compareTo(start) < 0 || value.compareTo(end) > 0) + { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *

Validate that the specified argument object fall between the two + * exclusive values specified; otherwise, throws an exception.

+ * + *
Validate.inclusiveBetween(0, 2, 1);
+ * + * @param value the object to validate + * @param start the exclusive start value + * @param end the exclusive end value + * @throws IllegalArgumentException if the value falls out of the boundaries + * @see #exclusiveBetween(Object, Object, Comparable, String, Object...) + */ + public static void exclusiveBetween(T start, T end, Comparable value) + { + if (value.compareTo(start) <= 0 || value.compareTo(end) >= 0) + { + throw new IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end)); + } + } + + /** + *

Validate that the specified argument object fall between the two + * exclusive values specified; otherwise, throws an exception with the + * specified message.

+ * + *
Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");
+ * + * @param value the object to validate + * @param start the exclusive start value + * @param end the exclusive end value + * @param message the exception message + * @param optional values to replace in the exception message + * @throws IllegalArgumentException if the value falls out of the boundaries + * @see #exclusiveBetween(Object, Object, Comparable) + */ + public static void exclusiveBetween(T start, T end, Comparable value, String message, Object... values) + { + if (value.compareTo(start) <= 0 || value.compareTo(end) >= 0) + { + throw new IllegalArgumentException(String.format(message, values)); + } + } } diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index dd0fee7a2..647bc4630 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -754,5 +754,96 @@ public class ValidateTest extends TestCase { String test = Validate.validIndex(input, 0); assertSame(input, test); } - + + public void testMatchesPattern() + { + CharSequence str = "hi"; + Validate.matchesPattern(str, "[a-z]*"); + try + { + Validate.matchesPattern(str, "[0-9]*"); + fail("Expecting IllegalArgumentException"); + } + catch (IllegalArgumentException e) + { + assertEquals("The string hi does not match the pattern [0-9]*", e.getMessage()); + } + } + + public void testMatchesPattern_withMessage() + { + CharSequence str = "hi"; + Validate.matchesPattern(str, "[a-z]*", "Does not match"); + try + { + Validate.matchesPattern(str, "[0-9]*", "Does not match"); + fail("Expecting IllegalArgumentException"); + } + catch (IllegalArgumentException e) + { + assertEquals("Does not match", e.getMessage()); + } + } + + public void testInclusiveBetween() + { + Validate.inclusiveBetween("a", "c", "b"); + Validate.inclusiveBetween(0, 2, 1); + Validate.inclusiveBetween(0, 2, 2); + try { + Validate.inclusiveBetween(0, 5, 6); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("The value 6 is not in the specified inclusive range of 0 to 5", e.getMessage()); + } + } + + public void testInclusiveBetween_withMessage() + { + Validate.inclusiveBetween("a", "c", "b", "Error"); + Validate.inclusiveBetween(0, 2, 1, "Error"); + Validate.inclusiveBetween(0, 2, 2, "Error"); + try { + Validate.inclusiveBetween(0, 5, 6, "Error"); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Error", e.getMessage()); + } + } + + public void testExclusiveBetween() + { + Validate.exclusiveBetween("a", "c", "b"); + Validate.exclusiveBetween(0, 2, 1); + try { + Validate.exclusiveBetween(0, 5, 6); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("The value 6 is not in the specified exclusive range of 0 to 5", e.getMessage()); + } + try { + Validate.exclusiveBetween(0, 5, 5); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("The value 5 is not in the specified exclusive range of 0 to 5", e.getMessage()); + } + } + + public void testExclusiveBetween_withMessage() + { + Validate.exclusiveBetween("a", "c", "b", "Error"); + Validate.exclusiveBetween(0, 2, 1, "Error"); + try { + Validate.exclusiveBetween(0, 5, 6, "Error"); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Error", e.getMessage()); + } + try { + Validate.exclusiveBetween(0, 5, 5, "Error"); + fail("Expecting IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Error", e.getMessage()); + } + } }