LANG-834: Validate: add inclusiveBetween and exclusiveBetween overloads for primitives types. Initial patch provided by Sebb.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1557509 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
28daf04ae2
commit
11a15c6394
|
@ -22,6 +22,7 @@
|
|||
<body>
|
||||
|
||||
<release version="3.3" date="TBA" description="Bugfix and Feature release">
|
||||
<action issue="LANG-834" type="add" dev="britter">Validate: add inclusiveBetween and exclusiveBetween overloads for primitives types</action>
|
||||
<action issue="LANG-900" type="add" dev="britter" due-to="Duncan Jones">New RandomUtils class</action>
|
||||
<action issue="LANG-915" type="fix" dev="britter" due-to="Sergio Fernández">Wrong locale handling in LocaleUtils.toLocale()</action>
|
||||
</release>
|
||||
|
|
|
@ -926,6 +926,94 @@ public class Validate {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* inclusive values specified; otherwise, throws an exception.
|
||||
*
|
||||
* <pre>Validate.inclusiveBetween(0, 2, 1);</pre>
|
||||
*
|
||||
* @param start the inclusive start value
|
||||
* @param end the inclusive end value
|
||||
* @param value the value to validate
|
||||
* @throws IllegalArgumentException if the value falls outside the boundaries (inclusive)
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public static void inclusiveBetween(long start, long end, long value) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value < start || value > end) {
|
||||
throw new IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* inclusive values specified; otherwise, throws an exception with the
|
||||
* specified message.
|
||||
*
|
||||
* <pre>Validate.inclusiveBetween(0, 2, 1, "Not in range");</pre>
|
||||
*
|
||||
* @param start the inclusive start value
|
||||
* @param end the inclusive end value
|
||||
* @param value the value to validate
|
||||
* @param message the exception message if invalid, not null
|
||||
*
|
||||
* @throws IllegalArgumentException if the value falls outside the boundaries
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
public static void inclusiveBetween(long start, long end, long value, String message) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value < start || value > end) {
|
||||
throw new IllegalArgumentException(String.format(message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* inclusive values specified; otherwise, throws an exception.
|
||||
*
|
||||
* <pre>Validate.inclusiveBetween(0.1, 2.1, 1.1);</pre>
|
||||
*
|
||||
* @param start the inclusive start value
|
||||
* @param end the inclusive end value
|
||||
* @param value the value to validate
|
||||
* @throws IllegalArgumentException if the value falls outside the boundaries (inclusive)
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public static void inclusiveBetween(double start, double end, double value) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value < start || value > end) {
|
||||
throw new IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* inclusive values specified; otherwise, throws an exception with the
|
||||
* specified message.
|
||||
*
|
||||
* <pre>Validate.inclusiveBetween(0.1, 2.1, 1.1, "Not in range");</pre>
|
||||
*
|
||||
* @param start the inclusive start value
|
||||
* @param end the inclusive end value
|
||||
* @param value the value to validate
|
||||
* @param message the exception message if invalid, not null
|
||||
*
|
||||
* @throws IllegalArgumentException if the value falls outside the boundaries
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
public static void inclusiveBetween(double start, double end, double value, String message) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value < start || value > end) {
|
||||
throw new IllegalArgumentException(String.format(message));
|
||||
}
|
||||
}
|
||||
|
||||
// exclusiveBetween
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
|
@ -976,6 +1064,94 @@ public class Validate {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* exclusive values specified; otherwise, throws an exception.
|
||||
*
|
||||
* <pre>Validate.exclusiveBetween(0, 2, 1);</pre>
|
||||
*
|
||||
* @param start the exclusive start value
|
||||
* @param end the exclusive end value
|
||||
* @param value the value to validate
|
||||
* @throws IllegalArgumentException if the value falls out of the boundaries
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public static void exclusiveBetween(long start, long end, long value) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value <= start || value >= end) {
|
||||
throw new IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* exclusive values specified; otherwise, throws an exception with the
|
||||
* specified message.
|
||||
*
|
||||
* <pre>Validate.exclusiveBetween(0, 2, 1, "Not in range");</pre>
|
||||
*
|
||||
* @param start the exclusive start value
|
||||
* @param end the exclusive end value
|
||||
* @param value the value to validate
|
||||
* @param message the exception message if invalid, not null
|
||||
*
|
||||
* @throws IllegalArgumentException if the value falls outside the boundaries
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
public static void exclusiveBetween(long start, long end, long value, String message) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value <= start || value >= end) {
|
||||
throw new IllegalArgumentException(String.format(message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* exclusive values specified; otherwise, throws an exception.
|
||||
*
|
||||
* <pre>Validate.exclusiveBetween(0.1, 2.1, 1.1);</pre>
|
||||
*
|
||||
* @param start the exclusive start value
|
||||
* @param end the exclusive end value
|
||||
* @param value the value to validate
|
||||
* @throws IllegalArgumentException if the value falls out of the boundaries
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
@SuppressWarnings("boxing")
|
||||
public static void exclusiveBetween(double start, double end, double value) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value <= start || value >= end) {
|
||||
throw new IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, value, start, end));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the specified primitive value falls between the two
|
||||
* exclusive values specified; otherwise, throws an exception with the
|
||||
* specified message.
|
||||
*
|
||||
* <pre>Validate.exclusiveBetween(0.1, 2.1, 1.1, "Not in range");</pre>
|
||||
*
|
||||
* @param start the exclusive start value
|
||||
* @param end the exclusive end value
|
||||
* @param value the value to validate
|
||||
* @param message the exception message if invalid, not null
|
||||
*
|
||||
* @throws IllegalArgumentException if the value falls outside the boundaries
|
||||
*
|
||||
* @since 3.3
|
||||
*/
|
||||
public static void exclusiveBetween(double start, double end, double value, String message) {
|
||||
// TODO when breaking BC, consider returning value
|
||||
if (value <= start || value >= end) {
|
||||
throw new IllegalArgumentException(String.format(message));
|
||||
}
|
||||
}
|
||||
|
||||
// isInstanceOf
|
||||
//---------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -835,6 +835,29 @@ public class ValidateTest {
|
|||
public void testInclusiveBetween()
|
||||
{
|
||||
Validate.inclusiveBetween("a", "c", "b");
|
||||
try {
|
||||
Validate.inclusiveBetween("0", "5", "6");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("The value 6 is not in the specified inclusive range of 0 to 5", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInclusiveBetween_withMessage()
|
||||
{
|
||||
Validate.inclusiveBetween("a", "c", "b", "Error");
|
||||
try {
|
||||
Validate.inclusiveBetween("0", "5", "6", "Error");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("Error", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInclusiveBetweenLong()
|
||||
{
|
||||
Validate.inclusiveBetween(0, 2, 1);
|
||||
Validate.inclusiveBetween(0, 2, 2);
|
||||
try {
|
||||
|
@ -846,9 +869,8 @@ public class ValidateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testInclusiveBetween_withMessage()
|
||||
public void testInclusiveBetweenLong_withMessage()
|
||||
{
|
||||
Validate.inclusiveBetween("a", "c", "b", "Error");
|
||||
Validate.inclusiveBetween(0, 2, 1, "Error");
|
||||
Validate.inclusiveBetween(0, 2, 2, "Error");
|
||||
try {
|
||||
|
@ -859,10 +881,71 @@ public class ValidateTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInclusiveBetweenDouble()
|
||||
{
|
||||
Validate.inclusiveBetween(0.1, 2.1, 1.1);
|
||||
Validate.inclusiveBetween(0.1, 2.1, 2.1);
|
||||
try {
|
||||
Validate.inclusiveBetween(0.1, 5.1, 6.1);
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("The value 6.1 is not in the specified inclusive range of 0.1 to 5.1", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInclusiveBetweenDouble_withMessage()
|
||||
{
|
||||
Validate.inclusiveBetween(0.1, 2.1, 1.1, "Error");
|
||||
Validate.inclusiveBetween(0.1, 2.1, 2.1, "Error");
|
||||
try {
|
||||
Validate.inclusiveBetween(0.1, 5.1, 6.1, "Error");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("Error", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusiveBetween()
|
||||
{
|
||||
Validate.exclusiveBetween("a", "c", "b");
|
||||
try {
|
||||
Validate.exclusiveBetween("0", "5", "6");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final 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 (final IllegalArgumentException e) {
|
||||
assertEquals("The value 5 is not in the specified exclusive range of 0 to 5", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusiveBetween_withMessage()
|
||||
{
|
||||
Validate.exclusiveBetween("a", "c", "b", "Error");
|
||||
try {
|
||||
Validate.exclusiveBetween("0", "5", "6", "Error");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("Error", e.getMessage());
|
||||
}
|
||||
try {
|
||||
Validate.exclusiveBetween("0", "5", "5", "Error");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("Error", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusiveBetweenLong()
|
||||
{
|
||||
Validate.exclusiveBetween(0, 2, 1);
|
||||
try {
|
||||
Validate.exclusiveBetween(0, 5, 6);
|
||||
|
@ -879,9 +962,8 @@ public class ValidateTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void testExclusiveBetween_withMessage()
|
||||
public void testExclusiveBetweenLong_withMessage()
|
||||
{
|
||||
Validate.exclusiveBetween("a", "c", "b", "Error");
|
||||
Validate.exclusiveBetween(0, 2, 1, "Error");
|
||||
try {
|
||||
Validate.exclusiveBetween(0, 5, 6, "Error");
|
||||
|
@ -897,6 +979,42 @@ public class ValidateTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusiveBetweenDouble()
|
||||
{
|
||||
Validate.exclusiveBetween(0.1, 2.1, 1.1);
|
||||
try {
|
||||
Validate.exclusiveBetween(0.1, 5.1, 6.1);
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("The value 6.1 is not in the specified exclusive range of 0.1 to 5.1", e.getMessage());
|
||||
}
|
||||
try {
|
||||
Validate.exclusiveBetween(0.1, 5.1, 5.1);
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("The value 5.1 is not in the specified exclusive range of 0.1 to 5.1", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExclusiveBetweenDouble_withMessage()
|
||||
{
|
||||
Validate.exclusiveBetween(0.1, 2.1, 1.1, "Error");
|
||||
try {
|
||||
Validate.exclusiveBetween(0.1, 5.1, 6.1, "Error");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("Error", e.getMessage());
|
||||
}
|
||||
try {
|
||||
Validate.exclusiveBetween(0.1, 5.1, 5.1, "Error");
|
||||
fail("Expecting IllegalArgumentException");
|
||||
} catch (final IllegalArgumentException e) {
|
||||
assertEquals("Error", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsInstanceOf() {
|
||||
Validate.isInstanceOf(String.class, "hi");
|
||||
|
|
Loading…
Reference in New Issue