Return-Path:
Delivered-To: apmail-commons-commits-archive@minotaur.apache.org
Received: (qmail 92513 invoked from network); 3 Jan 2010 06:39:35 -0000
Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3)
by minotaur.apache.org with SMTP; 3 Jan 2010 06:39:34 -0000
Received: (qmail 93814 invoked by uid 500); 3 Jan 2010 06:39:34 -0000
Delivered-To: apmail-commons-commits-archive@commons.apache.org
Received: (qmail 93712 invoked by uid 500); 3 Jan 2010 06:39:33 -0000
Mailing-List: contact commits-help@commons.apache.org; run by ezmlm
Precedence: bulk
List-Help:
List-Unsubscribe:
List-Post:
List-Id:
Reply-To: dev@commons.apache.org
Delivered-To: mailing list commits@commons.apache.org
Received: (qmail 93703 invoked by uid 99); 3 Jan 2010 06:39:33 -0000
Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230)
by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 03 Jan 2010 06:39:33 +0000
X-ASF-Spam-Status: No, hits=-2000.0 required=10.0
tests=ALL_TRUSTED
X-Spam-Check-By: apache.org
Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4)
by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 03 Jan 2010 06:39:26 +0000
Received: by eris.apache.org (Postfix, from userid 65534)
id DF5262388A18; Sun, 3 Jan 2010 06:39:04 +0000 (UTC)
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Subject: svn commit: r895334 -
/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
Date: Sun, 03 Jan 2010 06:39:04 -0000
To: commits@commons.apache.org
From: pbenedict@apache.org
X-Mailer: svnmailer-1.0.8
Message-Id: <20100103063904.DF5262388A18@eris.apache.org>
X-Virus-Checked: Checked by ClamAV on apache.org
Author: pbenedict
Date: Sun Jan 3 06:39:04 2010
New Revision: 895334
URL: http://svn.apache.org/viewvc?rev=895334&view=rev
Log:
LANG-559: Added validState()
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java?rev=895334&r1=895333&r2=895334&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java (original)
+++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/Validate.java Sun Jan 3 06:39:04 2010
@@ -48,18 +48,19 @@
*/
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.
@@ -77,32 +78,6 @@
* 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 is false
- * @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
- * 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
@@ -114,7 +89,6 @@
* @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 @@
* @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 @@
* 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 @@
* @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 @@
* @throws IllegalArgumentException if expression is false
* @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 @@
* @see #notNull(Object, String, Object...)
*/
public static T notNull(T object) {
- return notNull(object, DEFAULT_IS_NULL_EXCEPTION_MESSAGE);
+ return notNull(object, DEFAULT_IS_NULL_EX_MESSAGE);
}
/**
@@ -291,7 +259,7 @@
* @see #notEmpty(Object[], String, Object...)
*/
public static T[] notEmpty(T[] array) {
- return notEmpty(array, DEFAULT_NOT_EMPTY_ARRAY_EXCEPTION_MESSAGE);
+ return notEmpty(array, DEFAULT_NOT_EMPTY_ARRAY_EX_MESSAGE);
}
// notEmpty collection
@@ -339,7 +307,7 @@
* @see #notEmpty(Collection, String, Object...)
*/
public static > T notEmpty(T collection) {
- return notEmpty(collection, DEFAULT_NOT_EMPTY_COLLECTION_EXCEPTION_MESSAGE);
+ return notEmpty(collection, DEFAULT_NOT_EMPTY_COLLECTION_EX_MESSAGE);
}
// notEmpty map
@@ -387,7 +355,7 @@
* @see #notEmpty(Map, String, Object...)
*/
public static > T notEmpty(T map) {
- return notEmpty(map, DEFAULT_NOT_EMPTY_MAP_EXCEPTION_MESSAGE);
+ return notEmpty(map, DEFAULT_NOT_EMPTY_MAP_EX_MESSAGE);
}
// notEmpty string
@@ -436,7 +404,7 @@
* @see #notEmpty(CharSequence, String, Object...)
*/
public static T notEmpty(T chars) {
- return notEmpty(chars, DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EXCEPTION_MESSAGE);
+ return notEmpty(chars, DEFAULT_NOT_EMPTY_CHAR_SEQUENCE_EX_MESSAGE);
}
// notBlank string
@@ -486,7 +454,7 @@
* @see #notBlank(CharSequence, String, Object...)
*/
public static T notBlank(T chars) {
- return notBlank(chars, DEFAULT_NOT_BLANK_EXCEPTION_MESSAGE);
+ return notBlank(chars, DEFAULT_NOT_BLANK_EX_MESSAGE);
}
// notNullElements array
@@ -546,7 +514,7 @@
* @see #noNullElements(Object[], String, Object...)
*/
public static T[] noNullElements(T[] array) {
- return noNullElements(array, DEFAULT_NO_NULL_ELEMENTS_ARRAY_EXCEPTION_MESSAGE);
+ return noNullElements(array, DEFAULT_NO_NULL_ELEMENTS_ARRAY_EX_MESSAGE);
}
// notNullElements collection
@@ -607,7 +575,7 @@
* @see #noNullElements(Collection, String, Object...)
*/
public static > T noNullElements(T iterable) {
- return noNullElements(iterable, DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EXCEPTION_MESSAGE);
+ return noNullElements(iterable, DEFAULT_NO_NULL_ELEMENTS_COLLECTION_EX_MESSAGE);
}
// validIndex array
@@ -661,7 +629,7 @@
* @see #validIndex(Object[], int, String, Object...)
*/
public static T[] validIndex(T[] array, int index) {
- return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EXCEPTION_MESSAGE, Integer.valueOf(index));
+ return validIndex(array, index, DEFAULT_VALID_INDEX_ARRAY_EX_MESSAGE, Integer.valueOf(index));
}
// validIndex collection
@@ -712,7 +680,7 @@
* @see #validIndex(Collection, int, String, Object...)
*/
public static > T validIndex(T collection, int index) {
- return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EXCEPTION_MESSAGE, Integer.valueOf(index));
+ return validIndex(collection, index, DEFAULT_VALID_INDEX_COLLECTION_EX_MESSAGE, Integer.valueOf(index));
}
// validIndex string
@@ -768,7 +736,50 @@
* @see #validIndex(CharSequence, int, String, Object...)
*/
public static T validIndex(T chars, int index) {
- return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EXCEPTION_MESSAGE, Integer.valueOf(index));
+ return validIndex(chars, index, DEFAULT_VALID_INDEX_CHAR_SEQUENCE_EX_MESSAGE, Integer.valueOf(index));
+ }
+
+ /**
+ * 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 is false
+ * @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));
+ }
}
}