diff --git a/src/main/java/org/apache/commons/lang3/Validate.java b/src/main/java/org/apache/commons/lang3/Validate.java index e94afaf65..1e7e9822e 100644 --- a/src/main/java/org/apache/commons/lang3/Validate.java +++ b/src/main/java/org/apache/commons/lang3/Validate.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -22,22 +22,23 @@ import java.util.regex.Pattern; /** - *
This class assists in validating arguments. The validation methods are - * based along the following principles: + *
This class assists in validating arguments. The validation methods are + * based along the following principles: *
All exceptions messages are format strings + * + *
All exceptions messages are + * format strings * as defined by the Java platform. For example:
- * + * ** Validate.isTrue(i > 0, "The value must be greater than zero: %d", i); * Validate.notNull(surname, "The surname must not be %s", null); *- * + * *
#ThreadSafe#
* @author Apache Software Foundation * @author Ola Berg @@ -50,23 +51,31 @@ */ 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_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"; - private static final String DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE = "The validated collection contains null element at index: %d"; + private static final String DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE = + "The validated array contains null element at index: %d"; + private static final String DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE = + "The validated collection contains null element at index: %d"; private static final String DEFAULT_NOT_BLANK_EX_MESSAGE = "The validated character sequence is blank"; private static final String DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE = "The validated array is empty"; - private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence is empty"; + private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE = + "The validated character sequence is empty"; private static final String DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE = "The validated collection is empty"; private static final String DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE = "The validated map is empty"; private static final String DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE = "The validated array index is invalid: %d"; - private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = "The validated character sequence index is invalid: %d"; - private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = "The validated collection index is invalid: %d"; + private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE = + "The validated character sequence index is invalid: %d"; + private static final String DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE = + "The validated collection index is invalid: %d"; private static final String DEFAULT_VALID_STATE_EX_MESSAGE = "The validated state is false"; - private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = "The validated class can not be converted to the %s class"; + private static final String DEFAULT_IS_ASSIGNABLE_EX_MESSAGE = + "The validated class can not be converted to the %s class"; private static final String DEFAULT_IS_INSTANCE_OF_EX_MESSAGE = "The validated object is not an instance of %s"; /** @@ -80,16 +89,16 @@ public Validate() { //--------------------------------------------------------------------------------- /** - *Validate that the argument condition is {@code true}; otherwise + *
Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when - * validating according to an arbitrary boolean expression, such as validating a + * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.
* *Validate.isTrue(i > 0.0, "The value must be greater than zero: %d", i);* *
For performance reasons, the long value is passed as a separate parameter and * appended to the exception message only in the case of an error.
- * + * * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param value the value to append to the message when invalid @@ -105,17 +114,17 @@ public static void isTrue(boolean expression, String message, long value) { } /** - *Validate that the argument condition is {@code true}; otherwise + *
Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when - * validating according to an arbitrary boolean expression, such as validating a + * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.
* *Validate.isTrue(d > 0.0, "The value must be greater than zero: %s", d);* *
For performance reasons, the double value is passed as a separate parameter and * appended to the exception message only in the case of an error.
- * - * @param expression the boolean expression to check + * + * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param value the value to append to the message when invalid * @throws IllegalArgumentException if expression is {@code false} @@ -130,16 +139,16 @@ public static void isTrue(boolean expression, String message, double value) { } /** - *Validate that the argument condition is {@code true}; otherwise + *
Validate that the argument condition is {@code true}; otherwise * throwing an exception with the specified message. This method is useful when - * validating according to an arbitrary boolean expression, such as validating a + * validating according to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.
* ** Validate.isTrue(i >= min && i <= max, "The value must be between %d and %d", min, max); * Validate.isTrue(myObject.isOk(), "The object is not okay");* - * @param expression the boolean expression to check + * @param expression the boolean expression to check * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message, null array not recommended * @throws IllegalArgumentException if expression is {@code false} @@ -154,19 +163,19 @@ public static void isTrue(boolean expression, String message, Object... values) } /** - *
Validate that the argument condition is {@code true}; otherwise - * throwing an exception. This method is useful when validating according - * to an arbitrary boolean expression, such as validating a + *
Validate that the argument condition is {@code true}; otherwise + * throwing an exception. This method is useful when validating according + * to an arbitrary boolean expression, such as validating a * primitive number or using your own custom validation expression.
* ** Validate.isTrue(i > 0); * Validate.isTrue(myObject.isOk());* - *
The message of the exception is "The validated expression is + *
The message of the exception is "The validated expression is * false".
- * - * @param expression the boolean expression to check + * + * @param expression the boolean expression to check * @throws IllegalArgumentException if expression is {@code false} * @see #isTrue(boolean, String, long) * @see #isTrue(boolean, String, double) @@ -182,14 +191,14 @@ public static void isTrue(boolean expression) { //--------------------------------------------------------------------------------- /** - *Validate that the specified argument is not {@code null}; + *
Validate that the specified argument is not {@code null}; * otherwise throwing an exception. * *
Validate.notNull(myObject, "The object must not be null");* - *
The message of the exception is "The validated object is + *
The message of the exception is "The validated object is * null".
- * + * * @paramValidate that the specified argument is not {@code null}; + *
Validate that the specified argument is not {@code null}; * otherwise throwing an exception with the specified message. * *
Validate.notNull(myObject, "The object must not be null");- * + * * @param
Validate that the specified argument array is neither {@code null} - * nor a length of zero (no elements); otherwise throwing an exception + *
Validate that the specified argument array is neither {@code null} + * nor a length of zero (no elements); otherwise throwing an exception * with the specified message. * *
Validate.notEmpty(myArray, "The array must not be empty");- * + * * @param
Validate that the specified argument array is neither {@code null} - * nor a length of zero (no elements); otherwise throwing an exception. + *
Validate that the specified argument array is neither {@code null} + * nor a length of zero (no elements); otherwise throwing an exception. * *
Validate.notEmpty(myArray);- * - *
The message in the exception is "The validated array is + * + *
The message in the exception is "The validated array is
* empty".
- *
+ *
* @param Validate that the specified argument collection is neither {@code null}
- * nor a size of zero (no elements); otherwise throwing an exception
+ * Validate that the specified argument collection is neither {@code null}
+ * nor a size of zero (no elements); otherwise throwing an exception
* with the specified message.
*
* Validate that the specified argument collection is neither {@code null}
- * nor a size of zero (no elements); otherwise throwing an exception.
+ * Validate that the specified argument collection is neither {@code null}
+ * nor a size of zero (no elements); otherwise throwing an exception.
*
* The message in the exception is "The validated collection is
+ *
+ * The message in the exception is "The validated collection is
* empty". Validate that the specified argument map is neither {@code null}
- * nor a size of zero (no elements); otherwise throwing an exception
+ * Validate that the specified argument map is neither {@code null}
+ * nor a size of zero (no elements); otherwise throwing an exception
* with the specified message.
*
* Validate that the specified argument map is neither {@code null}
- * nor a size of zero (no elements); otherwise throwing an exception.
+ * Validate that the specified argument map is neither {@code null}
+ * nor a size of zero (no elements); otherwise throwing an exception.
*
* The message in the exception is "The validated map is
+ *
+ * The message in the exception is "The validated map is
* empty". Validate that the specified argument character sequence is
- * neither {@code null} nor a length of zero (no characters);
+ * Validate that the specified argument character sequence is
+ * neither {@code null} nor a length of zero (no characters);
* otherwise throwing an exception with the specified message.
*
* Validate that the specified argument character sequence is
- * neither {@code null} nor a length of zero (no characters);
+ * Validate that the specified argument character sequence is
+ * neither {@code null} nor a length of zero (no characters);
* otherwise throwing an exception with the specified message.
*
* The message in the exception is "The validated
+ *
+ * The message in the exception is "The validated
* character sequence is empty". Validate that the specified argument character sequence is
+ * Validate that the specified argument character sequence is
* neither {@code null}, a length of zero (no characters), empty
- * nor whitespace; otherwise throwing an exception with the specified
+ * nor whitespace; otherwise throwing an exception with the specified
* message.
*
* Validate that the specified argument character sequence is
+ * Validate that the specified argument character sequence is
* neither {@code null}, a length of zero (no characters), empty
* nor whitespace; otherwise throwing an exception.
*
* The message in the exception is "The validated character
+ *
+ * The message in the exception is "The validated character
* sequence is blank". Validate that the specified argument array is neither
+ * Validate that the specified argument array is neither
* {@code null} nor contains any elements that are {@code null};
* otherwise throwing an exception with the specified message.
*
* If the array is {@code null}, then the message in the exception
+ *
+ * If the array is {@code null}, then the message in the exception
* is "The validated object is null". If the array has a {@code null} element, then the iteration
- * index of the invalid element is appended to the {@code values}
+ *
+ * If the array has a {@code null} element, then the iteration
+ * index of the invalid element is appended to the {@code values}
* argument. Validate that the specified argument array is neither
+ * Validate that the specified argument array is neither
* {@code null} nor contains any elements that are {@code null};
* otherwise throwing an exception.
*
* If the array is {@code null}, then the message in the exception
+ *
+ * If the array is {@code null}, then the message in the exception
* is "The validated object is null". If the array has a {@code null} element, then the message in the
- * exception is "The validated array contains null element at index:
+ * exception is "The validated array contains null element at index:
* " followed by the index. Validate that the specified argument iterable is neither
+ * Validate that the specified argument iterable is neither
* {@code null} nor contains any elements that are {@code null};
* otherwise throwing an exception with the specified message.
*
* If the iterable is {@code null}, then the message in the exception
+ *
+ * If the iterable is {@code null}, then the message in the exception
* is "The validated object is null". If the iterable has a {@code null} element, then the iteration
- * index of the invalid element is appended to the {@code values}
+ *
+ * If the iterable has a {@code null} element, then the iteration
+ * index of the invalid element is appended to the {@code values}
* argument. Validate that the specified argument iterable is neither
+ * Validate that the specified argument iterable is neither
* {@code null} nor contains any elements that are {@code null};
* otherwise throwing an exception.
*
* If the iterable is {@code null}, then the message in the exception
+ *
+ * If the iterable is {@code null}, then the message in the exception
* is "The validated object is null". If the array has a {@code null} element, then the message in the
- * exception is "The validated iterable contains null element at index:
+ * exception is "The validated iterable contains null element at index:
* " followed by the index. Validates that the index is within the bounds of the argument
+ * Validates that the index is within the bounds of the argument
* array; otherwise throwing an exception with the specified message. If the array is {@code null}, then the message of the exception
+ *
+ * If the array is {@code null}, then the message of the exception
* is "The validated object is null". Validates that the index is within the bounds of the argument
+ * Validates that the index is within the bounds of the argument
* array; otherwise throwing an exception. If the array is {@code null}, then the message of the exception
* is "The validated object is null". If the index is invalid, then the message of the exception is
- * "The validated array index is invalid: " followed by the
+ *
+ * If the index is invalid, then the message of the exception is
+ * "The validated array index is invalid: " followed by the
* index. Validates that the index is within the bounds of the argument
+ * Validates that the index is within the bounds of the argument
* collection; otherwise throwing an exception with the specified message. If the collection is {@code null}, then the message of the
+ *
+ * If the collection is {@code null}, then the message of the
* exception is "The validated object is null". Validates that the index is within the bounds of the argument
+ * Validates that the index is within the bounds of the argument
* collection; otherwise throwing an exception. If the index is invalid, then the message of the exception
- * is "The validated collection index is invalid: "
+ * If the index is invalid, then the message of the exception
+ * is "The validated collection index is invalid: "
* followed by the index. Validates that the index is within the bounds of the argument
- * character sequence; otherwise throwing an exception with the
+ * Validates that the index is within the bounds of the argument
+ * character sequence; otherwise throwing an exception with the
* specified message. If the character sequence is {@code null}, then the message
+ *
+ * If the character sequence is {@code null}, then the message
* of the exception is "The validated object is null". Validates that the index is within the bounds of the argument
+ * Validates that the index is within the bounds of the argument
* character sequence; otherwise throwing an exception. If the character sequence is {@code null}, then the message
- * of the exception is "The validated object is
+ * If the character sequence is {@code null}, then the message
+ * of the exception is "The validated object is
* null". If the index is invalid, then the message of the exception
- * is "The validated character sequence index is invalid: "
+ *
+ * If the index is invalid, then the message of the exception
+ * is "The validated character sequence index is invalid: "
* followed by the index. Validate that the stateful condition is {@code true}; otherwise
- * throwing an exception. This method is useful when validating according
- * to an arbitrary boolean expression, such as validating a
+ * Validate that the stateful condition is {@code true}; otherwise
+ * throwing an exception. This method is useful when validating according
+ * to an arbitrary boolean expression, such as validating a
* primitive number or using your own custom validation expression. The message of the exception is "The validated state is
+ * The message of the exception is "The validated state is
* false". Validate that the stateful condition is {@code true}; otherwise
+ * Validate that the stateful condition is {@code true}; otherwise
* throwing an exception with the specified message. This method is useful when
- * validating according to an arbitrary boolean expression, such as validating a
+ * validating according to an arbitrary boolean expression, such as validating a
* primitive number or using your own custom validation expression.Validate.notEmpty(myCollection, "The collection must not be empty");
- *
+ *
* @param Validate.notEmpty(myCollection);
- *
- * Validate.notEmpty(myMap, "The map must not be empty");
- *
+ *
* @param Validate.notEmpty(myMap);
- *
- * Validate.notEmpty(myString, "The string must not be empty");
- *
+ *
* @param Validate.notEmpty(myString);
- *
- * Validate.notBlank(myString, "The string must not be blank");
- *
+ *
* @param Validate.notBlank(myString);
- *
- * Validate.noNullElements(myArray, "The array contain null at position %d");
- *
- * Validate.noNullElements(myArray);
- *
- * Validate.noNullElements(myCollection, "The collection contains null at position %d");
- *
- * Validate.noNullElements(myCollection);
- *
- * Validate.validIndex(myArray, 2, "The array index is invalid: ");
- *
- * Validate.validIndex(myArray, 2);
*
* Validate.validIndex(myCollection, 2, "The collection index is invalid: ");
- *
- * Validate.validIndex(myCollection, 2);
*
- * Validate.validIndex(myStr, 2, "The string index is invalid: ");
- *
- * Validate.validIndex(myStr, 2);
*
- *
* Validate.validState(field > 0);
* Validate.validState(this.isOk());
*
- * Validate.validState(this.isOk(), "The state is not OK: %s", myObject);
*
- * @param expression the boolean expression to check
+ * @param expression the boolean expression to check
* @param message the {@link String#format(String, Object...)} exception message if invalid, not null
* @param values the optional values for the formatted exception message, null array not recommended
* @throws IllegalStateException if expression is {@code false}
* @see #validState(boolean)
- *
+ *
* @since 3.0
*/
public static void validState(boolean expression, String message, Object... values) {
@@ -832,14 +841,14 @@ public static void validState(boolean expression, String message, Object... valu
* 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, not null * @param pattern the regular expression pattern, not null * @throws IllegalArgumentException if the character sequence does not match the pattern * @see #matchesPattern(CharSequence, String, String, Object...) - * + * * @since 3.0 */ public static void matchesPattern(CharSequence input, String pattern) { @@ -853,16 +862,16 @@ public static void matchesPattern(CharSequence input, String pattern) { * 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, not null * @param pattern the regular expression pattern, not null * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message, null array not recommended * @throws IllegalArgumentException if the character sequence does not match the pattern * @see #matchesPattern(CharSequence, String) - * + * * @since 3.0 */ public static void matchesPattern(CharSequence input, String pattern, String message, Object... values) { @@ -879,13 +888,14 @@ public static void matchesPattern(CharSequence input, String pattern, String mes * inclusive values specified; otherwise, throws an exception. * *Validate.inclusiveBetween(0, 2, 1);- * + * + * @param
Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");- * + * + * @param
Validate.inclusiveBetween(0, 2, 1);- * + * + * @param
Validate.inclusiveBetween(0, 2, 1, "Not in boundaries");- * + * + * @param
Validate that the argument is an instance of the specified class; otherwise * throwing an exception. This method is useful when validating according to an arbitrary * class
- * + * *Validate.isInstanceOf(OkClass.class, object);- * + * *
The message of the exception is "The validated object is not an instance of" * followed by the name of the class
- * + * * @param type the class the object must be validated against, not null * @param obj the object to check, null throws an exception * @throws IllegalArgumentException if argument is not of specified class * @see #isInstanceOf(Class, Object, String, Object...) - * + * * @since 3.0 */ public static void isInstanceOf(Class> type, Object obj) { @@ -991,18 +1004,19 @@ public static void isInstanceOf(Class> type, Object obj) { /** *Validate that the argument is an instance of the specified class; otherwise - * throwing an exception with the specified message. This method is useful when + * throwing an exception with the specified message. This method is useful when * validating according to an arbitrary class
- * - *Validate.isInstanceOf(OkClass.classs, object, "Wrong class, object is of class %s", object.getClass().getName());- * + * + *
Validate.isInstanceOf(OkClass.classs, object, "Wrong class, object is of class %s", + * object.getClass().getName());+ * * @param type the class the object must be validated against, not null * @param obj the object to check, null throws an exception * @param message the {@link String#format(String, Object...)} exception message if invalid, not null * @param values the optional values for the formatted exception message, null array not recommended * @throws IllegalArgumentException if argument is not of specified class * @see #isInstanceOf(Class, Object) - * + * * @since 3.0 */ public static void isInstanceOf(Class> type, Object obj, String message, Object... values) { @@ -1018,17 +1032,17 @@ public static void isInstanceOf(Class> type, Object obj, String message, Objec *
Validate that the argument can be converted to the specified class; otherwise * throwing an exception with the specified message. This method is useful when * validating if there will be no casting errors.
- * + * *Validate.isAssignableFrom(SuperClass.class, object.getClass());- * + * *
The message of the exception is "The validated object can not be converted to the" * followed by the name of the class and "class"
- * + * * @param superType the class the class must be validated against, not null * @param type the class to check, not null * @throws IllegalArgumentException if argument can not be converted to the specified class * @see #isAssignableFrom(Class, Class, String, Object...) - * + * * @since 3.0 */ public static void isAssignableFrom(Class> superType, Class> type) { @@ -1041,12 +1055,12 @@ public static void isAssignableFrom(Class> superType, Class> type) { *Validate that the argument can be converted to the specified class; otherwise * throwing an exception. This method is useful when validating if there will be no * casting errors.
- * + * *Validate.isAssignableFrom(SuperClass.class, object.getClass());- * + * *
The message of the exception is "The validated object can not be converted to the" * followed by the name of the class and "class"
- * + * * @param superType the class the class must be validated against, not null * @param type the class to check, not null * @param message the {@link String#format(String, Object...)} exception message if invalid, not null