diff --git a/src/main/java/org/apache/commons/lang3/Validate.java b/src/main/java/org/apache/commons/lang3/Validate.java index 8df360484..5e83b7075 100644 --- a/src/main/java/org/apache/commons/lang3/Validate.java +++ b/src/main/java/org/apache/commons/lang3/Validate.java @@ -48,18 +48,19 @@ import java.util.Map; */ public class Validate { - private static final String DEFAULT_IS_NULL_EXCEPTION_MESSAGE = "The validated object is null"; - private static final String DEFAULT_IS_TRUE_EXCEPTION_MESSAGE = "The validated expression is false"; - private static final String DEFAULT_NO_NULL_ELEMENTS_ARRAY_EXCEPTION_MESSAGE = "The validated array contains null element at index: %d"; - private static final String DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EXCEPTION_MESSAGE = "The validated collection contains null element at index: %d"; - private static final String DEFAULT_NOT_BLANK_EXCEPTION_MESSAGE = "The validated character sequence is blank"; - private static final String DEFAULT_NOT_EMPTY_ARRAY_EXCEPTION_MESSAGE = "The validated array is empty"; - private static final String DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EXCEPTION_MESSAGE = "The validated character sequence is empty"; - private static final String DEFAULT_NOT_EMPTY_COLLECTION_EXCEPTION_MESSAGE = "The validated collection is empty"; - private static final String DEFAULT_NOT_EMPTY_MAP_EXCEPTION_MESSAGE = "The validated map is empty"; - private static final String DEFAULT_VALID_INDEX_ARRAY_EXCEPTION_MESSAGE = "The validated array index is invalid: %d"; - private static final String DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EXCEPTION_MESSAGE = "The validated character sequence index is invalid: %d"; - private static final String DEFAULT_VALID_INDEX_COLLECTION_EXCEPTION_MESSAGE = "The validated collection index is invalid: %d"; + 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_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_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_STATE_EX_MESSAGE = "The validated state is false"; /** * Constructor. This class should not normally be instantiated. @@ -71,32 +72,6 @@ public class Validate { // isTrue //--------------------------------------------------------------------------------- - /** - *
Validate that the argument condition is 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
- * primitive number or using your own custom validation expression.
Validate.isTrue(myObject.isOk(), "The object is not OK: %s", myObject);- * - *
For performance reasons, the object 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 exception message if invalid - * @param value the value to append to the message when invalid - * @throws IllegalArgumentException if expression isfalse
- * @see #isTrue(boolean)
- * @see #isTrue(boolean, String, long)
- * @see #isTrue(boolean, String, double)
- * @see #isTrue(boolean, String, Object...)
- */
- public static void isTrue(boolean expression, String message, Object value) {
- if (expression == false) {
- throw new IllegalArgumentException(String.format(message, value));
- }
- }
-
/**
* Validate that the argument condition is true
; otherwise
* throwing an exception with the specified message. This method is useful when
@@ -114,7 +89,6 @@ public class Validate {
* @throws IllegalArgumentException if expression is false
* @see #isTrue(boolean)
* @see #isTrue(boolean, String, double)
- * @see #isTrue(boolean, String, Object)
* @see #isTrue(boolean, String, Object...)
*/
public static void isTrue(boolean expression, String message, long value) {
@@ -140,7 +114,6 @@ public class Validate {
* @throws IllegalArgumentException if expression is false
* @see #isTrue(boolean)
* @see #isTrue(boolean, String, long)
- * @see #isTrue(boolean, String, Object)
* @see #isTrue(boolean, String, Object...)
*/
public static void isTrue(boolean expression, String message, double value) {
@@ -159,9 +132,6 @@ public class Validate {
* 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");
*
- *
For performance reasons, the message string should not involve a string append, - * instead use the {@link #isTrue(boolean, String, Object)} method.
- * * @param expression the boolean expression to check * @param message the exception message if invalid * @param values the optional values for the formatted exception message @@ -169,7 +139,6 @@ public class Validate { * @see #isTrue(boolean) * @see #isTrue(boolean, String, long) * @see #isTrue(boolean, String, double) - * @see #isTrue(boolean, String, Object) */ public static void isTrue(boolean expression, String message, Object... values) { if (expression == false) { @@ -194,12 +163,11 @@ public class Validate { * @throws IllegalArgumentException if expression isfalse
* @see #isTrue(boolean, String, long)
* @see #isTrue(boolean, String, double)
- * @see #isTrue(boolean, String, Object)
* @see #isTrue(boolean, String, Object...)
*/
public static void isTrue(boolean expression) {
if (expression == false) {
- throw new IllegalArgumentException(DEFAULT_IS_TRUE_EXCEPTION_MESSAGE);
+ throw new IllegalArgumentException(DEFAULT_IS_TRUE_EX_MESSAGE);
}
}
@@ -222,7 +190,7 @@ public class Validate {
* @see #notNull(Object, String, Object...)
*/
public static Validate that the stateful condition is 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.validState(field > 0); + * Validate.validState(this.isOk());+ * + *
The message of the exception is "The validated state is + * false".
+ * + * @param expression the boolean expression to check + * @throws IllegalStateException if expression isfalse
+ * @see #validState(boolean, String, Object...)
+ */
+ public static void validState(boolean expression) {
+ if (expression == false) {
+ throw new IllegalArgumentException(DEFAULT_VALID_STATE_EX_MESSAGE);
+ }
+ }
+
+ /**
+ * Validate that the stateful condition is 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
+ * primitive number or using your own custom validation expression.
Validate.validState(this.isOk(), "The state is not OK: %s", myObject);+ * + * @param expression the boolean expression to check + * @param message the exception message if invalid + * @param values the optional values for the formatted exception message + * @throws IllegalStateException if expression is
false
+ * @see #validState(boolean)
+ */
+ public static void validState(boolean expression, String message, Object... values) {
+ if (expression == false) {
+ throw new IllegalStateException(String.format(message, values));
+ }
}
}