diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 8043fb944..9a2560759 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@
Validates that the specified argument is not {@code NaN}; otherwise + * throwing an exception.
+ * + *Validate.notNaN(myDouble);+ * + *
The message of the exception is "The validated value is not a + * number".
+ * + * @param value the value to validate + * @throws IllegalArgumentException if the value is not a number + * @see #notNaN(double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void notNaN(final double value) { + notNaN(value, DEFAULT_NOT_NAN_EX_MESSAGE); + } + + /** + *Validates that the specified argument is not {@code NaN}; otherwise + * throwing an exception with the specified message.
+ * + *Validate.notNaN(myDouble, "The value must be a number");+ * + * @param value the value to validate + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if the value is not a number + * @see #notNaN(double) + * + * @since 3.5 + */ + public static void notNaN(final double value, final String message, final Object... values) { + if (Double.isNaN(value)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // finite + //--------------------------------------------------------------------------------- + + /** + *
Validates that the specified argument is not infinite or {@code NaN}; + * otherwise throwing an exception.
+ * + *Validate.finite(myDouble);+ * + *
The message of the exception is "The value is invalid: %f".
+ * + * @param value the value to validate + * @throws IllegalArgumentException if the value is infinite or {@code NaN} + * @see #finite(double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void finite(final double value) { + finite(value, DEFAULT_FINITE_EX_MESSAGE, value); + } + + /** + *Validates that the specified argument is not infinite or {@code NaN}; + * otherwise throwing an exception with the specified message.
+ * + *Validate.finite(myDouble, "The argument must contain a numeric value");+ * + * @param value the value to validate + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if the value is infinite or {@code NaN} + * @see #finite(double) + * + * @since 3.5 + */ + public static void finite(final double value, final String message, final Object... values) { + if (Double.isNaN(value) || Double.isInfinite(value)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // greater + //--------------------------------------------------------------------------------- + + /** + *
Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception.
+ * + *Validate.greaterObj(myObject, refObject);+ * + *
The message of the exception is "The value {@code value} is not + * greater than {@code min}".
+ * + * @paramValidates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception with the specified message.
+ * + *Validate.greaterObj(myObject, refObject, "The value must be greater than the reference");+ * + * @param
Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception.
+ * + *Validate.greater(myLong, 0);+ * + *
The message of the exception is "The value {@code value} is not + * greater than {@code min}".
+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greater(final long value, final long min) { + greater(value, min, DEFAULT_GREATER_EX_MESSAGE, value, min); + } + + /** + *Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception with the specified message.
+ * + *Validate.greater(myLong, 0);+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(long, long) + * + * @since 3.5 + */ + public static void greater(final long value, final long min, final String message, final Object... values) { + if (value <= min) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *
Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.greater(myDouble, 0.0);+ * + *
The message of the exception is "The value {@code value} is not + * greater than {@code min}".
+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greater(final double value, final double min) { + greater(value, min, DEFAULT_GREATER_EX_MESSAGE, value, min); + } + + /** + *Validates that the specified argument is strictly greater than a given + * reference; otherwise throwing an exception with the specified message.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.greater(myDouble, 0.0);+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than or equal to {@code min} + * @see #greater(double, double) + * + * @since 3.5 + */ + public static void greater(final double value, final double min, final String message, final Object... values) { + if (!(value > min)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // greaterOrEqual + //--------------------------------------------------------------------------------- + + /** + *
Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *Validate.greaterOrEqualObj(myObject, refObject);+ * + *
The message of the exception is "The value {@code value} is not + * greater than or equal to {@code min}".
+ * + * @paramValidates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *Validate.greaterOrEqualObj(myObject, refObject, "The value must be greater than the reference");+ * + * @param
Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *Validate.greaterOrEqual(myLong, 0);+ * + *
The message of the exception is "The value {@code value} is not + * greater than or equal to {@code min}".
+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greaterOrEqual(final long value, final long min) { + greaterOrEqual(value, min, DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE, value, min); + } + + /** + *Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.
+ * + *Validate.greaterOrEqual(myLong, 0);+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(long, long) + * + * @since 3.5 + */ + public static void greaterOrEqual(final long value, final long min, final String message, final Object... values) { + if (value < min) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *
Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.greaterOrEqual(myDouble, 0.0);+ * + *
The message of the exception is "The value {@code value} is not + * greater than or equal to {@code min}".
+ * + * @param value the value to validate + * @param min the reference value + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void greaterOrEqual(final double value, final double min) { + greaterOrEqual(value, min, DEFAULT_GREATER_OR_EQUAL_EX_MESSAGE, value, min); + } + + /** + *Validates that the specified argument is greater than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.greaterOrEqual(myDouble, 0.0);+ * + * @param value the value to validate + * @param min the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is smaller than {@code min} + * @see #greaterOrEqual(double, double) + * + * @since 3.5 + */ + public static void greaterOrEqual(final double value, final double min, final String message, final Object... values) { + if (!(value >= min)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // smaller + //--------------------------------------------------------------------------------- + + /** + *
Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception.
+ * + *Validate.smallerObj(myObject, refObject);+ * + *
The message of the exception is "The value {@code value} is not + * smaller than {@code max}".
+ * + * @paramValidates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception with the specified message.
+ * + *Validate.smallerObj(myObject, refObject, "The value must be greater than the reference");+ * + * @param
Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception.
+ * + *Validate.smaller(myLong, 0);+ * + *
The message of the exception is "The value {@code value} is not + * smaller than {@code max}".
+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smaller(final long value, final long max) { + smaller(value, max, DEFAULT_SMALLER_EX_MESSAGE, value, max); + } + + /** + *Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception with the specified message.
+ * + *Validate.smaller(myLong, 0);+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(long, long) + * + * @since 3.5 + */ + public static void smaller(final long value, final long max, final String message, final Object... values) { + if (value >= max) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *
Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.smaller(myDouble, 0.0);+ * + *
The message of the exception is "The value {@code value} is not + * smaller than {@code max}".
+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smaller(final double value, final double max) { + smaller(value, max, DEFAULT_SMALLER_EX_MESSAGE, value, max); + } + + /** + *Validates that the specified argument is strictly smaller than a given + * reference; otherwise throwing an exception with the specified message.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.smaller(myDouble, 0.0);+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than or equal to {@code max} + * @see #smaller(double, double) + * + * @since 3.5 + */ + public static void smaller(final double value, final double max, final String message, final Object... values) { + if (!(value < max)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // smallerOrEqual + //--------------------------------------------------------------------------------- + + /** + *
Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *Validate.smallerOrEqualObj(myObject, refObject);+ * + *
The message of the exception is "The value {@code value} is not + * smaller than or equal to {@code max}".
+ * + * @paramValidates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.
+ * + *Validate.smallerOrEqualObj(myObject, refObject, "The value must be greater than the reference");+ * + * @param
Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *Validate.smallerOrEqual(myLong, 0);+ * + *
The message of the exception is "The value {@code value} is not + * smaller than or equal to {@code max}".
+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(long, long, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smallerOrEqual(final long value, final long max) { + smallerOrEqual(value, max, DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE, value, max); + } + + /** + *Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.
+ * + *Validate.smallerOrEqual(myLong, 0);+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(long, long) + * + * @since 3.5 + */ + public static void smallerOrEqual(final long value, final long max, final String message, final Object... values) { + if (value > max) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *
Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.smallerOrEqual(myDouble, 0.0);+ * + *
The message of the exception is "The value {@code value} is not + * smaller than or equal to {@code max}".
+ * + * @param value the value to validate + * @param max the reference value + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(double, double, java.lang.String, java.lang.Object...) + * + * @since 3.5 + */ + public static void smallerOrEqual(final double value, final double max) { + smallerOrEqual(value, max, DEFAULT_SMALLER_OR_EQUAL_EX_MESSAGE, value, max); + } + + /** + *Validates that the specified argument is smaller than, or equal to, a + * given reference; otherwise throwing an exception with the specified message.
+ * + *If {@code min} or {@code value} is {@code NaN}, the test will fail and + * the exception will be thrown.
+ * + *Validate.smallerOrEqual(myDouble, 0.0);+ * + * @param value the value to validate + * @param max the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is greater than {@code max} + * @see #smallerOrEqual(double, double) + * + * @since 3.5 + */ + public static void smallerOrEqual(final double value, final double max, final String message, final Object... values) { + if (!(value <= max)) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + // different + //--------------------------------------------------------------------------------- + + /** + *
Validates that the specified argument is different from a given value + * (reference); otherwise throwing an exception.
+ * + *Two objects are considered different if + * {@code value.compareTo(reference) != 0}
+ * + *Validate.differentObj(myObject, refObject);+ * + *
The message of the exception is "The value {@code value} is + * invalid".
+ * + * @paramValidates that the specified argument is different from a given value + * (reference); otherwise throwing an exception with the specified message.
+ * + *Two objects are considered different if + * {@code value.compareTo(reference) != 0}
+ * + *Validate.differentObj(myObject, refObject, "The value is invalid");+ * + * @param
Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception.
+ * + *Validate.different(myLong, 0);+ * + *
The message of the exception is "The value {@code value} is + * invalid".
+ * + * @param value the value to validate + * @param reference the reference value + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final long value, final long reference) { + different(value, reference, DEFAULT_DIFFERENT_EX_MESSAGE, value); + } + + /** + *Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception with the specified message.
+ * + *Validate.different(myLong, 0, "The value is invalid");+ * + * @param value the value to validate + * @param reference the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final long value, final long reference, final String message, final Object... values) { + if (value == reference) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + + /** + *
Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception.
+ * + *If {@code value} or {@code reference} is {@code NaN}, no exception will be thrown.
+ * + *Validate.different(myDouble, 0.0);+ * + *
The message of the exception is "The value {@code value} is + * invalid".
+ * + * @param value the value to validate + * @param reference the reference value + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final double value, final double reference) { + different(value, reference, DEFAULT_DIFFERENT_EX_MESSAGE, value); + } + + /** + *Validates that the specified argument is not equal to a given value + * (reference); otherwise throwing an exception with the specified message.
+ * + *If {@code value} or {@code reference} is {@code NaN}, no exception will be thrown.
+ * + *Validate.different(myDouble, 0.0, "The value is invalid");+ * + * @param value the value to validate + * @param reference the reference value + * @param message the {@link String#format(String, Object...)} exception message if invalid, not null + * @param values the optional values for the formatted exception message + * @throws IllegalArgumentException if {@code value} is equal to {@code reference} + * + * @since 3.5 + */ + public static void different(final double value, final double reference, final String message, final Object... values) { + if (value == reference) { + throw new IllegalArgumentException(String.format(message, values)); + } + } + // inclusiveBetween //--------------------------------------------------------------------------------- @@ -1156,7 +1921,7 @@ public class Validate { /** * Validates that the argument is an instance of the specified class, if not throws an exception. - * + * *
This method is useful when validating according to an arbitrary class
* *Validate.isInstanceOf(OkClass.class, object);@@ -1207,7 +1972,7 @@ public class Validate { /** * Validates that the argument can be converted to the specified class, if not, throws an exception. - * + * *
This method is useful when validating that there will be no casting errors.
* *Validate.isAssignableFrom(SuperClass.class, object.getClass());@@ -1231,7 +1996,7 @@ public class Validate { /** * Validates that the argument can be converted to the specified class, if not throws an exception. - * + * *
This method is useful when validating if there will be no casting errors.
* *Validate.isAssignableFrom(SuperClass.class, object.getClass());diff --git a/src/test/java/org/apache/commons/lang3/ValidateTest.java b/src/test/java/org/apache/commons/lang3/ValidateTest.java index 954179c7e..d577ee65e 100644 --- a/src/test/java/org/apache/commons/lang3/ValidateTest.java +++ b/src/test/java/org/apache/commons/lang3/ValidateTest.java @@ -41,7 +41,7 @@ import org.junit.Test; * Unit tests {@link org.apache.commons.lang3.Validate}. */ public class ValidateTest { - + //----------------------------------------------------------------------- @Test public void testIsTrue1() { @@ -114,7 +114,7 @@ public class ValidateTest { } catch (final NullPointerException ex) { assertEquals("The validated object is null", ex.getMessage()); } - + final String str = "Hi"; final String testStr = Validate.notNull(str); assertSame(str, testStr); @@ -131,7 +131,7 @@ public class ValidateTest { } catch (final NullPointerException ex) { assertEquals("MSG", ex.getMessage()); } - + final String str = "Hi"; final String testStr = Validate.notNull(str, "Message"); assertSame(str, testStr); @@ -154,7 +154,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("The validated array is empty", ex.getMessage()); } - + final String[] array = new String[] {"hi"}; final String[] test = Validate.notEmpty(array); assertSame(array, test); @@ -176,7 +176,7 @@ public class ValidateTest { } catch (final IllegalArgumentException ex) { assertEquals("MSG", ex.getMessage()); } - + final String[] array = new String[] {"hi"}; final String[] test = Validate.notEmpty(array, "Message"); assertSame(array, test); @@ -201,7 +201,7 @@ public class ValidateTest { } coll.add(Integer.valueOf(8)); Validate.notEmpty(coll); - + final Collection