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>
|
<body>
|
||||||
|
|
||||||
<release version="3.3" date="TBA" description="Bugfix and Feature release">
|
<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-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>
|
<action issue="LANG-915" type="fix" dev="britter" due-to="Sergio Fernández">Wrong locale handling in LocaleUtils.toLocale()</action>
|
||||||
</release>
|
</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
|
// 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
|
// isInstanceOf
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -835,10 +835,8 @@ public class ValidateTest {
|
||||||
public void testInclusiveBetween()
|
public void testInclusiveBetween()
|
||||||
{
|
{
|
||||||
Validate.inclusiveBetween("a", "c", "b");
|
Validate.inclusiveBetween("a", "c", "b");
|
||||||
Validate.inclusiveBetween(0, 2, 1);
|
|
||||||
Validate.inclusiveBetween(0, 2, 2);
|
|
||||||
try {
|
try {
|
||||||
Validate.inclusiveBetween(0, 5, 6);
|
Validate.inclusiveBetween("0", "5", "6");
|
||||||
fail("Expecting IllegalArgumentException");
|
fail("Expecting IllegalArgumentException");
|
||||||
} catch (final IllegalArgumentException e) {
|
} catch (final IllegalArgumentException e) {
|
||||||
assertEquals("The value 6 is not in the specified inclusive range of 0 to 5", e.getMessage());
|
assertEquals("The value 6 is not in the specified inclusive range of 0 to 5", e.getMessage());
|
||||||
|
@ -849,6 +847,30 @@ public class ValidateTest {
|
||||||
public void testInclusiveBetween_withMessage()
|
public void testInclusiveBetween_withMessage()
|
||||||
{
|
{
|
||||||
Validate.inclusiveBetween("a", "c", "b", "Error");
|
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 {
|
||||||
|
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 testInclusiveBetweenLong_withMessage()
|
||||||
|
{
|
||||||
Validate.inclusiveBetween(0, 2, 1, "Error");
|
Validate.inclusiveBetween(0, 2, 1, "Error");
|
||||||
Validate.inclusiveBetween(0, 2, 2, "Error");
|
Validate.inclusiveBetween(0, 2, 2, "Error");
|
||||||
try {
|
try {
|
||||||
|
@ -858,11 +880,72 @@ public class ValidateTest {
|
||||||
assertEquals("Error", e.getMessage());
|
assertEquals("Error", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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
|
@Test
|
||||||
public void testExclusiveBetween()
|
public void testExclusiveBetween()
|
||||||
{
|
{
|
||||||
Validate.exclusiveBetween("a", "c", "b");
|
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);
|
Validate.exclusiveBetween(0, 2, 1);
|
||||||
try {
|
try {
|
||||||
Validate.exclusiveBetween(0, 5, 6);
|
Validate.exclusiveBetween(0, 5, 6);
|
||||||
|
@ -877,11 +960,10 @@ public class ValidateTest {
|
||||||
assertEquals("The value 5 is not in the specified exclusive range of 0 to 5", e.getMessage());
|
assertEquals("The value 5 is not in the specified exclusive range of 0 to 5", e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExclusiveBetween_withMessage()
|
public void testExclusiveBetweenLong_withMessage()
|
||||||
{
|
{
|
||||||
Validate.exclusiveBetween("a", "c", "b", "Error");
|
|
||||||
Validate.exclusiveBetween(0, 2, 1, "Error");
|
Validate.exclusiveBetween(0, 2, 1, "Error");
|
||||||
try {
|
try {
|
||||||
Validate.exclusiveBetween(0, 5, 6, "Error");
|
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
|
@Test
|
||||||
public void testIsInstanceOf() {
|
public void testIsInstanceOf() {
|
||||||
Validate.isInstanceOf(String.class, "hi");
|
Validate.isInstanceOf(String.class, "hi");
|
||||||
|
|
Loading…
Reference in New Issue